From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 21 Jul 2018 14:27:34 -0400
Subject: [PATCH] Duplicate UUID Resolve Option

Due to a bug in https://github.com/PaperMC/Paper/commit/2e29af3df05ec0a383f48be549d1c03200756d24
which was added all the way back in March of 2016, it was unknown (potentially not at the time)
that an entity might actually change the seed of the random object.

At some point, EntitySquid did start setting the seed. Due to this shared random, this caused
every entity to use a Random object with a predictable seed.

This has caused entities to potentially generate with the same UUID....

Over the years, servers have had entities disappear, but no sign of trouble
because CraftBukkit removed the log lines indicating that something was wrong.

We have fixed the root issue causing duplicate UUID's, however we now have chunk
files full of entities that have the same UUID as another entity!

When these chunks load, the 2nd entity will not be added to the world correctly.

If that chunk loads in a different order in the future, then it will reverse and the
missing one is now the one added to the world and not the other. This results in very
inconsistent entity behavior.

This change allows you to recover any duplicate entity by generating a new UUID for it.
This also lets you delete them instead if you don't want to risk having new entities added to
the world that you previously did not see.

But for those who are ok with leaving this inconsistent behavior, you may use WARN or NOTHING options.

It is recommended you regenerate the entities, as these were legit entities, and deserve your love.

diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
index e55647f5fb58aeca93bbb70fa8d06c1e356fe633..0b99f20ec760692a350acd4f78dd060e30d88b50 100644
--- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
@@ -443,6 +443,45 @@ public class PaperWorldConfig {
         preventMovingIntoUnloadedChunks = getBoolean("prevent-moving-into-unloaded-chunks", false);
     }
 
+    public enum DuplicateUUIDMode {
+        SAFE_REGEN, DELETE, NOTHING, WARN
+    }
+    public DuplicateUUIDMode duplicateUUIDMode = DuplicateUUIDMode.SAFE_REGEN;
+    public int duplicateUUIDDeleteRange = 32;
+    private void repairDuplicateUUID() {
+        String desiredMode = getString("duplicate-uuid-resolver", "saferegen").toLowerCase().trim();
+        duplicateUUIDDeleteRange = getInt("duplicate-uuid-saferegen-delete-range", duplicateUUIDDeleteRange);
+        switch (desiredMode.toLowerCase()) {
+            case "regen":
+            case "regenerate":
+            case "saferegen":
+            case "saferegenerate":
+                duplicateUUIDMode = DuplicateUUIDMode.SAFE_REGEN;
+                log("Duplicate UUID Resolve: Regenerate New UUID if distant (Delete likely duplicates within " + duplicateUUIDDeleteRange + " blocks)");
+                break;
+            case "remove":
+            case "delete":
+                duplicateUUIDMode = DuplicateUUIDMode.DELETE;
+                log("Duplicate UUID Resolve: Delete Entity");
+                break;
+            case "silent":
+            case "nothing":
+                duplicateUUIDMode = DuplicateUUIDMode.NOTHING;
+                logError("Duplicate UUID Resolve: Do Nothing (no logs) - Warning, may lose indication of bad things happening");
+                break;
+            case "log":
+            case "warn":
+                duplicateUUIDMode = DuplicateUUIDMode.WARN;
+                log("Duplicate UUID Resolve: Warn (do nothing but log it happened, may be spammy)");
+                break;
+            default:
+                duplicateUUIDMode = DuplicateUUIDMode.WARN;
+                logError("Warning: Invalid duplicate-uuid-resolver config " + desiredMode + " - must be one of: regen, delete, nothing, warn");
+                log("Duplicate UUID Resolve: Warn (do nothing but log it happened, may be spammy)");
+                break;
+        }
+    }
+
     public boolean countAllMobsForSpawning = false;
     private void countAllMobsForSpawning() {
         countAllMobsForSpawning = getBoolean("count-all-mobs-for-spawning", false);
diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index 9f2c9a1e32e1bfbeeda8e8f733f04d7f5347bbe9..f0c4e98a6d5c448850a72ba90fb68c512fff8310 100644
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -1,6 +1,7 @@
 package net.minecraft.server.level;
 
 import co.aikar.timings.Timing; // Paper
+import com.destroystokyo.paper.PaperWorldConfig; // Paper
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableList.Builder;
 import com.google.common.collect.Iterables;
@@ -29,13 +30,17 @@ import java.io.Writer;
 import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.BitSet;
+import java.util.HashMap; // Paper
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map; // Paper
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Queue;
 import java.util.Set;
 import java.util.concurrent.CancellationException;
+import java.util.UUID; // Paper
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.CompletionStage;
@@ -856,6 +861,7 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
                     entity.discard();
                     needsRemoval = true;
                 }
