Paper/Spigot-Server-Patches/0301-Add-some-Debug-to-Chunk-Entity-slices.patch
Aikar 9dc4d6448b
Numerous fixes to entity related changes
While it wasn't really "broken" before, if plugins use NMS
(which they really should't be) and mess with entity management
themselves, and get it wrong, they could ultimately corrupt our
state expectations.

I've been unable to reproduce any issues locally, but these changes
are the result of me analyzing the code pretty deeply and seeing
about how to make it more durable to abnormal usage.

Any servers seeing oddities, please run with -Ddebug.entities=true
and send me any logs triggered.
2019-04-05 23:08:45 -04:00

74 lines
3.2 KiB
Diff

From 100f05b6d3e0b894381d9f32dbadabdc77a97d9d 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 5d187e5d7d..01abe5e376 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -709,6 +709,25 @@ public class Chunk implements IChunkAccess {
if (k >= this.entitySlices.length) {
k = this.entitySlices.length - 1;
}
+ // Paper - remove from any old list if its in one
+ List<Entity> nextSlice = this.entitySlices[k]; // the next list to be added to
+ List<Entity> currentSlice = entity.entitySlice;
+ if (nextSlice == currentSlice) {
+ if (World.DEBUG_ENTITIES) MinecraftServer.LOGGER.warn("Entity was already in this chunk!" + entity, new Throwable());
+ return; // ??? silly plugins
+ }
+ if (currentSlice != null && currentSlice.contains(entity)) {
+ // Still in an old chunk...
+ if (World.DEBUG_ENTITIES) MinecraftServer.LOGGER.warn("Entity is still in another chunk!" + entity, new Throwable());
+ Chunk chunk = entity.getCurrentChunk();
+ if (chunk != null) {
+ chunk.removeEntity(entity);
+ } else {
+ removeEntity(entity);
+ }
+ currentSlice.remove(entity); // Just incase the above did not remove from the previous slice
+ }
+ // Paper end
if (!entity.inChunk || entity.getCurrentChunk() != this) entityCounts.increment(entity.getMinecraftKeyString()); // Paper
entity.inChunk = true;
@@ -718,6 +737,7 @@ public class Chunk implements IChunkAccess {
entity.chunkZ = this.locZ;
this.entitySlices[k].add(entity);
// Paper start
+ entity.entitySlice = this.entitySlices[k]; // Paper
this.markDirty();
if (entity instanceof EntityItem) {
itemCounts[k]++;
@@ -746,6 +766,9 @@ public class Chunk implements IChunkAccess {
}
// Paper start
if (entity.currentChunk != null && entity.currentChunk.get() == this) entity.setCurrentChunk(null);
+ if (entitySlices[i] == entity.entitySlice) {
+ entity.entitySlice = null;
+ }
if (!this.entitySlices[i].remove(entity)) {
return;
}
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
index 539273afbc..ead5af991c 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
@@ -62,6 +62,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
}
}
};
+ List<Entity> entitySlice = null;
// Paper end
static boolean isLevelAtLeast(NBTTagCompound tag, int level) {
return tag.hasKey("Bukkit.updateLevel") && tag.getInt("Bukkit.updateLevel") >= level;
--
2.21.0