From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 21 Jul 2018 08:25:40 -0400
Subject: [PATCH] Add Debug Entities option to debug dupe uuid issues

Add -Ddebug.entities=true to your JVM flags to gain more information

diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index fe730cd301e6bae46320355ee8cb67d406a72a9f..5fd57a3d30972d86c62025d63cd57b56a147cc94 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -76,6 +76,8 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
     public com.destroystokyo.paper.loottable.PaperLootableInventoryData lootableData; // Paper
     private CraftEntity bukkitEntity;
 
+    PlayerChunkMap.EntityTracker tracker; // Paper
+    Throwable addedToWorldStack; // Paper - entity debug
     public CraftEntity getBukkitEntity() {
         if (bukkitEntity == null) {
             bukkitEntity = CraftEntity.getEntity(world.getServer(), this);
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index 7804cc0f6a02d14f4adbe05b37e8470a382c0d26..4ee26ff08f7a058648ab54f0dcd81b466a9aced1 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -1064,6 +1064,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
                 } else {
                     PlayerChunkMap.EntityTracker playerchunkmap_entitytracker = new PlayerChunkMap.EntityTracker(entity, i, j, entitytypes.isDeltaTracking());
 
+                    entity.tracker = playerchunkmap_entitytracker; // Paper - Fast access to tracker
                     this.trackedEntities.put(entity.getId(), playerchunkmap_entitytracker);
                     playerchunkmap_entitytracker.track(this.world.getPlayers());
                     if (entity instanceof EntityPlayer) {
@@ -1106,7 +1107,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
         if (playerchunkmap_entitytracker1 != null) {
             playerchunkmap_entitytracker1.a();
         }
-
+        entity.tracker = null; // Paper - We're no longer tracked
     }
 
     protected void g() {
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index 2f57c7bc76ddd8fd4b796d85eaa7200d8da41865..a1c33c525ce8ae0f4736198658174f8d67ab6320 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -63,6 +63,7 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
     public boolean pvpMode;
     public boolean keepSpawnInMemory = true;
     public org.bukkit.generator.ChunkGenerator generator;
+    public static final boolean DEBUG_ENTITIES = Boolean.getBoolean("debug.entities"); // Paper
 
     public boolean captureBlockStates = false;
     public boolean captureTreeGeneration = false;
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index 1019f180397abef5ec28d00c432415a7270026e2..5086a854b84b05e5b9c0f5677051d19dfa42f343 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -76,6 +76,9 @@ public class WorldServer extends World {
     // CraftBukkit start
     private int tickPosition;
     boolean hasPhysicsEvent = true; // Paper
+    private static Throwable getAddToWorldStackTrace(Entity entity) {
+        return new Throwable(entity + " Added to world at " + new java.util.Date());
+    }
 
     // Add env and gen to constructor
     public WorldServer(MinecraftServer minecraftserver, Executor executor, WorldNBTStorage worldnbtstorage, WorldData worlddata, DimensionManager dimensionmanager, GameProfilerFiller gameprofilerfiller, WorldLoadListener worldloadlistener, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen) {
@@ -997,8 +1000,28 @@ public class WorldServer extends World {
     // CraftBukkit start
     private boolean addEntity0(Entity entity, CreatureSpawnEvent.SpawnReason spawnReason) {
         org.spigotmc.AsyncCatcher.catchOp("entity add"); // Spigot
-        if (entity.valid) { MinecraftServer.LOGGER.error("Attempted Double World add on " + entity, new Throwable()); return true; } // Paper
+        // Paper start
+        if (entity.valid) {
+            MinecraftServer.LOGGER.error("Attempted Double World add on " + entity, new Throwable());
+
+            if (DEBUG_ENTITIES) {
+                Throwable thr = entity.addedToWorldStack;
+                if (thr == null) {
+                    MinecraftServer.LOGGER.error("Double add entity has no add stacktrace");
+                } else {
+                    MinecraftServer.LOGGER.error("Double add stacktrace: ", thr);
+                }
+            }
+            return true;
+        }
+        // Paper end
         if (entity.dead) {
+            // Paper start
+            if (DEBUG_ENTITIES) {
+                new Throwable("Tried to add entity " + entity + " but it was marked as removed already").printStackTrace(); // CraftBukkit
+                getAddToWorldStackTrace(entity).printStackTrace();
+            }
+            // Paper end
             // WorldServer.LOGGER.warn("Tried to add entity {} but it was marked as removed already", EntityTypes.getName(entity.getEntityType())); // CraftBukkit
             return false;
         } else if (this.isUUIDTaken(entity)) {
@@ -1158,7 +1181,24 @@ public class WorldServer extends World {
                 }
             }
 
-            this.entitiesByUUID.put(entity.getUniqueID(), entity);
+            if (DEBUG_ENTITIES) {
+                entity.addedToWorldStack = getAddToWorldStackTrace(entity);
+            }
+
+            Entity old = this.entitiesByUUID.put(entity.getUniqueID(), entity);
+            if (old != null && old.getId() != entity.getId() && old.valid) {
+                Logger logger = LogManager.getLogger();
+                logger.error("Overwrote an existing entity " + old + " with " + entity);
+                if (DEBUG_ENTITIES) {
+                    if (old.addedToWorldStack != null) {
+                        old.addedToWorldStack.printStackTrace();
+                    } else {
+                        logger.error("Oddly, the old entity was not added to the world in the normal way. Plugins?");
+                    }
+                    entity.addedToWorldStack.printStackTrace();
+                }
+            }
+
             this.getChunkProvider().addEntity(entity);
             // CraftBukkit start - SPIGOT-5278
             if (entity instanceof EntityDrowned) {