+                checkDupeUUID(world, entity); // Paper
                 return !needsRemoval;
             }));
             // CraftBukkit end
@@ -906,6 +912,43 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
         });
     }
 
+    // Paper start
+    private static void checkDupeUUID(ServerLevel level, Entity entity) {
+        PaperWorldConfig.DuplicateUUIDMode mode = level.paperConfig.duplicateUUIDMode;
+        if (mode != PaperWorldConfig.DuplicateUUIDMode.WARN
+            && mode != PaperWorldConfig.DuplicateUUIDMode.DELETE
+            && mode != PaperWorldConfig.DuplicateUUIDMode.SAFE_REGEN) {
+            return;
+        }
+        Entity other = level.getEntity(entity.getUUID());
+
+        if (mode == PaperWorldConfig.DuplicateUUIDMode.SAFE_REGEN && other != null && !other.isRemoved()
+            && Objects.equals(other.getEncodeId(), entity.getEncodeId())
+            && entity.getBukkitEntity().getLocation().distance(other.getBukkitEntity().getLocation()) < level.paperConfig.duplicateUUIDDeleteRange
+        ) {
+            if (ServerLevel.DEBUG_ENTITIES) LOGGER.warn("[DUPE-UUID] Duplicate UUID found used by " + other + ", deleted entity " + entity + " because it was near the duplicate and likely an actual duplicate. See https://github.com/PaperMC/Paper/issues/1223 for discussion on what this is about.");
+            entity.discard();
+            return;
+        }
+        if (other != null && !other.isRemoved()) {
+            switch (mode) {
+                case SAFE_REGEN: {
+                    entity.setUUID(UUID.randomUUID());
+                    if (ServerLevel.DEBUG_ENTITIES) LOGGER.warn("[DUPE-UUID] Duplicate UUID found used by " + other + ", regenerated UUID for " + entity + ". See https://github.com/PaperMC/Paper/issues/1223 for discussion on what this is about.");
+                    break;
+                }
+                case DELETE: {
+                    if (ServerLevel.DEBUG_ENTITIES) LOGGER.warn("[DUPE-UUID] Duplicate UUID found used by " + other + ", deleted entity " + entity + ". See https://github.com/PaperMC/Paper/issues/1223 for discussion on what this is about.");
+                    entity.discard();
+                    break;
+                }
+                default:
+                    if (ServerLevel.DEBUG_ENTITIES) LOGGER.warn("[DUPE-UUID] Duplicate UUID found used by " + other + ", doing nothing to " + entity + ". See https://github.com/PaperMC/Paper/issues/1223 for discussion on what this is about.");
+                    break;
+            }
+        }
+    }
+    // Paper end
     public CompletableFuture<Either<LevelChunk, ChunkHolder.ChunkLoadingFailure>> prepareTickingChunk(ChunkHolder holder) {
         ChunkPos chunkcoordintpair = holder.getPos();
         CompletableFuture<Either<List<ChunkAccess>, ChunkHolder.ChunkLoadingFailure>> completablefuture = this.getChunkRangeFuture(chunkcoordintpair, 1, (i) -> {
diff --git a/src/main/java/net/minecraft/world/level/entity/PersistentEntitySectionManager.java b/src/main/java/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
index 0177a0323af651adace928b22f9461326ec07bc0..e19f5b2c8f485d596a64d5d96e75fa1f4a8255b5 100644
--- a/src/main/java/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
+++ b/src/main/java/net/minecraft/world/level/entity/PersistentEntitySectionManager.java
@@ -78,7 +78,22 @@ public class PersistentEntitySectionManager<T extends EntityAccess> implements A
 
     private boolean addEntityUuid(T entity) {
         if (!this.knownUuids.add(entity.getUUID())) {
+            // Paper start
+            T conflict = this.visibleEntityStorage.getEntity(entity.getUUID());
+            if (conflict != null && ((Entity) conflict).isRemoved()) {
+                stopTracking(conflict); // remove the existing entity
+                return true;
+            }
+            // Paper end
             PersistentEntitySectionManager.LOGGER.warn("UUID of added entity already exists: {}", entity);
+            // Paper start
+            if (net.minecraft.world.level.Level.DEBUG_ENTITIES && ((Entity) entity).level.paperConfig.duplicateUUIDMode != com.destroystokyo.paper.PaperWorldConfig.DuplicateUUIDMode.NOTHING) {
+                if (((Entity) entity).addedToWorldStack != null) {
+                    ((Entity) entity).addedToWorldStack.printStackTrace();
+                }
+                net.minecraft.server.level.ServerLevel.getAddToWorldStackTrace((net.minecraft.world.entity.Entity) entity).printStackTrace();
+            }
+            // Paper end
             return false;
         } else {
             return true;
