From b20573ca8c5803914bbaa8628215a3f64d1f5531 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Fri, 17 Jun 2022 00:00:17 -0700 Subject: [PATCH] Fix incorrect random nextLong to nextInt (#8009) --- .../Optimise-random-block-ticking.patch | 54 +++++-------------- .../Use-a-Shared-Random-for-Entities.patch | 29 +++++----- 2 files changed, 29 insertions(+), 54 deletions(-) diff --git a/patches/server/Optimise-random-block-ticking.patch b/patches/server/Optimise-random-block-ticking.patch index 9c611727de..b396a29ca5 100644 --- a/patches/server/Optimise-random-block-ticking.patch +++ b/patches/server/Optimise-random-block-ticking.patch @@ -27,11 +27,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +package io.papermc.paper.util.math; + +import net.minecraft.util.RandomSource; ++import net.minecraft.world.level.levelgen.LegacyRandomSource; +import net.minecraft.world.level.levelgen.PositionalRandomFactory; ++import org.checkerframework.checker.nullness.qual.NonNull; ++import org.checkerframework.framework.qual.DefaultQualifier; + -+import java.util.Random; -+ -+public final class ThreadUnsafeRandom extends Random implements RandomSource { ++@DefaultQualifier(NonNull.class) ++public final class ThreadUnsafeRandom extends LegacyRandomSource { + + // 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; @@ -44,9 +46,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + private long seed; + ++ public ThreadUnsafeRandom(long seed) { ++ super(seed); ++ } ++ + @Override + public RandomSource fork() { -+ return new ThreadUnsafeRandom(); ++ return new ThreadUnsafeRandom(this.nextLong()); + } + + @Override @@ -55,18 +61,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + + @Override -+ public int nextInt(int origin, int bound) { -+ return RandomSource.super.nextInt(origin, bound); -+ } -+ -+ @Override + public void setSeed(long seed) { + // note: called by Random constructor + this.seed = initialScramble(seed); + } + + @Override -+ protected int next(int bits) { ++ public int next(int bits) { + // avoid the expensive CAS logic used by superclass + 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 + 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 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 @@ -129,7 +99,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 } + // Paper start - optimise random block ticking + 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 public void tickChunk(LevelChunk chunk, int randomTickSpeed) { diff --git a/patches/server/Use-a-Shared-Random-for-Entities.patch b/patches/server/Use-a-Shared-Random-for-Entities.patch index 117a2d15b7..67e9f60cf4 100644 --- a/patches/server/Use-a-Shared-Random-for-Entities.patch +++ b/patches/server/Use-a-Shared-Random-for-Entities.patch @@ -15,7 +15,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + // Paper start + 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; + + @Override @@ -38,40 +38,45 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + 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 + @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() { -+ return super.nextInt(); ++ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextLong(); + } + + @Override + public int nextInt() { -+ return super.nextInt(); ++ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt(); + } + + @Override + public int nextInt(int bound) { -+ return super.nextInt(bound); ++ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt(bound); + } + + @Override + public boolean nextBoolean() { -+ return super.nextBoolean(); ++ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextBoolean(); + } + + @Override + public float nextFloat() { -+ return super.nextFloat(); ++ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextFloat(); + } + + @Override + public double nextDouble() { -+ return super.nextDouble(); ++ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextDouble(); + } + + @Override