From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <spottedleaf@spottedleaf.dev>
Date: Wed, 13 May 2020 09:59:12 -0700
Subject: [PATCH] Use entity ticking chunk map for entity tracker

Should bring us back in-line with tracker performance
before the loaded entity list reversion.

diff --git a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
index 2beb14192910a35936e0ff5878c227bf22abd442..db271a5a1f09f8c11ac2498f8975cf7b025dd5e3 100644
--- a/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/PlayerChunkMap.java
@@ -2153,22 +2153,25 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
     private final void processTrackQueue() {
         this.world.timings.tracker1.startTiming();
         try {
-            for (EntityTracker tracker : this.trackedEntities.values()) {
-                // update tracker entry
-                tracker.updatePlayers(tracker.tracker.getPlayersInTrackRange());
+            com.tuinity.tuinity.util.maplist.IteratorSafeOrderedReferenceSet.Iterator<Chunk> iterator = this.world.getChunkProvider().entityTickingChunks.iterator();
+            try {
+            while (iterator.hasNext()) {
+                Chunk chunk = iterator.next();
+                Entity[] entities = chunk.entities.getRawData();
+                for (int i = 0, len = chunk.entities.size(); i < len; ++i) {
+                    Entity entity = entities[i];
+                    EntityTracker tracker = this.trackedEntities.get(entity.getId());
+                    if (tracker != null) {
+                        tracker.updatePlayers(tracker.tracker.getPlayersInTrackRange());
+                        tracker.trackerEntry.tick();
+                    }
+                }
             }
-        } finally {
-            this.world.timings.tracker1.stopTiming();
-        }
-
-
-        this.world.timings.tracker2.startTiming();
-        try {
-            for (EntityTracker tracker : this.trackedEntities.values()) {
-                tracker.trackerEntry.tick();
+            } finally {
+                iterator.finishedIterating();
             }
         } finally {
-            this.world.timings.tracker2.stopTiming();
+            this.world.timings.tracker1.stopTiming();
         }
     }
     // Paper end - optimised tracker
