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 b47a8a082170bcb630c4354be7c77a4cac71d105..b49e3f2cdc1fa5ff3723fae452404664ff710252 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 09e13d3bf936e8a4b1352a4733b7525c1886d8fa..0a0106d9192e6913f7579a4fdd77a9c0fc8366ba 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -453,7 +453,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; @@ -490,7 +490,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 5706c9b744b660d6f7639b8152dce82799c4b466..460aba465eac5cb8d25456c0d4d04a132cee43c7 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 ea0df00966cf9900a8a0153099bbb04e5b47116d..1168465f6b9414b08680660413c62967cd1e95ee 100644 --- a/src/main/java/net/minecraft/world/entity/raid/Raid.java +++ b/src/main/java/net/minecraft/world/entity/raid/Raid.java @@ -110,7 +110,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; @@ -124,7 +124,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 ce8e39d4bf4c7474e1e8694312e1985327a88e62..525e41f3c38c9a79e237f32ac49ba02ab3438c30 100644 --- a/src/main/java/net/minecraft/world/level/Explosion.java +++ b/src/main/java/net/minecraft/world/level/Explosion.java @@ -92,7 +92,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, ParticleOptions particle, ParticleOptions emitterParticle, SoundEvent soundEvent) { - 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 850fc0906f23f1073e6d799222ed442b88af1bff..ff92b93dbffa6f3463c62d55cec535a3871ed7d8 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 @@ -360,7 +360,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 8d57a03793934c8c0b259974bd4d9f9c2cab884d..2bff5f04e67b47bb3fbd6c9b1d235be1bb829b03 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 @@ -472,7 +472,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 }); } @@ -490,7 +490,7 @@ public class EndDragonFight { this.portalLocation = this.portalLocation.atY(this.level.getMinBuildHeight() + 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);