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 f5bc3497831877e0c2b7dc1cbd8abe3a67d7695b..5783a8014e3faf93b81903c947b2534e41ba6037 100644 --- a/src/main/java/net/minecraft/server/commands/SpreadPlayersCommand.java +++ b/src/main/java/net/minecraft/server/commands/SpreadPlayersCommand.java @@ -65,7 +65,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 8e216eaafb1b90d4fd373a4384ecff0b1e917835..43c170fe1eed27e1b0367a3b277cc6d034bc34e1 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -441,7 +441,7 @@ public class ServerPlayer extends Player { 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; @@ -478,7 +478,7 @@ public class ServerPlayer extends Player { 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 1ef089dbf83de35d875c00efdf468c397be56978..b9f5dc95f859acb8f8fd4739537485af7187ae3b 100644 --- a/src/main/java/net/minecraft/server/rcon/thread/QueryThreadGs4.java +++ b/src/main/java/net/minecraft/server/rcon/thread/QueryThreadGs4.java @@ -349,7 +349,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 b8c238287e0639b578170c6fec0d4db5a1a59fe7..054c68780c0ac8e0f64dcc832d5d272e75bc2d86 100644 --- a/src/main/java/net/minecraft/world/entity/projectile/FishingHook.java +++ b/src/main/java/net/minecraft/world/entity/projectile/FishingHook.java @@ -86,7 +86,7 @@ public class FishingHook extends Projectile { private FishingHook(EntityType type, Level world, int luckOfTheSeaLevel, int lureLevel) { 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.noCulling = true; 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 0db3cbcdbc7f73dd2c10bfb696209753fa33aa8d..5d8968d2c22d2548ecb1b5dde0ead600b1a83735 100644 --- a/src/main/java/net/minecraft/world/entity/raid/Raid.java +++ b/src/main/java/net/minecraft/world/entity/raid/Raid.java @@ -111,7 +111,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; @@ -125,7 +125,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/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java index 8f2c0dcb0ca69ea1b09bb055c6a9494980be8e92..2a7813209d664fd8c123488ce9c530b4440be163 100644 --- a/src/main/java/net/minecraft/world/level/Explosion.java +++ b/src/main/java/net/minecraft/world/level/Explosion.java @@ -81,7 +81,7 @@ public class Explosion { } public Explosion(Level world, @Nullable Entity entity, @Nullable DamageSource damageSource, @Nullable ExplosionDamageCalculator behavior, double x, double y, double z, float power, boolean createFire, Explosion.BlockInteraction destructionType) { - this.random = RandomSource.create(); + this.random = world == null || world.random == null ? RandomSource.create() : world.random; // Gale - Patina - reduce RandomSource instances this.toBlow = new ObjectArrayList(); this.hitPlayers = Maps.newHashMap(); this.level = world; 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 414a47a82927bed02a4f3cf330c78e46f1d66782..cc17cb985856fd8d1083b5b1f57f82c651d28443 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 @@ -361,7 +361,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 9cb6a36ac210574a3d62202b16058e9fdc94691e..51a3188f4873666100ba438550efa76e59342afd 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 @@ -454,7 +454,7 @@ public class EndDragonFight { this.level.registryAccess().registry(Registries.CONFIGURED_FEATURE).flatMap((iregistry) -> { return iregistry.getHolder(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 }); } @@ -472,7 +472,7 @@ public class EndDragonFight { this.portalLocation = this.portalLocation.atY(this.level.getMinBuildHeight() + 1); } // Paper end - worldgenendtrophy.place(FeatureConfiguration.NONE, this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), this.portalLocation); + worldgenendtrophy.place(FeatureConfiguration.NONE, this.level, this.level.getChunkSource().getGenerator(), this.level.random, this.portalLocation); // Gale - Patina - reduce RandomSource instances } @Nullable