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/src/main/java/net/minecraft/server/commands/SpreadPlayersCommand.java b/src/main/java/net/minecraft/server/commands/SpreadPlayersCommand.java index 6f69e74d8662025c4148b36de7a35e789abcd1d2..8c18763ff74a6fbc87eafa1170c4ad9d5f1de22f 100644 --- a/src/main/java/net/minecraft/server/commands/SpreadPlayersCommand.java +++ b/src/main/java/net/minecraft/server/commands/SpreadPlayersCommand.java @@ -66,7 +66,7 @@ public class SpreadPlayersCommand { if (maxY < j) { throw SpreadPlayersCommand.ERROR_INVALID_MAX_HEIGHT.create(maxY, j); } else { - RandomSource randomsource = RandomSource.create(); + RandomSource randomsource = source.getLevel().random; // Gale - Patina - reduce RandomSource instances double d0 = (double) (center.x - maxRange); double d1 = (double) (center.y - maxRange); double d2 = (double) (center.x + maxRange); diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java index 36df6a23f1943c55ecc6bed55b48a77ba3b449e4..22e0d57048aedd3ac1ae5aecb43498fbef26f6ee 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -524,7 +524,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple long l = k * k; int i1 = l > 2147483647L ? Integer.MAX_VALUE : (int) l; int j1 = this.getCoprime(i1); - int k1 = RandomSource.create().nextInt(i1); + int k1 = worldserver.random.nextInt(i1); // Gale - Patina - reduce RandomSource instances for (int l1 = 0; l1 < i1; ++l1) { int i2 = (k1 + j1 * l1) % i1; @@ -563,7 +563,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player imple long l = k * k; int i1 = l > 2147483647L ? Integer.MAX_VALUE : (int) l; int j1 = this.getCoprime(i1); - int k1 = RandomSource.create().nextInt(i1); + int k1 = world.random.nextInt(i1); // Gale - Patina - reduce RandomSource instances for (int l1 = 0; l1 < i1; ++l1) { int i2 = (k1 + j1 * l1) % i1; diff --git a/src/main/java/net/minecraft/server/rcon/thread/QueryThreadGs4.java b/src/main/java/net/minecraft/server/rcon/thread/QueryThreadGs4.java index 0e0867d7add9a024bbe9471f8ff92bbb25996a3d..94ffa65fa99c4c713936f7004d9f46ac94a9fe7a 100644 --- a/src/main/java/net/minecraft/server/rcon/thread/QueryThreadGs4.java +++ b/src/main/java/net/minecraft/server/rcon/thread/QueryThreadGs4.java @@ -341,7 +341,7 @@ public class QueryThreadGs4 extends GenericThread { this.identBytes[2] = bs[5]; this.identBytes[3] = bs[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/src/main/java/net/minecraft/world/entity/projectile/FishingHook.java b/src/main/java/net/minecraft/world/entity/projectile/FishingHook.java index 4daa69c6be6d48563e30343a7e40e4da9ec7e5ad..d33a3b62ec7d5f82366844b2df66518e7df50700 100644 --- a/src/main/java/net/minecraft/world/entity/projectile/FishingHook.java +++ b/src/main/java/net/minecraft/world/entity/projectile/FishingHook.java @@ -88,7 +88,7 @@ public class FishingHook extends Projectile { private FishingHook(EntityType type, Level world, int luckBonus, int waitTimeReductionTicks) { super(type, world); - this.syncronizedRandom = RandomSource.create(); + this.syncronizedRandom = world.random; // Gale - Patina - reduce RandomSource instances this.openWater = true; this.currentState = FishingHook.FishHookState.FLYING; this.luck = Math.max(0, luckBonus); diff --git a/src/main/java/net/minecraft/world/entity/raid/Raid.java b/src/main/java/net/minecraft/world/entity/raid/Raid.java index 4ce08edf6e7a59238b1c4e85acc04eab21b21ee9..3618bbf30d61d7d2c18b9da69125a5f7fefab939 100644 --- a/src/main/java/net/minecraft/world/entity/raid/Raid.java +++ b/src/main/java/net/minecraft/world/entity/raid/Raid.java @@ -116,7 +116,7 @@ public class Raid { public Raid(int id, ServerLevel world, BlockPos pos) { this.raidEvent = new ServerBossEvent(Raid.RAID_NAME_COMPONENT, BossEvent.BossBarColor.RED, BossEvent.BossBarOverlay.NOTCHED_10); - this.random = RandomSource.create(); + this.random = world.random; // Gale - Patina - reduce RandomSource instances this.waveSpawnPos = Optional.empty(); this.id = id; this.level = world; @@ -130,7 +130,7 @@ public class Raid { public Raid(ServerLevel world, CompoundTag nbt) { this.raidEvent = new ServerBossEvent(Raid.RAID_NAME_COMPONENT, BossEvent.BossBarColor.RED, BossEvent.BossBarOverlay.NOTCHED_10); - this.random = RandomSource.create(); + this.random = world.random; // Gale - Patina - reduce RandomSource instances this.waveSpawnPos = Optional.empty(); this.level = world; this.id = nbt.getInt("Id"); diff --git a/src/main/java/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java index 68fd5d3f6553af8af867e34946cb8b3f852da985..c8d2a0ae9da26fb5a3c73ec3388ec503122b4af3 100644 --- a/src/main/java/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java +++ b/src/main/java/net/minecraft/world/level/block/entity/TheEndGatewayBlockEntity.java @@ -268,7 +268,7 @@ public class TheEndGatewayBlockEntity extends TheEndPortalBlockEntity { } private static void spawnGatewayPortal(ServerLevel world, BlockPos pos, EndGatewayConfiguration config) { - Feature.END_GATEWAY.place(config, world, world.getChunkSource().getGenerator(), RandomSource.create(), pos); + Feature.END_GATEWAY.place(config, world, world.getChunkSource().getGenerator(), world.random, pos); // Gale - Patina - reduce RandomSource instances } @Override diff --git a/src/main/java/net/minecraft/world/level/dimension/end/EndDragonFight.java b/src/main/java/net/minecraft/world/level/dimension/end/EndDragonFight.java index b331c93c82c27f9456fec208a0c008c5bedfa8c4..3a015cc92b3d9cf869bd1e4c48e0831d40e41b85 100644 --- a/src/main/java/net/minecraft/world/level/dimension/end/EndDragonFight.java +++ b/src/main/java/net/minecraft/world/level/dimension/end/EndDragonFight.java @@ -475,7 +475,7 @@ public class EndDragonFight { this.level.registryAccess().lookup(Registries.CONFIGURED_FEATURE).flatMap((iregistry) -> { return iregistry.get(EndFeatures.END_GATEWAY_DELAYED); }).ifPresent((holder_c) -> { - ((ConfiguredFeature) holder_c.value()).place(this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), pos); + ((ConfiguredFeature) holder_c.value()).place(this.level, this.level.getChunkSource().getGenerator(), this.level.random, pos); // Gale - Patina - reduce RandomSource instances }); } @@ -493,7 +493,7 @@ public class EndDragonFight { this.portalLocation = this.portalLocation.atY(this.level.getMinY() + 1); } // Paper end - Prevent "softlocked" exit portal generation - if (worldgenendtrophy.place(FeatureConfiguration.NONE, this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), this.portalLocation)) { + if (worldgenendtrophy.place(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);