From f8132c51c3e7aaddc09b9f2bef6600ba5c480eb4 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Tue, 28 Jan 2025 17:02:50 -0800 Subject: [PATCH] Properly implement force loaded chunk API We need to verify that the methods are only being invoked on the global region. Additionally, do not use CraftWorld#getChunk for retrieving the Chunk object as it would trip a thread check. Rather, we construct the CraftChunk manually as it is simply a world+coordinates wrapper. The call never needed to block until the chunk was loaded either. --- .../bukkit/craftbukkit/CraftWorld.java.patch | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/folia-server/paper-patches/files/src/main/java/org/bukkit/craftbukkit/CraftWorld.java.patch b/folia-server/paper-patches/files/src/main/java/org/bukkit/craftbukkit/CraftWorld.java.patch index 88bc008..bac1b1c 100644 --- a/folia-server/paper-patches/files/src/main/java/org/bukkit/craftbukkit/CraftWorld.java.patch +++ b/folia-server/paper-patches/files/src/main/java/org/bukkit/craftbukkit/CraftWorld.java.patch @@ -83,6 +83,32 @@ return true; } +@@ -648,21 +_,24 @@ + + @Override + public boolean isChunkForceLoaded(int x, int z) { ++ io.papermc.paper.threadedregions.RegionizedServer.ensureGlobalTickThread("Cannot read force-loaded chunk off global region"); // Folia - region threading + return this.getHandle().getForcedChunks().contains(ChunkPos.asLong(x, z)); + } + + @Override + public void setChunkForceLoaded(int x, int z, boolean forced) { ++ io.papermc.paper.threadedregions.RegionizedServer.ensureGlobalTickThread("Cannot modify force-loaded chunks off global region"); // Folia - region threading + warnUnsafeChunk("forceloading a faraway chunk", x, z); // Paper + this.getHandle().setChunkForced(x, z, forced); + } + + @Override + public Collection getForceLoadedChunks() { ++ io.papermc.paper.threadedregions.RegionizedServer.ensureGlobalTickThread("Cannot read force-loaded chunks off global region"); // Folia - region threading + Set chunks = new HashSet<>(); + + for (long coord : this.getHandle().getForcedChunks()) { +- chunks.add(this.getChunkAt(ChunkPos.getX(coord), ChunkPos.getZ(coord))); ++ chunks.add(new org.bukkit.craftbukkit.CraftChunk(this.world, ChunkPos.getX(coord), ChunkPos.getZ(coord))); // Folia - region threading + } + + return Collections.unmodifiableCollection(chunks); @@ -782,13 +_,15 @@ @Override