Use redirect in WorldgenRandom instead of overwriting

This commit is contained in:
DBotThePony 2025-03-12 15:11:51 +07:00
parent 72277eb71c
commit 8f509dcffb
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 19 additions and 9 deletions

View File

@ -34,7 +34,7 @@ mod_name=Better Random
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=BSD 2 Clause mod_license=BSD 2 Clause
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=1.2.1 mod_version=1.2.2
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View File

@ -4,29 +4,39 @@ import net.minecraft.util.RandomSource;
import net.minecraft.world.level.levelgen.LegacyRandomSource; import net.minecraft.world.level.levelgen.LegacyRandomSource;
import net.minecraft.world.level.levelgen.WorldgenRandom; import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.world.level.levelgen.XoroshiroRandomSource; import net.minecraft.world.level.levelgen.XoroshiroRandomSource;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.dbotthepony.mc.prng.GJRAND64RandomSource; import ru.dbotthepony.mc.prng.GJRAND64RandomSource;
@Mixin(WorldgenRandom.class) @Mixin(WorldgenRandom.class)
public abstract class WorldgenRandomMixin { public abstract class WorldgenRandomMixin {
@Shadow @Shadow
protected RandomSource randomSource; private RandomSource randomSource;
// quite lazy solution because if someone genuinely want to get lcg they wont be able to // This is a mixed bag os a solution.
@Inject( // On one hand, we cover all cases where WorldgenRandom getting called within vanilla code,
// on other hand it also detours and redirects calls done by mods, which *may* delibarately use LCG or Xoroshiro128++
// But we also need to consider that mods will likely just copy/do what vanilla code does, and hence
// they expect code to follow same behavior as vanilla, so in such case us redirecting PRNG is completely desirable
@Redirect(
method = "<init>(Lnet/minecraft/util/RandomSource;)V", method = "<init>(Lnet/minecraft/util/RandomSource;)V",
at = @At("TAIL") at = @At(
value = "FIELD",
target = "randomSource:Lnet/minecraft/util/RandomSource;",
opcode = Opcodes.PUTFIELD
)
) )
public void constructorMix(RandomSource randomSource, CallbackInfo info) { public void constructorMix(WorldgenRandom self, RandomSource randomSource) {
if (randomSource instanceof LegacyRandomSource rng) { if (randomSource instanceof LegacyRandomSource rng) {
this.randomSource = new GJRAND64RandomSource(rng.seed.get()); this.randomSource = new GJRAND64RandomSource(rng.seed.get());
} else if (randomSource instanceof XoroshiroRandomSource rng) { } else if (randomSource instanceof XoroshiroRandomSource rng) {
this.randomSource = new GJRAND64RandomSource(rng.randomNumberGenerator.seedHi, rng.randomNumberGenerator.seedLo); this.randomSource = new GJRAND64RandomSource(rng.randomNumberGenerator.seedHi, rng.randomNumberGenerator.seedLo);
} else {
this.randomSource = randomSource;
} }
} }

View File

@ -6,7 +6,7 @@ protected net.minecraft.world.RandomSequences includeSequenceId
public net.minecraft.world.RandomSequences$DirtyMarkingRandomSource public net.minecraft.world.RandomSequences$DirtyMarkingRandomSource
public net.minecraft.world.RandomSequences$DirtyMarkingRandomSource <init>(Lnet/minecraft/world/RandomSequences;Lnet/minecraft/util/RandomSource;)V public net.minecraft.world.RandomSequences$DirtyMarkingRandomSource <init>(Lnet/minecraft/world/RandomSequences;Lnet/minecraft/util/RandomSource;)V
protected-f net.minecraft.world.level.levelgen.WorldgenRandom randomSource private-f net.minecraft.world.level.levelgen.WorldgenRandom randomSource
public net.minecraft.world.level.levelgen.LegacyRandomSource seed public net.minecraft.world.level.levelgen.LegacyRandomSource seed
public net.minecraft.world.level.levelgen.XoroshiroRandomSource randomNumberGenerator public net.minecraft.world.level.levelgen.XoroshiroRandomSource randomNumberGenerator
public net.minecraft.world.level.levelgen.Xoroshiro128PlusPlus seedLo public net.minecraft.world.level.levelgen.Xoroshiro128PlusPlus seedLo