Improve hardcore RNG
All checks were successful
/ build (push) Successful in 57s

This commit is contained in:
Minecon724 2025-01-02 13:19:23 +01:00
parent 6ad6550f5d
commit 81fa6440a0
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 32 additions and 7 deletions

View file

@ -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");

View file

@ -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);
}