From ec2f1e40933512f3159338b275748fe6b329dc4f Mon Sep 17 00:00:00 2001 From: Aikar Date: Wed, 17 Oct 2018 22:47:27 -0400 Subject: [PATCH] Ensure chunk neighbor counts do not get desynced Mojang was not checking that the chunk did not overwrite, or was successfully removed. We're seeing odd reports in #1561 that indicates issues around this are having problems. --- ...-neighbor-counts-do-not-get-desynced.patch | 52 +++++++++++++++++++ .../Optimize-Chunk-Access.patch | 29 ++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Spigot-Server-Patches/Ensure-chunk-neighbor-counts-do-not-get-desynced.patch diff --git a/Spigot-Server-Patches/Ensure-chunk-neighbor-counts-do-not-get-desynced.patch b/Spigot-Server-Patches/Ensure-chunk-neighbor-counts-do-not-get-desynced.patch new file mode 100644 index 0000000000..948601a134 --- /dev/null +++ b/Spigot-Server-Patches/Ensure-chunk-neighbor-counts-do-not-get-desynced.patch @@ -0,0 +1,52 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Aikar +Date: Wed, 17 Oct 2018 22:31:54 -0400 +Subject: [PATCH] Ensure chunk neighbor counts do not get desynced + +Mojang was not checking that the chunk did not overwrite, or +was successfully removed. + +We're seeing odd reports in #1561 that indicates issues around +this are having problems. + +diff --git a/src/main/java/net/minecraft/server/ChunkMap.java b/src/main/java/net/minecraft/server/ChunkMap.java +index 39ac032b0b..1fb0770015 100644 +--- a/src/main/java/net/minecraft/server/ChunkMap.java ++++ b/src/main/java/net/minecraft/server/ChunkMap.java +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + chunk.world.timings.syncChunkLoadPostTimer.startTiming(); // Paper + lastChunkByPos = chunk; // Paper + Chunk chunk1 = (Chunk) super.put(i, chunk); ++ if (chunk1 == null) { // Paper - we should never be overwriting chunks + ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i); + + for (int j = chunkcoordintpair.x - 1; j <= chunkcoordintpair.x + 1; ++j) { +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + chunk.setNeighborLoaded(x, z); + } + } ++ // Paper start ++ } } else { ++ a.error("Overwrote existing chunk! (" + chunk.world.getWorld().getName() + ":" + chunk.locX+"," + chunk.locZ + ")", new IllegalStateException()); + } ++ // Paper end + // Paper start - if this is a spare chunk (not part of any players view distance), go ahead and queue it for unload. + if (!((WorldServer)chunk.world).getPlayerChunkMap().isChunkInUse(chunk.locX, chunk.locZ)) { + if (chunk.world.paperConfig.delayChunkUnloadsBy > 0) { +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + + public Chunk a(long i) { + Chunk chunk = (Chunk) super.remove(i); ++ if (chunk != null) { // Paper - don't decrement if we didn't remove anything + ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i); + + for (int j = chunkcoordintpair.x - 1; j <= chunkcoordintpair.x + 1; ++j) { +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + } + + // Paper start ++ } // close if (chunk != null) + if (lastChunkByPos != null && i == lastChunkByPos.chunkKey) { + lastChunkByPos = null; + } +-- \ No newline at end of file diff --git a/Spigot-Server-Patches/Optimize-Chunk-Access.patch b/Spigot-Server-Patches/Optimize-Chunk-Access.patch index 86c8715bb9..2049f6db48 100644 --- a/Spigot-Server-Patches/Optimize-Chunk-Access.patch +++ b/Spigot-Server-Patches/Optimize-Chunk-Access.patch @@ -9,7 +9,7 @@ getChunkAt is called for the same chunk multiple times in a row, often from getT Optimize this look up by using a Last Access cache. diff --git a/src/main/java/net/minecraft/server/ChunkMap.java b/src/main/java/net/minecraft/server/ChunkMap.java -index 4b8b77710b..df967ff07d 100644 +index 4b8b77710b..71ddaf591e 100644 --- a/src/main/java/net/minecraft/server/ChunkMap.java +++ b/src/main/java/net/minecraft/server/ChunkMap.java @@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { @@ -20,6 +20,33 @@ index 4b8b77710b..df967ff07d 100644 Chunk chunk1 = (Chunk) super.put(i, chunk); ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(i); +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + for (int k = chunkcoordintpair.z - 1; k <= chunkcoordintpair.z + 1; ++k) { + if (j != chunkcoordintpair.x || k != chunkcoordintpair.z) { + long l = ChunkCoordIntPair.a(j, k); +- Chunk chunk2 = (Chunk) this.get(l); ++ Chunk chunk2 = (Chunk) super.get(l); // Paper - use super to avoid polluting last access cache + + if (chunk2 != null) { + chunk.H(); +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + continue; + } + +- Chunk neighbor = this.get(ChunkCoordIntPair.a(chunkcoordintpair.x + x, chunkcoordintpair.z + z)); ++ Chunk neighbor = super.get(ChunkCoordIntPair.a(chunkcoordintpair.x + x, chunkcoordintpair.z + z)); // Paper - use super to avoid polluting last access cache + if (neighbor != null) { + neighbor.setNeighborLoaded(-x, -z); + chunk.setNeighborLoaded(x, z); +@@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { + for (int j = chunkcoordintpair.x - 1; j <= chunkcoordintpair.x + 1; ++j) { + for (int k = chunkcoordintpair.z - 1; k <= chunkcoordintpair.z + 1; ++k) { + if (j != chunkcoordintpair.x || k != chunkcoordintpair.z) { +- Chunk chunk1 = (Chunk) this.get(ChunkCoordIntPair.a(j, k)); ++ Chunk chunk1 = (Chunk) super.get(ChunkCoordIntPair.a(j, k)); // Paper - use super to avoid polluting last access cache + + if (chunk1 != null) { + chunk1.I(); @@ -0,0 +0,0 @@ public class ChunkMap extends Long2ObjectOpenHashMap { } }