From fc5ae5be8255b9d1c03a551d656543a85b091f64 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Mon, 24 Oct 2022 19:23:46 -0700 Subject: [PATCH] Fix incorrect handling of mustNotSave CB used the dirty flag to construct the chunk unload event, but then sets mustNotSave to the inverted value of the event after calling the event without considering that the chunk may actually be brought up to loaded status again later. Then, CB overrides the isUnsaved method of LevelChunk to additionally use mustNotSave. Thus, if the chunk is not marked dirty when unloading, the mustNotSave value will be set to true. Then, once the chunk is reloaded and edited the dirty flag will be set. However, when unloading the chunk finally, the isUnsaved method will return false due to mustNotSave being true. Thus, the chunk will never be saved. To fix these issues, no longer make mustNotSave override isUnsaved and always set the save flag for the chunk unload event. This issue started popping up recently due to the recent change to mark chunks as not dirty after saving them, which increased the chance of the save issue occurring in the first place. --- patches/server/0016-Rewrite-chunk-system.patch | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/patches/server/0016-Rewrite-chunk-system.patch b/patches/server/0016-Rewrite-chunk-system.patch index 7e3c8d8b1..0b4810be7 100644 --- a/patches/server/0016-Rewrite-chunk-system.patch +++ b/patches/server/0016-Rewrite-chunk-system.patch @@ -16675,7 +16675,7 @@ index e6240f891e396d91e31b02fdf3084be77e9d6697..00cb9dafc711607f28529ea9afbcdb49 public int getIndex() { diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java -index e75ec8f6aa597b5f3048d6269fba45eef057bc71..0e2a15d623e5ff5c34252e4b714713a6a670e755 100644 +index e75ec8f6aa597b5f3048d6269fba45eef057bc71..81092acad680e6c797745d24badba4dd488bb246 100644 --- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java +++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java @@ -183,6 +183,43 @@ public class LevelChunk extends ChunkAccess { @@ -16819,16 +16819,27 @@ index e75ec8f6aa597b5f3048d6269fba45eef057bc71..0e2a15d623e5ff5c34252e4b714713a6 if (this.needsDecoration) { try (co.aikar.timings.Timing ignored = this.level.timings.chunkLoadPopulate.startTiming()) { // Paper -@@ -716,7 +817,9 @@ public class LevelChunk extends ChunkAccess { +@@ -716,8 +817,10 @@ public class LevelChunk extends ChunkAccess { } public void unloadCallback() { + if (!this.loadedTicketLevel) { LOGGER.error("Double calling chunk unload!", new Throwable()); } // Paper org.bukkit.Server server = this.level.getCraftServer(); +- org.bukkit.event.world.ChunkUnloadEvent unloadEvent = new org.bukkit.event.world.ChunkUnloadEvent(this.bukkitChunk, this.isUnsaved()); + this.chunkHolder.getEntityChunk().callEntitiesUnloadEvent(); // Paper - rewrite chunk system - org.bukkit.event.world.ChunkUnloadEvent unloadEvent = new org.bukkit.event.world.ChunkUnloadEvent(this.bukkitChunk, this.isUnsaved()); ++ org.bukkit.event.world.ChunkUnloadEvent unloadEvent = new org.bukkit.event.world.ChunkUnloadEvent(this.bukkitChunk, true); // Paper - rewrite chunk system - force save to true so that mustNotSave is correctly set below server.getPluginManager().callEvent(unloadEvent); // note: saving can be prevented, but not forced if no saving is actually required + this.mustNotSave = !unloadEvent.isSaveChunk(); +@@ -741,7 +844,7 @@ public class LevelChunk extends ChunkAccess { + + @Override + public boolean isUnsaved() { +- return super.isUnsaved() && !this.mustNotSave; ++ return super.isUnsaved(); // Paper - rewrite chunk system - do NOT clobber the dirty flag + } + // CraftBukkit end + @@ -804,7 +907,10 @@ public class LevelChunk extends ChunkAccess { }); }