diff --git a/leaf-server/minecraft-patches/features/0085-Multithreaded-Tracker.patch b/leaf-server/minecraft-patches/features/0085-Multithreaded-Tracker.patch index de9c1a53..23247266 100644 --- a/leaf-server/minecraft-patches/features/0085-Multithreaded-Tracker.patch +++ b/leaf-server/minecraft-patches/features/0085-Multithreaded-Tracker.patch @@ -37,7 +37,7 @@ index dd2509996bfd08e8c3f9f2be042229eac6d7692d..a35e9fae8f8da0c42f0616c4f78dc396 private static final byte CHUNK_TICKET_STAGE_NONE = 0; private static final byte CHUNK_TICKET_STAGE_LOADING = 1; diff --git a/net/minecraft/server/level/ChunkMap.java b/net/minecraft/server/level/ChunkMap.java -index 5d9d233e3a568aa6297ed9c703fa450f98158602..08873993b3b75db325fe1f2b4ab1de73b3d05be4 100644 +index 5d9d233e3a568aa6297ed9c703fa450f98158602..4984e57d3b0806164111e79acfc82f6d9b27bfbf 100644 --- a/net/minecraft/server/level/ChunkMap.java +++ b/net/minecraft/server/level/ChunkMap.java @@ -248,6 +248,15 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider @@ -88,9 +88,9 @@ index 5d9d233e3a568aa6297ed9c703fa450f98158602..08873993b3b75db325fe1f2b4ab1de73 this.lastTrackedChunk = chunk; final ServerPlayer[] playersRaw = players.getRawDataUnchecked(); ++ // Leaf start - Multithreaded tracker + final int playersLen = players.size(); // Ensure length won't change in the future tasks + -+ // Leaf start - Multithreaded tracker + if (org.dreeam.leaf.config.modules.async.MultithreadedTracker.enabled && org.dreeam.leaf.config.modules.async.MultithreadedTracker.compatModeEnabled) { + final boolean isServerPlayer = this.entity instanceof ServerPlayer; + final boolean isRealPlayer = isServerPlayer && ((ca.spottedleaf.moonrise.patches.chunk_system.player.ChunkSystemServerPlayer) this.entity).moonrise$isRealPlayer(); diff --git a/leaf-server/minecraft-patches/features/0090-Smart-sort-entities-in-NearestLivingEntitySensor.patch b/leaf-server/minecraft-patches/features/0090-Smart-sort-entities-in-NearestLivingEntitySensor.patch index 1e87e011..87c4d127 100644 --- a/leaf-server/minecraft-patches/features/0090-Smart-sort-entities-in-NearestLivingEntitySensor.patch +++ b/leaf-server/minecraft-patches/features/0090-Smart-sort-entities-in-NearestLivingEntitySensor.patch @@ -12,7 +12,7 @@ In non-strict test, this can give ~60-110% improvement (524ms on Paper, 204ms on under 625 villagers situation. diff --git a/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java b/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java -index b0c5e41fefc7c9adf1a61bd5b52861736657d37e..a887bb8ed2318d6ee39be350a1d0e7223ecf3ff5 100644 +index b0c5e41fefc7c9adf1a61bd5b52861736657d37e..d4d75a3f5d03533626417aaa3b0457ab98acea1c 100644 --- a/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java +++ b/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java @@ -13,17 +13,28 @@ import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities; @@ -21,32 +21,32 @@ index b0c5e41fefc7c9adf1a61bd5b52861736657d37e..a887bb8ed2318d6ee39be350a1d0e722 public class NearestLivingEntitySensor extends Sensor { + + // Leaf start - Smart sort entities in NearestLivingEntitySensor -+ private final org.dreeam.leaf.util.fastBitRadixSort sorter; -+ // Leaf end - Smart sort entities in NearestLivingEntitySensor ++ private final org.dreeam.leaf.util.FastBitRadixSort sorter; ++ + public NearestLivingEntitySensor() { -+ this.sorter = new org.dreeam.leaf.util.fastBitRadixSort(); ++ this.sorter = new org.dreeam.leaf.util.FastBitRadixSort(); + } ++ // Leaf end - Smart sort entities in NearestLivingEntitySensor + @Override protected void doTick(ServerLevel level, T entity) { double attributeValue = entity.getAttributeValue(Attributes.FOLLOW_RANGE); -+ double rangeSqr = attributeValue * attributeValue; AABB aabb = entity.getBoundingBox().inflate(attributeValue, attributeValue, attributeValue); - List entitiesOfClass = level.getEntitiesOfClass( - LivingEntity.class, aabb, matchableEntity -> matchableEntity != entity && matchableEntity.isAlive() - ); - entitiesOfClass.sort(Comparator.comparingDouble(entity::distanceToSqr)); -+ ++ // Leaf start - Smart sort entities in NearestLivingEntitySensor ++ double rangeSqr = attributeValue * attributeValue; + List entities = level.getEntitiesOfClass(LivingEntity.class, aabb, e -> e != entity && e.isAlive() && entity.distanceToSqr(e) <= rangeSqr); -+ + LivingEntity[] sorted = this.sorter.sort(entities, entity, LivingEntity.class); + List sortedList = java.util.Arrays.asList(sorted); -+ Brain brain = entity.getBrain(); - brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, entitiesOfClass); - brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, entity, entitiesOfClass)); + brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, sortedList); + brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, entity, sortedList)); ++ // Leaf end - Smart sort entities in NearestLivingEntitySensor } @Override diff --git a/leaf-server/minecraft-patches/features/0132-Async-chunk-send.patch b/leaf-server/minecraft-patches/features/0132-Async-chunk-send.patch index 196e225f..78fa3b40 100644 --- a/leaf-server/minecraft-patches/features/0132-Async-chunk-send.patch +++ b/leaf-server/minecraft-patches/features/0132-Async-chunk-send.patch @@ -158,7 +158,7 @@ index 14878690a88fd4de3e2c127086607e6c819c636c..64a8b50bfac66f75d8c87d9e6e4000dc // Paper start - PlayerChunkLoadEvent if (io.papermc.paper.event.packet.PlayerChunkLoadEvent.getHandlerList().getRegisteredListeners().length > 0) { diff --git a/net/minecraft/world/level/chunk/LevelChunkSection.java b/net/minecraft/world/level/chunk/LevelChunkSection.java -index b8ac6a9ba7b56ccd034757f7d135d272b8e69e90..e307e618775acb2052593e16d6ff2a5a9edbac4a 100644 +index b8ac6a9ba7b56ccd034757f7d135d272b8e69e90..6354a2cbcfe2b940e3c3a80b12b24c7f0f52c202 100644 --- a/net/minecraft/world/level/chunk/LevelChunkSection.java +++ b/net/minecraft/world/level/chunk/LevelChunkSection.java @@ -18,7 +18,7 @@ public class LevelChunkSection implements ca.spottedleaf.moonrise.patches.block_ @@ -166,7 +166,7 @@ index b8ac6a9ba7b56ccd034757f7d135d272b8e69e90..e307e618775acb2052593e16d6ff2a5a public static final int SECTION_SIZE = 4096; public static final int BIOME_CONTAINER_BITS = 2; - short nonEmptyBlockCount; // Paper - package private -+ volatile short nonEmptyBlockCount; // Paper - package private // Leaf - volatile ++ volatile short nonEmptyBlockCount; // Paper - package private // Leaf - Async chunk send - volatile private short tickingBlockCount; private short tickingFluidCount; private boolean isRandomlyTickingBlocksStatus; // Leaf - Cache random tick block status diff --git a/leaf-server/src/main/java/org/dreeam/leaf/util/fastBitRadixSort.java b/leaf-server/src/main/java/org/dreeam/leaf/util/FastBitRadixSort.java similarity index 98% rename from leaf-server/src/main/java/org/dreeam/leaf/util/fastBitRadixSort.java rename to leaf-server/src/main/java/org/dreeam/leaf/util/FastBitRadixSort.java index ba9cf22c..598ed57a 100644 --- a/leaf-server/src/main/java/org/dreeam/leaf/util/fastBitRadixSort.java +++ b/leaf-server/src/main/java/org/dreeam/leaf/util/FastBitRadixSort.java @@ -5,7 +5,7 @@ import net.minecraft.world.entity.Entity; import java.lang.reflect.Array; // Required for Array.newInstance import java.util.List; -public class fastBitRadixSort { +public class FastBitRadixSort { private static final int SMALL_ARRAY_THRESHOLD = 2; private Entity[] entityBuffer = new Entity[0];