diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java index 9c54a02..7a791cc 100644 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java @@ -45,7 +45,7 @@ public record TweaksConfig( boolean updaterEnabled, boolean hardcoreEnabled, - float hardcoreChance, + double hardcoreChance, boolean sleepEnabled, boolean sleepInstant, @@ -117,7 +117,7 @@ public record TweaksConfig( boolean updaterEnabled = config.getBoolean("updater.enabled"); boolean hardcoreEnabled = config.getBoolean("hardcore.enabled"); - float hardcoreChance = (float) config.getDouble("hardcore.chance"); + double hardcoreChance = config.getDouble("hardcore.chance"); boolean sleepEnabled = config.getBoolean("sleep.enabled"); boolean sleepInstant = config.getBoolean("sleep.instant"); diff --git a/src/main/java/eu/m724/tweaks/hardcore/HardcoreManager.java b/src/main/java/eu/m724/tweaks/hardcore/HardcoreManager.java index 0dc632b..e03179e 100644 --- a/src/main/java/eu/m724/tweaks/hardcore/HardcoreManager.java +++ b/src/main/java/eu/m724/tweaks/hardcore/HardcoreManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Minecon724 + * Copyright (C) 2025 Minecon724 * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file * in the project root for the full license text. */ @@ -8,14 +8,40 @@ package eu.m724.tweaks.hardcore; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.events.*; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import eu.m724.tweaks.DebugLogger; import eu.m724.tweaks.TweaksConfig; +import net.minecraft.world.level.levelgen.RandomSupport; +import net.minecraft.world.level.levelgen.Xoroshiro128PlusPlus; import org.bukkit.plugin.Plugin; +import java.math.BigDecimal; +import java.math.BigInteger; + +// how we do it is much faster than any Random public class HardcoreManager { - private final float chance = TweaksConfig.getConfig().hardcoreChance(); + private final double chance = TweaksConfig.getConfig().hardcoreChance(); + + private final long chanceLong = BigInteger.valueOf(Long.MIN_VALUE) + .add( + new BigDecimal( + BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.valueOf(Long.MIN_VALUE)) + ).multiply( + BigDecimal.valueOf(chance) + ).toBigInteger() + ).longValue(); + + private final Xoroshiro128PlusPlus rng = new Xoroshiro128PlusPlus( + RandomSupport.generateUniqueSeed(), + RandomSupport.generateUniqueSeed() + ); public void init(Plugin plugin) { + DebugLogger.fine("Chance long: " + chanceLong); + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter( plugin, ListenerPriority.NORMAL, @@ -24,9 +50,8 @@ public class HardcoreManager { @Override public void onPacketSending(PacketEvent event) { PacketContainer packet = event.getPacket(); - int entityId = packet.getIntegers().read(0); - if (chance > ((48271 * entityId) % 65537) / 65537f) // gotta be fast + if (rng.nextLong() < chanceLong) // the "is hardcore" boolean https://wiki.vg/Protocol#Login_.28play.29 packet.getBooleans().write(0, true); }