Faster otmRandom

This commit is contained in:
DBotThePony 2025-03-11 11:39:22 +07:00
parent 6c0265582a
commit 0159931930
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 6 additions and 12 deletions

View File

@ -2,20 +2,18 @@ package ru.dbotthepony.mc.otm.mixin;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import net.neoforged.fml.ModList;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import ru.dbotthepony.mc.otm.core.IMatteryLevel;
import ru.dbotthepony.mc.otm.core.util.GJRAND64RandomSource;
@Mixin(Level.class)
public abstract class LevelMixin implements IMatteryLevel {
private RandomSource otm_random;
private final RandomSource otm_random = ModList.get().isLoaded("better_random") ? null : new GJRAND64RandomSource();
@Override
public @NotNull RandomSource getOtmRandom() {
if (otm_random == null)
otm_random = new GJRAND64RandomSource();
public @Nullable RandomSource getOtmRandom() {
return otm_random;
}
}

View File

@ -11,11 +11,7 @@ interface IMatteryLevel {
* Original Minecraft use LCG, which may show bad behavior when repeatedly sampled *a lot*,
* which is what [Level]'s random is used for. OTM provided PRNG should behave better in this scenario.
*/
val otmRandom: RandomSource
val otmRandom: RandomSource?
}
private val isBetterRandomLoaded by lazy {
ModList.get().isLoaded("better_random")
}
val Level.otmRandom: RandomSource get() = if (isBetterRandomLoaded) random else (this as IMatteryLevel).otmRandom
val Level.otmRandom: RandomSource get() = (this as IMatteryLevel).otmRandom ?: random