aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0340-Add-some-Debug-to-Chunk-Entity-slices.patch
blob: 271f4fb86be40746c78e494b0dfd788183a714ee (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
From cb60a9794593e7752a415d3f558e8b789054c2eb Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 23 Jul 2018 22:44:23 -0400
Subject: [PATCH] Add some Debug to Chunk Entity slices

If we detect unexpected state, log and try to recover

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 ce66bb780d..2a5d1c90b4 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -681,8 +681,34 @@ public class Chunk {
         entity.ab = this.locX;
         entity.ac = k;
         entity.ad = this.locZ;
-        this.entitySlices[k].add(entity);
+
         // Paper start
+        List<Entity> entitySlice = this.entitySlices[k];
+        boolean inThis = entitySlice.contains(entity);
+        List<Entity> currentSlice = entity.entitySlice;
+        if ((currentSlice != null && currentSlice.contains(entity)) || inThis) {
+            if (currentSlice == entitySlice || inThis) {
+                LogManager.getLogger().warn(entity + " was already in this chunk section! Report this to https://github.com/PaperMC/Paper/issues/1302");
+                new Throwable().printStackTrace();
+                return;
+            } else {
+                LogManager.getLogger().warn(entity + " is still in another ChunkSection! Report this to https://github.com/PaperMC/Paper/issues/1302");
+
+                Chunk chunk = entity.getCurrentChunk();
+                if (chunk != null) {
+                    if (chunk != this) {
+                        LogManager.getLogger().warn(entity + " was in another chunk at that! " + chunk.locX + "," + chunk.locZ);
+                    }
+                    chunk.removeEntity(entity);
+                } else {
+                    removeEntity(entity);
+                }
+                new Throwable().printStackTrace();
+            }
+        }
+        entity.entitySlice = entitySlice;
+        entitySlice.add(entity);
+
         this.markDirty();
         entity.setCurrentChunk(this);
         entityCounts.increment(entity.getMinecraftKeyString());
@@ -725,6 +751,12 @@ public class Chunk {
         }
 
         // Paper start
+        if (entity.entitySlice == null || !entity.entitySlice.contains(entity) || entitySlices[i] == entity.entitySlice) {
+            entity.entitySlice = null;
+        } else {
+            LogManager.getLogger().warn(entity + " was removed from a entitySlice we did not expect. Report this to https://github.com/PaperMC/Paper/issues/1302");
+            new Throwable().printStackTrace();
+        }
         if (!this.entitySlices[i].remove(entity)) { return; }
         this.markDirty();
         entity.setCurrentChunk(null);
@@ -955,6 +987,7 @@ public class Chunk {
                 }
                 // Spigot End
                 entity.setCurrentChunk(null); // Paper
+                entity.entitySlice = null; // Paper
 
                 // Do not pass along players, as doing so can get them stuck outside of time.
                 // (which for example disables inventory icon updates and prevents block breaking)
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index eb8904a728..86b0b84335 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -59,6 +59,7 @@ public abstract class Entity implements ICommandListener, KeyedObject { // Paper
             }
         }
     };
+    List<Entity> entitySlice = null;
     // Paper end
     static boolean isLevelAtLeast(NBTTagCompound tag, int level) {
         return tag.hasKey("Bukkit.updateLevel") && tag.getInt("Bukkit.updateLevel") >= level;
-- 
2.18.0