From 444ce8619463ae5b988dfef0b29c8aba8707c105 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 3 Aug 2018 23:02:44 -0400 Subject: [PATCH 1/2] Entity add to world fixes - #1223 1) Chunk Registration might kill an entity, don't add it to the world if it did! 2) By default, entities are added to the world per slice iteration. This opens risk of the slices being manipulated during chunk add if an EntityAddToWorldEvent spawns an entity into this chunk. Fix this by differing entity add to world for all entities at the same time 3) If a duplicate entity is attempted to add to the world of an entity, and the original entity is dead, overwrite it as the logic does for unloaod queued entities. Should hopefully finish up issues with #1223 --- ...revent-Saving-Bad-entities-to-chunks.patch | 28 +----- .../0348-Entity-add-to-world-fixes.patch | 87 +++++++++++++++++++ 2 files changed, 88 insertions(+), 27 deletions(-) create mode 100644 Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch diff --git a/Spigot-Server-Patches/0341-Prevent-Saving-Bad-entities-to-chunks.patch b/Spigot-Server-Patches/0341-Prevent-Saving-Bad-entities-to-chunks.patch index ea270b2e5..428b636be 100644 --- a/Spigot-Server-Patches/0341-Prevent-Saving-Bad-entities-to-chunks.patch +++ b/Spigot-Server-Patches/0341-Prevent-Saving-Bad-entities-to-chunks.patch @@ -1,4 +1,4 @@ -From 186657866775b48d9c845b8066339b463c4f88ad Mon Sep 17 00:00:00 2001 +From 354cae3bd5b0d32cf8c1f8cebdff763013606fd5 Mon Sep 17 00:00:00 2001 From: Aikar Date: Thu, 26 Jul 2018 00:11:12 -0400 Subject: [PATCH] Prevent Saving Bad entities to chunks @@ -56,32 +56,6 @@ index bcce5e8b7e..bad287fca4 100644 nbttagcompound.set("Entities", nbttaglist1); NBTTagList nbttaglist2 = new NBTTagList(); -diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java -index c53e77b821..2a4405638e 100644 ---- a/src/main/java/net/minecraft/server/World.java -+++ b/src/main/java/net/minecraft/server/World.java -@@ -1204,7 +1204,7 @@ public abstract class World implements IBlockAccess { - } - - this.getChunkAt(i, j).a(entity); -- this.entityList.add(entity); -+ if (!entity.dead) this.entityList.add(entity); // Paper - don't add dead entities, chunk registration may of killed it - this.b(entity); - return true; - } -diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java -index 1244baf45a..1763d9f94d 100644 ---- a/src/main/java/net/minecraft/server/WorldServer.java -+++ b/src/main/java/net/minecraft/server/WorldServer.java -@@ -1173,7 +1173,7 @@ public class WorldServer extends World implements IAsyncTaskHandler { - if (this.entitiesByUUID.containsKey(uuid)) { - Entity entity1 = (Entity) this.entitiesByUUID.get(uuid); - -- if (this.f.contains(entity1)) { -+ if (this.f.contains(entity1) || entity1.dead) { // Paper - overwrite the current dead one - this.f.remove(entity1); - } else { - if (!(entity instanceof EntityHuman)) { -- 2.18.0 diff --git a/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch b/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch new file mode 100644 index 000000000..5a914f728 --- /dev/null +++ b/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch @@ -0,0 +1,87 @@ +From 65255110b50ca2fb51d2b95fa1cc8c5f61da88e8 Mon Sep 17 00:00:00 2001 +From: Aikar +Date: Fri, 3 Aug 2018 22:47:46 -0400 +Subject: [PATCH] Entity add to world fixes + +1) Chunk Registration might kill an entity, don't add it to the world if it did! + +2) By default, entities are added to the world per slice iteration. +This opens risk of the slices being manipulated during chunk add if an +EntityAddToWorldEvent spawns an entity into this chunk. +Fix this by differing entity add to world for all entities at the same time + +3) If a duplicate entity is attempted to add to the world of an entity, and +the original entity is dead, overwrite it as the logic does for unloaod queued entities. + +diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java +index f41f282848..c6f47d4089 100644 +--- a/src/main/java/net/minecraft/server/Chunk.java ++++ b/src/main/java/net/minecraft/server/Chunk.java +@@ -888,6 +888,7 @@ public class Chunk { + this.world.b(this.tileEntities.values()); + List[] aentityslice = this.entitySlices; // Spigot + int i = aentityslice.length; ++ List toAdd = new java.util.ArrayList<>(32); // Paper + + for (int j = 0; j < i; ++j) { + List entityslice = aentityslice[j]; // Spigot +@@ -934,10 +935,12 @@ public class Chunk { + thisChunk.put(entity.uniqueID, entity); + } + } +- // Paper end + +- this.world.a((Collection) entityslice); ++ //this.world.a((Collection) entityslice); // Move down, add all entities at same time ++ toAdd.addAll(entityslice); ++ // Paper end + } ++ this.world.addChunkEntities(toAdd); // Paper - add all at same time to avoid entities adding to world modifying slice state + + } + +diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java +index 2ad7c75d2b..c04a9d5a09 100644 +--- a/src/main/java/net/minecraft/server/World.java ++++ b/src/main/java/net/minecraft/server/World.java +@@ -1204,6 +1204,7 @@ public abstract class World implements IBlockAccess { + } + + this.getChunkAt(i, j).a(entity); ++ if (entity.dead) return false; // Paper - don't add dead entities, chunk registration may of killed it + this.entityList.add(entity); + this.b(entity); + return true; +@@ -2743,6 +2744,7 @@ public abstract class World implements IBlockAccess { + return i; + } + ++ public void addChunkEntities(Collection collection) { a(collection); } // Paper - OBFHELPER + public void a(Collection collection) { + org.spigotmc.AsyncCatcher.catchOp( "entity world add"); // Spigot + // CraftBukkit start +@@ -2752,7 +2754,7 @@ public abstract class World implements IBlockAccess { + while (iterator.hasNext()) { + Entity entity = (Entity) iterator.next(); + +- if (entity == null) { ++ if (entity == null || entity.dead || entity.valid) { // Paper - prevent adding already added or dead entities + continue; + } + this.entityList.add(entity); +diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java +index 1244baf45a..a14b5e0618 100644 +--- a/src/main/java/net/minecraft/server/WorldServer.java ++++ b/src/main/java/net/minecraft/server/WorldServer.java +@@ -1173,7 +1173,7 @@ public class WorldServer extends World implements IAsyncTaskHandler { + if (this.entitiesByUUID.containsKey(uuid)) { + Entity entity1 = (Entity) this.entitiesByUUID.get(uuid); + +- if (this.f.contains(entity1)) { ++ if (this.f.contains(entity1) || entity1.dead) { // Paper - if dupe is dead, overwrite + this.f.remove(entity1); + } else { + if (!(entity instanceof EntityHuman)) { +-- +2.18.0 + From aea67db7a0db769f96e2aff1ea9dab9497ac843a Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 4 Aug 2018 00:23:00 -0400 Subject: [PATCH 2/2] make dupe uuid saferegen delete range configurable also ensure we never process already valid entities. this shouldnt be possible as of recent commits as we made the entity slice array safer, but doesn't hurt for this logic to be safe too incase that patch got dropped in a future version by accident/necessarily --- .../0338-Duplicate-UUID-Resolve-Option.patch | 18 ++++++++++-------- ...Add-some-Debug-to-Chunk-Entity-slices.patch | 4 ++-- .../0348-Entity-add-to-world-fixes.patch | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Spigot-Server-Patches/0338-Duplicate-UUID-Resolve-Option.patch b/Spigot-Server-Patches/0338-Duplicate-UUID-Resolve-Option.patch index e4bfe005a..acc5a4413 100644 --- a/Spigot-Server-Patches/0338-Duplicate-UUID-Resolve-Option.patch +++ b/Spigot-Server-Patches/0338-Duplicate-UUID-Resolve-Option.patch @@ -1,4 +1,4 @@ -From af75c184b955299758aaf93c63af5910425309ae Mon Sep 17 00:00:00 2001 +From b40bcfb513f86c14a0fa7cd15281fa6fff4f7aed Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 21 Jul 2018 14:27:34 -0400 Subject: [PATCH] Duplicate UUID Resolve Option @@ -33,10 +33,10 @@ But for those who are ok with leaving this inconsistent behavior, you may use WA 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 14c8edeffc..b373bba864 100644 +index 14c8edeffc..c3bd82692d 100644 --- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java +++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java -@@ -541,4 +541,45 @@ public class PaperWorldConfig { +@@ -541,4 +541,47 @@ public class PaperWorldConfig { log("Bed Search Radius: " + bedSearchRadius); } } @@ -45,13 +45,15 @@ index 14c8edeffc..b373bba864 100644 + SAFE_REGEN, 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 "saferegen": + case "saferegenerate": + duplicateUUIDMode = DuplicateUUIDMode.SAFE_REGEN; -+ log("Duplicate UUID Resolve: Safer Regenerate New UUID (Delete likely duplicates)"); ++ log("Duplicate UUID Resolve: Safer Regenerate New UUID (Delete likely duplicates within " + duplicateUUIDDeleteRange + " blocks)"); + break; + case "regen": + case "regenerate": @@ -83,7 +85,7 @@ index 14c8edeffc..b373bba864 100644 + } } diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java -index 4757081090..8cd2ed85bc 100644 +index 4757081090..3d5b6eabaf 100644 --- a/src/main/java/net/minecraft/server/Chunk.java +++ b/src/main/java/net/minecraft/server/Chunk.java @@ -1,5 +1,10 @@ @@ -123,7 +125,7 @@ index 4757081090..8cd2ed85bc 100644 + Map thisChunk = new HashMap<>(); + for (Iterator iterator = ((List) entityslice).iterator(); iterator.hasNext(); ) { + Entity entity = iterator.next(); -+ if (entity.dead) continue; ++ if (entity.dead || entity.valid) continue; + Entity other = ((WorldServer) world).entitiesByUUID.get(entity.uniqueID); + if (other == null || other.dead || world.getEntityUnloadQueue().contains(other)) { + other = thisChunk.get(entity.uniqueID); @@ -132,7 +134,7 @@ index 4757081090..8cd2ed85bc 100644 + if (mode == DuplicateUUIDMode.SAFE_REGEN && other != null && !other.dead && + !world.getEntityUnloadQueue().contains(other) + && java.util.Objects.equals(other.getSaveID(), entity.getSaveID()) -+ && entity.getBukkitEntity().getLocation().distance(other.getBukkitEntity().getLocation()) < 24 ++ && entity.getBukkitEntity().getLocation().distance(other.getBukkitEntity().getLocation()) < world.paperConfig.duplicateUUIDDeleteRange + ) { + 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.die(); @@ -177,7 +179,7 @@ index 7b856cad91..eb8904a728 100644 this.uniqueID = uuid; this.ar = this.uniqueID.toString(); diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java -index a01488e985..3012768cb9 100644 +index 5de8da65c0..c53e77b821 100644 --- a/src/main/java/net/minecraft/server/World.java +++ b/src/main/java/net/minecraft/server/World.java @@ -70,7 +70,7 @@ public abstract class World implements IBlockAccess { diff --git a/Spigot-Server-Patches/0340-Add-some-Debug-to-Chunk-Entity-slices.patch b/Spigot-Server-Patches/0340-Add-some-Debug-to-Chunk-Entity-slices.patch index 1f573d9ca..22c065571 100644 --- a/Spigot-Server-Patches/0340-Add-some-Debug-to-Chunk-Entity-slices.patch +++ b/Spigot-Server-Patches/0340-Add-some-Debug-to-Chunk-Entity-slices.patch @@ -1,4 +1,4 @@ -From 099d9012a80cc417b8b3be962d67942ce3162b0c Mon Sep 17 00:00:00 2001 +From e4d1385d484c0a847d796803128a541b22246000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Mon, 23 Jul 2018 22:44:23 -0400 Subject: [PATCH] Add some Debug to Chunk Entity slices @@ -9,7 +9,7 @@ This should hopefully avoid duplicate entities ever being created if the entity was to end up in 2 different chunk slices diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java -index ccb30d5bfd..94b294e87b 100644 +index 195cde784b..575ddcb2a0 100644 --- a/src/main/java/net/minecraft/server/Chunk.java +++ b/src/main/java/net/minecraft/server/Chunk.java @@ -681,8 +681,33 @@ public class Chunk { diff --git a/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch b/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch index 5a914f728..eba70c38a 100644 --- a/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch +++ b/Spigot-Server-Patches/0348-Entity-add-to-world-fixes.patch @@ -1,4 +1,4 @@ -From 65255110b50ca2fb51d2b95fa1cc8c5f61da88e8 Mon Sep 17 00:00:00 2001 +From 2a5ab03010c297502ce4c62084fbd321cd567634 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 3 Aug 2018 22:47:46 -0400 Subject: [PATCH] Entity add to world fixes @@ -14,7 +14,7 @@ Fix this by differing entity add to world for all entities at the same time the original entity is dead, overwrite it as the logic does for unloaod queued entities. diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java -index f41f282848..c6f47d4089 100644 +index 575ddcb2a0..3d512d7595 100644 --- a/src/main/java/net/minecraft/server/Chunk.java +++ b/src/main/java/net/minecraft/server/Chunk.java @@ -888,6 +888,7 @@ public class Chunk {