aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0347-Entity-add-to-world-fixes.patch
blob: 506a7aae75340b05f5210c03b4fde30f75b173b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
From 28915916c310632997ed1146e1f86bc7d89ac53e Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
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 dfa4a4bf..523d10e8 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -889,6 +889,7 @@ public class Chunk {
         this.world.b(this.tileEntities.values());
         List[] aentityslice = this.entitySlices; // Spigot
         int i = aentityslice.length;
+        List<Entity> toAdd = new java.util.ArrayList<>(32); // Paper
 
         for (int j = 0; j < i; ++j) {
             List entityslice = aentityslice[j]; // Spigot
@@ -936,10 +937,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 4a16f7ac..04d0fa1d 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<Entity> collection) { a(collection); } // Paper - OBFHELPER
     public void a(Collection<Entity> 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 1244baf4..a14b5e06 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.19.1