From d59b727a40b51715259dc579cdb16155df502c87 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 3 Aug 2018 23:02:44 -0400 Subject: [PATCH] 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 --- .../Entity-add-to-world-fixes.patch | 85 +++++++++++++++++++ ...revent-Saving-Bad-entities-to-chunks.patch | 26 ------ 2 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 Spigot-Server-Patches/Entity-add-to-world-fixes.patch diff --git a/Spigot-Server-Patches/Entity-add-to-world-fixes.patch b/Spigot-Server-Patches/Entity-add-to-world-fixes.patch new file mode 100644 index 0000000000..b8f44d21c7 --- /dev/null +++ b/Spigot-Server-Patches/Entity-add-to-world-fixes.patch @@ -0,0 +1,85 @@ +From 0000000000000000000000000000000000000000 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 +@@ -0,0 +0,0 @@ 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 +@@ -0,0 +0,0 @@ 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 +@@ -0,0 +0,0 @@ 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; +@@ -0,0 +0,0 @@ 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 +@@ -0,0 +0,0 @@ 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 +@@ -0,0 +0,0 @@ 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)) { +-- \ No newline at end of file diff --git a/Spigot-Server-Patches/Prevent-Saving-Bad-entities-to-chunks.patch b/Spigot-Server-Patches/Prevent-Saving-Bad-entities-to-chunks.patch index c2a9c259f7..7f87818d7b 100644 --- a/Spigot-Server-Patches/Prevent-Saving-Bad-entities-to-chunks.patch +++ b/Spigot-Server-Patches/Prevent-Saving-Bad-entities-to-chunks.patch @@ -56,30 +56,4 @@ 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 -@@ -0,0 +0,0 @@ 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 -@@ -0,0 +0,0 @@ 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)) { -- \ No newline at end of file