13
0
geforkt von Mirrors/Paper

Fix incorrect random nextLong to nextInt (#8009)

Dieser Commit ist enthalten in:
Jake Potrebic 2022-06-17 00:00:17 -07:00
Ursprung 969129420b
Commit b20573ca8c
2 geänderte Dateien mit 29 neuen und 54 gelöschten Zeilen

Datei anzeigen

@ -27,11 +27,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+package io.papermc.paper.util.math; +package io.papermc.paper.util.math;
+ +
+import net.minecraft.util.RandomSource; +import net.minecraft.util.RandomSource;
+import net.minecraft.world.level.levelgen.LegacyRandomSource;
+import net.minecraft.world.level.levelgen.PositionalRandomFactory; +import net.minecraft.world.level.levelgen.PositionalRandomFactory;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier;
+ +
+import java.util.Random; +@DefaultQualifier(NonNull.class)
+ +public final class ThreadUnsafeRandom extends LegacyRandomSource {
+public final class ThreadUnsafeRandom extends Random implements RandomSource {
+ +
+ // See javadoc and internal comments for java.util.Random where these values come from, how they are used, and the author for them. + // See javadoc and internal comments for java.util.Random where these values come from, how they are used, and the author for them.
+ private static final long multiplier = 0x5DEECE66DL; + private static final long multiplier = 0x5DEECE66DL;
@ -44,9 +46,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ +
+ private long seed; + private long seed;
+ +
+ public ThreadUnsafeRandom(long seed) {
+ super(seed);
+ }
+
+ @Override + @Override
+ public RandomSource fork() { + public RandomSource fork() {
+ return new ThreadUnsafeRandom(); + return new ThreadUnsafeRandom(this.nextLong());
+ } + }
+ +
+ @Override + @Override
@ -55,18 +61,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ +
+ @Override + @Override
+ public int nextInt(int origin, int bound) {
+ return RandomSource.super.nextInt(origin, bound);
+ }
+
+ @Override
+ public void setSeed(long seed) { + public void setSeed(long seed) {
+ // note: called by Random constructor + // note: called by Random constructor
+ this.seed = initialScramble(seed); + this.seed = initialScramble(seed);
+ } + }
+ +
+ @Override + @Override
+ protected int next(int bits) { + public int next(int bits) {
+ // avoid the expensive CAS logic used by superclass + // avoid the expensive CAS logic used by superclass
+ return (int) (((this.seed = this.seed * multiplier + addend) & mask) >>> (48 - bits)); + return (int) (((this.seed = this.seed * multiplier + addend) & mask) >>> (48 - bits));
+ } + }
@ -87,37 +88,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // however there's nothing that uses this class that relies on it + // however there's nothing that uses this class that relies on it
+ return fastRandomBounded(this.next(32) & 0xFFFFFFFFL, bound); + return fastRandomBounded(this.next(32) & 0xFFFFFFFFL, bound);
+ } + }
+
+ // these below are added to fix reobf issues that I don't wanna deal with right now
+ @Override
+ public long nextLong() {
+ return super.nextInt();
+ }
+
+ @Override
+ public int nextInt() {
+ return super.nextInt();
+ }
+
+ @Override
+ public boolean nextBoolean() {
+ return super.nextBoolean();
+ }
+
+ @Override
+ public float nextFloat() {
+ return super.nextFloat();
+ }
+
+ @Override
+ public double nextDouble() {
+ return super.nextDouble();
+ }
+
+ @Override
+ public double nextGaussian() {
+ return super.nextGaussian();
+ }
+} +}
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
@ -129,7 +99,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
} }
+ // Paper start - optimise random block ticking + // Paper start - optimise random block ticking
+ private final BlockPos.MutableBlockPos chunkTickMutablePosition = new BlockPos.MutableBlockPos(); + private final BlockPos.MutableBlockPos chunkTickMutablePosition = new BlockPos.MutableBlockPos();
+ private final io.papermc.paper.util.math.ThreadUnsafeRandom randomTickRandom = new io.papermc.paper.util.math.ThreadUnsafeRandom(); + private final io.papermc.paper.util.math.ThreadUnsafeRandom randomTickRandom = new io.papermc.paper.util.math.ThreadUnsafeRandom(this.random.nextLong());
+ // Paper end + // Paper end
public void tickChunk(LevelChunk chunk, int randomTickSpeed) { public void tickChunk(LevelChunk chunk, int randomTickSpeed) {

Datei anzeigen

@ -15,7 +15,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start + // Paper start
+ public static RandomSource SHARED_RANDOM = new RandomRandomSource(); + public static RandomSource SHARED_RANDOM = new RandomRandomSource();
+ private static final class RandomRandomSource extends java.util.Random implements RandomSource { + private static final class RandomRandomSource extends java.util.Random implements net.minecraft.world.level.levelgen.BitRandomSource {
+ private boolean locked = false; + private boolean locked = false;
+ +
+ @Override + @Override
@ -38,40 +38,45 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return new net.minecraft.world.level.levelgen.LegacyRandomSource.LegacyPositionalRandomFactory(this.nextLong()); + return new net.minecraft.world.level.levelgen.LegacyRandomSource.LegacyPositionalRandomFactory(this.nextLong());
+ } + }
+ +
+ @Override
+ public int nextInt(int origin, int bound) {
+ return RandomSource.super.nextInt(origin, bound);
+ }
+
+ // these below are added to fix reobf issues that I don't wanna deal with right now + // these below are added to fix reobf issues that I don't wanna deal with right now
+ @Override + @Override
+ public int next(int bits) {
+ return super.next(bits);
+ }
+
+ @Override
+ public int nextInt(int origin, int bound) {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt(origin, bound);
+ }
+
+ @Override
+ public long nextLong() { + public long nextLong() {
+ return super.nextInt(); + return net.minecraft.world.level.levelgen.BitRandomSource.super.nextLong();
+ } + }
+ +
+ @Override + @Override
+ public int nextInt() { + public int nextInt() {
+ return super.nextInt(); + return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt();
+ } + }
+ +
+ @Override + @Override
+ public int nextInt(int bound) { + public int nextInt(int bound) {
+ return super.nextInt(bound); + return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt(bound);
+ } + }
+ +
+ @Override + @Override
+ public boolean nextBoolean() { + public boolean nextBoolean() {
+ return super.nextBoolean(); + return net.minecraft.world.level.levelgen.BitRandomSource.super.nextBoolean();
+ } + }
+ +
+ @Override + @Override
+ public float nextFloat() { + public float nextFloat() {
+ return super.nextFloat(); + return net.minecraft.world.level.levelgen.BitRandomSource.super.nextFloat();
+ } + }
+ +
+ @Override + @Override
+ public double nextDouble() { + public double nextDouble() {
+ return super.nextDouble(); + return net.minecraft.world.level.levelgen.BitRandomSource.super.nextDouble();
+ } + }
+ +
+ @Override + @Override