From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Tue, 29 Nov 2022 00:45:45 +0100 Subject: [PATCH] Reduce RandomSource instances License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://galemc.org This patch is based on the following patch: "don't create new random instance" By: foss-mc <69294560+foss-mc@users.noreply.github.com> As part of: Patina (https://github.com/PatinaMC/Patina) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) diff --git a/net/minecraft/server/commands/SpreadPlayersCommand.java b/net/minecraft/server/commands/SpreadPlayersCommand.java index d381800ad054be6b054dcca43fbe80d3f0c0c771..5904b9d985487ff8bd1f330667c43096aa298b98 100644 --- a/net/minecraft/server/commands/SpreadPlayersCommand.java +++ b/net/minecraft/server/commands/SpreadPlayersCommand.java @@ -107,7 +107,7 @@ public class SpreadPlayersCommand { if (maxHeight < minY) { throw ERROR_INVALID_MAX_HEIGHT.create(maxHeight, minY); } else { - RandomSource randomSource = RandomSource.create(); + RandomSource randomSource = source.getLevel().random; // Gale - Patina - reduce RandomSource instances double d = center.x - maxRange; double d1 = center.y - maxRange; double d2 = center.x + maxRange; diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java index e676e2082700a2bdc2a93b0a356e4f6b1000776e..e56e930df980613a13f92d771f1036eba82b1d19 100644 --- a/net/minecraft/server/level/ServerPlayer.java +++ b/net/minecraft/server/level/ServerPlayer.java @@ -461,7 +461,7 @@ public class ServerPlayer extends Player implements ca.spottedleaf.moonrise.patc long l1 = l * l; int i = l1 > 2147483647L ? Integer.MAX_VALUE : (int)l1; int coprime = this.getCoprime(i); - int randomInt = RandomSource.create().nextInt(i); + int randomInt = level.random.nextInt(i); // Gale - Patina - reduce RandomSource instances for (int i1 = 0; i1 < i; i1++) { int i2 = (randomInt + coprime * i1) % i; diff --git a/net/minecraft/server/rcon/thread/QueryThreadGs4.java b/net/minecraft/server/rcon/thread/QueryThreadGs4.java index 0b8d279a53196f3998b1f6901738ca8e02ef7311..9add567d2ec2f5e9cd5bee84423c5a44433fe188 100644 --- a/net/minecraft/server/rcon/thread/QueryThreadGs4.java +++ b/net/minecraft/server/rcon/thread/QueryThreadGs4.java @@ -341,7 +341,7 @@ public class QueryThreadGs4 extends GenericThread { this.identBytes[2] = data[5]; this.identBytes[3] = data[6]; this.ident = new String(this.identBytes, StandardCharsets.UTF_8); - this.challenge = RandomSource.create().nextInt(16777216); + this.challenge = java.util.concurrent.ThreadLocalRandom.current().nextInt(16777216); // Gale - Patina - reduce RandomSource instances this.challengeBytes = String.format(Locale.ROOT, "\t%s%d\u0000", this.ident, this.challenge).getBytes(StandardCharsets.UTF_8); } diff --git a/net/minecraft/world/entity/projectile/FishingHook.java b/net/minecraft/world/entity/projectile/FishingHook.java index 1e012c7ef699a64ff3f1b00f897bb893ab25ecbd..800db05c1d2f2452a9e8ab7532f5ec10a924b0ce 100644 --- a/net/minecraft/world/entity/projectile/FishingHook.java +++ b/net/minecraft/world/entity/projectile/FishingHook.java @@ -48,7 +48,7 @@ import org.slf4j.Logger; public class FishingHook extends Projectile { private static final Logger LOGGER = LogUtils.getLogger(); - private final RandomSource syncronizedRandom = RandomSource.create(); + private final RandomSource syncronizedRandom; // Gale - Patina - reduce RandomSource instances private boolean biting; public int outOfWaterTime; private static final int MAX_OUT_OF_WATER_TIME = 10; @@ -86,6 +86,7 @@ public class FishingHook extends Projectile { this.minWaitTime = level.paperConfig().fishingTimeRange.minimum; this.maxWaitTime = level.paperConfig().fishingTimeRange.maximum; // Paper end - Configurable fishing time ranges + this.syncronizedRandom = level.random; // Gale - Patina - reduce RandomSource instances } public FishingHook(EntityType entityType, Level level) { diff --git a/net/minecraft/world/entity/raid/Raid.java b/net/minecraft/world/entity/raid/Raid.java index 27234627933ccaa70b2c51a792efb2e68104a84f..7b0de83f1eb79007c6fe0ecf00660143781d1aa6 100644 --- a/net/minecraft/world/entity/raid/Raid.java +++ b/net/minecraft/world/entity/raid/Raid.java @@ -99,7 +99,7 @@ public class Raid { public final ServerBossEvent raidEvent = new ServerBossEvent(RAID_NAME_COMPONENT, BossEvent.BossBarColor.RED, BossEvent.BossBarOverlay.NOTCHED_10); private int postRaidTicks; private int raidCooldownTicks; - private final RandomSource random = RandomSource.create(); + private final RandomSource random; // Gale - Patina - reduce RandomSource instances public final int numGroups; private Raid.RaidStatus status; private int celebrationTicks; @@ -120,6 +120,7 @@ public class Raid { this.center = center; this.numGroups = this.getNumGroups(level.getDifficulty()); this.status = Raid.RaidStatus.ONGOING; + this.random = level.random; // Gale - Patina - reduce RandomSource instances } public Raid(ServerLevel level, CompoundTag compound) { @@ -147,6 +148,7 @@ public class Raid { this.persistentDataContainer.putAll(compound.getCompound(PDC_NBT_KEY)); } // Paper end + this.random = level.random; // Gale - Patina - reduce RandomSource instances } public boolean isOver() { diff --git a/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java b/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java index 5bf39c542757bf97da8909b65c22786a8a30385a..9333db899f86f91a667273f23f24a1c2eca9ad64 100644 --- a/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java +++ b/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java @@ -248,7 +248,7 @@ public class TheEndGatewayBlockEntity extends TheEndPortalBlockEntity { } private static void spawnGatewayPortal(ServerLevel level, BlockPos pos, EndGatewayConfiguration config) { - Feature.END_GATEWAY.place(config, level, level.getChunkSource().getGenerator(), RandomSource.create(), pos); + Feature.END_GATEWAY.place(config, level, level.getChunkSource().getGenerator(), level.random, pos); // Gale - Patina - reduce RandomSource instances } @Override diff --git a/net/minecraft/world/level/dimension/end/EndDragonFight.java b/net/minecraft/world/level/dimension/end/EndDragonFight.java index 6e7e87c32734b3aae354bc34459e5f207da5c78f..b933038ce88cdf285c8b4a901c3493ae7605f57e 100644 --- a/net/minecraft/world/level/dimension/end/EndDragonFight.java +++ b/net/minecraft/world/level/dimension/end/EndDragonFight.java @@ -434,7 +434,7 @@ public class EndDragonFight { .registryAccess() .lookup(Registries.CONFIGURED_FEATURE) .flatMap(registry -> registry.get(EndFeatures.END_GATEWAY_DELAYED)) - .ifPresent(endGatewayFeature -> endGatewayFeature.value().place(this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), pos)); + .ifPresent(endGatewayFeature -> endGatewayFeature.value().place(this.level, this.level.getChunkSource().getGenerator(), this.level.random, pos)); // Gale - Patina - reduce RandomSource instances } public void spawnExitPortal(boolean active) { @@ -453,7 +453,7 @@ public class EndDragonFight { } // Paper end - Prevent "softlocked" exit portal generation if (endPodiumFeature.place( - FeatureConfiguration.NONE, this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), this.portalLocation + FeatureConfiguration.NONE, this.level, this.level.getChunkSource().getGenerator(), this.level.random, this.portalLocation // Gale - Patina - reduce RandomSource instances )) { int i = Mth.positiveCeilDiv(4, 16); this.level.getChunkSource().chunkMap.waitForLightBeforeSending(new ChunkPos(this.portalLocation), i);