PaperMC/patches/server/0073-Use-a-Shared-Random-for-Entities.patch
Spottedleaf 01a13871de
Rewrite chunk system (#8177)
Patch documentation to come

Issues with the old system that are fixed now:
- World generation does not scale with cpu cores effectively.
- Relies on the main thread for scheduling and maintaining chunk state, dropping chunk load/generate rates at lower tps.
- Unreliable prioritisation of chunk gen/load calls that block the main thread.
- Shutdown logic is utterly unreliable, as it has to wait for all chunks to unload - is it guaranteed that the chunk system is in a state on shutdown that it can reliably do this? Watchdog shutdown also typically failed due to thread checks, which is now resolved.
- Saving of data is not unified (i.e can save chunk data without saving entity data, poses problems for desync if shutdown is really abnormal.
- Entities are not loaded with chunks. This caused quite a bit of headache for Chunk#getEntities API, but now the new chunk system loads entities with chunks so that they are ready whenever the chunk loads in. Effectively brings the behavior back to 1.16 era, but still storing entities in their own separate regionfiles.

The above list is not complete. The patch documentation will complete it.

New chunk system hard relies on starlight and dataconverter, and most importantly the new concurrent utilities in ConcurrentUtil.

Some of the old async chunk i/o interface (i.e the old file io thread reroutes _some_ calls to the new file io thread) is kept for plugin compat reasons. It will be removed in the next major version of minecraft.

The old legacy chunk system patches have been moved to the removed folder in case we need them again.
2022-09-26 01:02:51 -07:00

113 lines
4.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 22 Mar 2016 00:33:47 -0400
Subject: [PATCH] Use a Shared Random for Entities
Reduces memory usage and provides ensures more randomness, Especially since a lot of garbage entity objects get created.
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index e28ccb5651a793775166d51859620882ee54b641..233c8950816521be5a9d099c29c99d0a421d30e4 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -159,6 +159,79 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
return tag.contains("Bukkit.updateLevel") && tag.getInt("Bukkit.updateLevel") >= level;
}
+ // Paper start
+ public static RandomSource SHARED_RANDOM = new RandomRandomSource();
+ private static final class RandomRandomSource extends java.util.Random implements net.minecraft.world.level.levelgen.BitRandomSource {
+ private boolean locked = false;
+
+ @Override
+ public synchronized void setSeed(long seed) {
+ if (locked) {
+ LOGGER.error("Ignoring setSeed on Entity.SHARED_RANDOM", new Throwable());
+ } else {
+ super.setSeed(seed);
+ locked = true;
+ }
+ }
+
+ @Override
+ public RandomSource fork() {
+ return new net.minecraft.world.level.levelgen.LegacyRandomSource(this.nextLong());
+ }
+
+ @Override
+ public net.minecraft.world.level.levelgen.PositionalRandomFactory forkPositional() {
+ return new net.minecraft.world.level.levelgen.LegacyRandomSource.LegacyPositionalRandomFactory(this.nextLong());
+ }
+
+ // these below are added to fix reobf issues that I don't wanna deal with right now
+ @Override
+ public int next(int bits) {
+ return super.next(bits);
+ }
+
+ @Override
+ public int nextInt(int origin, int bound) {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt(origin, bound);
+ }
+
+ @Override
+ public long nextLong() {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextLong();
+ }
+
+ @Override
+ public int nextInt() {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt();
+ }
+
+ @Override
+ public int nextInt(int bound) {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextInt(bound);
+ }
+
+ @Override
+ public boolean nextBoolean() {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextBoolean();
+ }
+
+ @Override
+ public float nextFloat() {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextFloat();
+ }
+
+ @Override
+ public double nextDouble() {
+ return net.minecraft.world.level.levelgen.BitRandomSource.super.nextDouble();
+ }
+
+ @Override
+ public double nextGaussian() {
+ return super.nextGaussian();
+ }
+ }
+ // Paper end
+
private CraftEntity bukkitEntity;
public CraftEntity getBukkitEntity() {
@@ -398,7 +471,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
this.bb = Entity.INITIAL_AABB;
this.stuckSpeedMultiplier = Vec3.ZERO;
this.nextStep = 1.0F;
- this.random = RandomSource.create();
+ this.random = SHARED_RANDOM; // Paper
this.remainingFireTicks = -this.getFireImmuneTicks();
this.fluidHeight = new Object2DoubleArrayMap(2);
this.fluidOnEyes = new HashSet();
diff --git a/src/main/java/net/minecraft/world/entity/animal/Squid.java b/src/main/java/net/minecraft/world/entity/animal/Squid.java
index 8e63ab510e4c2e458da726ca67be4d3b5274e7b7..a51424d29ac353cf1bec4d1484db0acb63bebba5 100644
--- a/src/main/java/net/minecraft/world/entity/animal/Squid.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Squid.java
@@ -46,7 +46,7 @@ public class Squid extends WaterAnimal {
public Squid(EntityType<? extends Squid> type, Level world) {
super(type, world);
- this.random.setSeed((long) this.getId());
+ //this.random.setSeed((long) this.getId()); // Paper - we set the random to shared, do not clobber the seed
this.tentacleSpeed = 1.0F / (this.random.nextFloat() + 1.0F) * 0.2F;
}