From 41085fc8b9bc2271fd39dd02c017270d7c5deaad Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Mon, 11 Nov 2024 12:17:36 -0700
Subject: [PATCH] Fix and optimize getChunkCount (#11610)

It was returning ticking chunk count instead of the intended full chunk count.
We can also directly use the size of the fullChunks collection instead of iterating all chunks.
---
 patches/server/MC-Utils.patch                          |  3 +++
 patches/server/Moonrise-optimisation-patches.patch     |  2 +-
 .../server/Provide-E-TE-Chunk-count-stat-methods.patch | 10 +---------
 3 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/patches/server/MC-Utils.patch b/patches/server/MC-Utils.patch
index 196d614376..282629407f 100644
--- a/patches/server/MC-Utils.patch
+++ b/patches/server/MC-Utils.patch
@@ -5353,6 +5353,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      private NaturalSpawner.SpawnState lastSpawnState;
 +    // Paper start
 +    private final ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<net.minecraft.world.level.chunk.LevelChunk> fullChunks = new ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<>();
++    public int getFullChunksCount() {
++        return this.fullChunks.size();
++    }
 +    long chunkFutureAwaitCounter;
 +    // Paper end
  
diff --git a/patches/server/Moonrise-optimisation-patches.patch b/patches/server/Moonrise-optimisation-patches.patch
index 182d3fdd96..9b3d8bc43b 100644
--- a/patches/server/Moonrise-optimisation-patches.patch
+++ b/patches/server/Moonrise-optimisation-patches.patch
@@ -26281,7 +26281,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      private static final Logger LOGGER = LogUtils.getLogger();
      private final DistanceManager distanceManager;
 @@ -0,0 +0,0 @@ public class ServerChunkCache extends ChunkSource {
-     private final ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<net.minecraft.world.level.chunk.LevelChunk> fullChunks = new ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<>();
+     }
      long chunkFutureAwaitCounter;
      // Paper end
 +    // Paper start - rewrite chunk system
diff --git a/patches/server/Provide-E-TE-Chunk-count-stat-methods.patch b/patches/server/Provide-E-TE-Chunk-count-stat-methods.patch
index f55b19b241..b9cd87406a 100644
--- a/patches/server/Provide-E-TE-Chunk-count-stat-methods.patch
+++ b/patches/server/Provide-E-TE-Chunk-count-stat-methods.patch
@@ -60,15 +60,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +
 +    @Override
 +    public int getChunkCount() {
-+        int ret = 0;
-+
-+        for (ChunkHolder chunkHolder : ca.spottedleaf.moonrise.common.util.ChunkSystem.getVisibleChunkHolders(this.world)) {
-+            if (chunkHolder.getTickingChunk() != null) {
-+                ++ret;
-+            }
-+        }
-+
-+        return ret;
++        return this.world.getChunkSource().getFullChunksCount();
 +    }
 +
 +    @Override