From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Fri, 19 Feb 2021 18:45:25 -0800
Subject: [PATCH] Remove chunk lookup & lambda allocation from counting mobs

The chunk lookup doesn't look cheap.

diff --git a/src/main/java/net/minecraft/server/level/ChunkProviderServer.java b/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
index 4934d0872af5c9cf6be640bad1f46298ad984337..9e96376a1d710c0ba7a763868b23fc586253e1a8 100644
--- a/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/level/ChunkProviderServer.java
@@ -1016,9 +1016,9 @@ public class ChunkProviderServer extends IChunkProvider {
                 for (EntityPlayer player : this.world.players) {
                     Arrays.fill(player.mobCounts, 0);
                 }
-                spawnercreature_d = SpawnerCreature.countMobs(l, this.world.A(), this::a, true);
+                spawnercreature_d = SpawnerCreature.countMobs(l, this.world.A(), this::a, true, this); // Tuinity
             } else {
-                spawnercreature_d = SpawnerCreature.countMobs(l, this.world.A(), this::a, false);
+                spawnercreature_d = SpawnerCreature.countMobs(l, this.world.A(), this::a, false, this); // Tuinity
             }
             // Paper end
             this.world.timings.countNaturalMobs.stopTiming(); // Paper - timings
diff --git a/src/main/java/net/minecraft/world/level/SpawnerCreature.java b/src/main/java/net/minecraft/world/level/SpawnerCreature.java
index b3c73c6b4cffa20813355c5f2132fe3eaa134007..d497006f05f79015cd791849888832bb53f4a414 100644
--- a/src/main/java/net/minecraft/world/level/SpawnerCreature.java
+++ b/src/main/java/net/minecraft/world/level/SpawnerCreature.java
@@ -64,9 +64,9 @@ public final class SpawnerCreature {
 
     public static SpawnerCreature.d a(int i, Iterable<Entity> iterable, SpawnerCreature.b spawnercreature_b) {
         // Paper start - add countMobs parameter
-        return countMobs(i, iterable, spawnercreature_b, false);
+        return countMobs(i, iterable, spawnercreature_b, false, null); // Tuinity - it'll still be broken no matter what
     }
-    public static SpawnerCreature.d countMobs(int i, Iterable<Entity> iterable, SpawnerCreature.b spawnercreature_b, boolean countMobs) {
+    public static SpawnerCreature.d countMobs(int i, Iterable<Entity> iterable, SpawnerCreature.b spawnercreature_b, boolean countMobs, net.minecraft.server.level.ChunkProviderServer chunkProvider) { // Tuinity - add CPS param
         // Paper end - add countMobs parameter
         SpawnerCreatureProbabilities spawnercreatureprobabilities = new SpawnerCreatureProbabilities();
         Object2IntOpenHashMap<EnumCreatureType> object2intopenhashmap = new Object2IntOpenHashMap();
@@ -97,7 +97,16 @@ public final class SpawnerCreature {
                 BlockPosition blockposition = entity.getChunkCoordinates();
                 long j = ChunkCoordIntPair.pair(blockposition.getX() >> 4, blockposition.getZ() >> 4);
 
-                spawnercreature_b.query(j, (chunk) -> {
+                // Tuinity start - remove chunk lookup and lambda
+                Chunk chunk = entity.getCurrentChunk();
+                if (chunk == null || j != chunk.coordinateKey) { // no chunk or coordinate doesn't match
+                    chunk = chunkProvider.getChunkAtIfLoadedMainThreadNoCache(blockposition.getX() >> 4, blockposition.getZ() >> 4);
+                    if (chunk == null) {
+                        // unloaded chunk! no longer FULL.
+                        continue;
+                    }
+                }
+                // Tuinity end - remove chunk lookup and lambda
                     BiomeSettingsMobs.b biomesettingsmobs_b = b(blockposition, chunk).b().a(entity.getEntityType());
 
                     if (biomesettingsmobs_b != null) {
@@ -107,10 +116,10 @@ public final class SpawnerCreature {
                     object2intopenhashmap.addTo(enumcreaturetype, 1);
                     // Paper start
                     if (countMobs) {
-                        ((WorldServer)chunk.world).getChunkProvider().playerChunkMap.updatePlayerMobTypeMap(entity);
+                        chunkProvider.playerChunkMap.updatePlayerMobTypeMap(entity); // Tuinity - directly use chunk provider
                     }
                     // Paper end
-                });
+                // Tuinity - remove chunk lookup and lambda
             }
         }
 
