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 61e2fc796d49fe45229a1b0cd630355ab7d59883..1b3d0bb2ea57572b1419373873f04cc4e62d3795 100644 --- a/src/main/java/net/minecraft/server/level/ServerPlayer.java +++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java @@ -466,7 +466,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; @@ -505,7 +505,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 1223c5d23d0ea6aed068bdf0f5725e2ad49fc82c..5df85ba4904a6b4b69ab584e9f30d34c68925a5c 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.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 5c0e5803284bfc82c2dd8bba9ded0ba7aac4962a..2cc37e5734233954710c4ef55bf3b2918f2026d5 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/Explosion.java b/src/main/java/net/minecraft/world/level/Explosion.java index 94e0a6cdf5c340dd11374e890963d043a440650a..df04f39d8e40eaab3a29ed33f253728d236c6957 100644 --- a/src/main/java/net/minecraft/world/level/Explosion.java +++ b/src/main/java/net/minecraft/world/level/Explosion.java @@ -336,7 +336,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, Holder 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 89df488afeffd0c060d2d0e7fae16daf978bd192..ec0dc7dc5b5c0690a592006f29513f0e80123f8a 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 84300f2f7b7be4f5281edd8e263646dbcbb3ba07..db38112104fe6009b57788b79e0cad4cf180badb 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 @@ -474,7 +474,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 }); } @@ -492,7 +492,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);