Affects Version/s: Minecraft 1.13, 1.15.2
Fix Version/s: 20w45a
I've been chasing down solving various entity related bugs in Paper, all which lead to the "Keeping entity with duplicate UUID" messages.
I found a major source of duplicate UUID's to be the fact that AnvilThreadedChunkStorage does not verify an entity is not dead before it saves entities to NBT.
This creates conditions where an entity is killed, and in that same tick, the chunk is saved and unloaded before it is processed in the removal queue, that entity will be saved to the chunk even though it was meant to be removed from the world.
Because the dead flag does not persist, this entity will be considered back live and well when the chunk is reloaded.
The server needs to validate that an entity is not dead before saving.
Another slightly related issue, is that an entity may of been moved, but has not been "processed" for its chunk registration, and the entity may be leaving this chunk.
This causes the entity to be saved in this chunk during an auto save even though its then moved to another chunk the next tick.
This specific issue doesn't directly cause duplicates though, but triggers the "Wrong Location!" when the entity is loaded into the chunk.
I fixed this by checking the chunkx/z of the entities location and if it doesn't match, go ahead and kick off the chunk registration process for the entity to move it out of this chunk and skip it.
For a full overview of all my changes to fix chunk entities, see: https://github.com/PaperMC/Paper/blob/22b2bf6270a1c7adf5ed5486cbdbec557ee5ca70/Spigot-Server-Patches/0341-Prevent-Saving-Bad-entities-to-chunks.patch (Licensed MIT)