From 162dea88de08979d7a67c4fa57b3d48eebf9a406 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Sat, 16 Nov 2024 12:11:56 +0000 Subject: [PATCH] Add for 1.21.3 --- .../fawe/v1_21_3/PaperweightGetBlocks.java | 21 +++++++++++++++++-- .../v1_21_3/PaperweightPlatformAdapter.java | 3 --- .../fawe/v1_21_4/PaperweightGetBlocks.java | 19 ++++++++++++++++- .../v1_21_4/PaperweightPlatformAdapter.java | 3 --- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightGetBlocks.java index 3013277fe2..46522944c5 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightGetBlocks.java @@ -17,6 +17,7 @@ import com.fastasyncworldedit.core.queue.implementation.QueueHandler; import com.fastasyncworldedit.core.queue.implementation.blocks.CharGetBlocks; import com.fastasyncworldedit.core.util.MathMan; +import com.fastasyncworldedit.core.util.MemUtil; import com.fastasyncworldedit.core.util.NbtUtils; import com.fastasyncworldedit.core.util.collection.AdaptedMap; import com.sk89q.worldedit.bukkit.BukkitAdapter; @@ -211,7 +212,7 @@ public void setSkyLightingToGet(char[][] light, int minSectionPosition, int maxS try { fillLightNibble(light, LightLayer.SKY, minSectionPosition, maxSectionPosition); } catch (Throwable e) { - LOGGER.error("Error setting lighting to get", e); + LOGGER.error("Error setting sky lighting to get", e); } } } @@ -432,7 +433,7 @@ public CompletableFuture ensureLoaded(ServerLevel nmsWorld, int chun } @Override - @SuppressWarnings("rawtypes") + @SuppressWarnings({"rawtypes", "unchecked"}) public synchronized > T call(IQueueExtent owner, IChunkSet set, Runnable finalizer) { if (!callLock.isHeldByCurrentThread()) { throw new IllegalStateException("Attempted to call chunk GET but chunk was not call-locked."); @@ -441,11 +442,24 @@ public synchronized > T call(IQueueExtent final ServerLevel nmsWorld = serverLevel; CompletableFuture nmsChunkFuture = ensureLoaded(nmsWorld, chunkX, chunkZ); LevelChunk chunk = nmsChunkFuture.getNow(null); + if ((chunk == null && MemUtil.shouldBeginSlow()) || Settings.settings().QUEUE.ASYNC_CHUNK_LOAD_WRITE) { + try { + // "Artificially" slow FAWE down if memory low as performing the operation async can cause large amounts of + // memory usage + chunk = nmsChunkFuture.get(); + } catch (InterruptedException | ExecutionException e) { + LOGGER.error("Could not get chunk at {},{} whilst low memory", chunkX, chunkZ, e); + throw new FaweException( + TextComponent.of("Could not get chunk at " + chunkX + "," + chunkZ + " whilst low memory: " + e.getMessage())); + } + } final int finalCopyKey = copyKey; // Run immediately if possible if (chunk != null) { return tryWrappedInternalCall(set, finalizer, finalCopyKey, chunk, nmsWorld); } + // Submit via the STQE as that will help handle excessive queuing by waiting for the submission count to fall below the + // target size nmsChunkFuture.thenApply(nmsChunk -> owner.submitTaskUnchecked(() -> (T) tryWrappedInternalCall( set, finalizer, @@ -453,6 +467,9 @@ public synchronized > T call(IQueueExtent nmsChunk, nmsWorld ))); + // If we have re-submitted, return a completed future to prevent potential deadlocks where a future reliant on the + // above submission is halting the BlockingExecutor, and preventing the above task from actually running. The futures + // submitted above will still be added to the STQE submissions. return (T) (Future) CompletableFuture.completedFuture(null); } diff --git a/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightPlatformAdapter.java index 14e504574f..91c1513551 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_21_3/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_3/PaperweightPlatformAdapter.java @@ -56,7 +56,6 @@ import net.minecraft.world.level.chunk.status.ChunkStatus; import net.minecraft.world.level.entity.PersistentEntitySectionManager; import org.apache.logging.log4j.Logger; -import org.bukkit.Bukkit; import org.bukkit.craftbukkit.CraftChunk; import javax.annotation.Nonnull; @@ -79,8 +78,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Semaphore; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.function.IntFunction; import static java.lang.invoke.MethodType.methodType; diff --git a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java index 2ec4259319..0ae51242b5 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java +++ b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightGetBlocks.java @@ -17,6 +17,7 @@ import com.fastasyncworldedit.core.queue.implementation.QueueHandler; import com.fastasyncworldedit.core.queue.implementation.blocks.CharGetBlocks; import com.fastasyncworldedit.core.util.MathMan; +import com.fastasyncworldedit.core.util.MemUtil; import com.fastasyncworldedit.core.util.NbtUtils; import com.fastasyncworldedit.core.util.collection.AdaptedMap; import com.sk89q.worldedit.bukkit.BukkitAdapter; @@ -432,7 +433,7 @@ public CompletableFuture ensureLoaded(ServerLevel nmsWorld, int chun } @Override - @SuppressWarnings("rawtypes") + @SuppressWarnings({"rawtypes", "unchecked"}) public synchronized > T call(IQueueExtent owner, IChunkSet set, Runnable finalizer) { if (!callLock.isHeldByCurrentThread()) { throw new IllegalStateException("Attempted to call chunk GET but chunk was not call-locked."); @@ -441,11 +442,24 @@ public synchronized > T call(IQueueExtent final ServerLevel nmsWorld = serverLevel; CompletableFuture nmsChunkFuture = ensureLoaded(nmsWorld, chunkX, chunkZ); LevelChunk chunk = nmsChunkFuture.getNow(null); + if ((chunk == null && MemUtil.shouldBeginSlow()) || Settings.settings().QUEUE.ASYNC_CHUNK_LOAD_WRITE) { + try { + // "Artificially" slow FAWE down if memory low as performing the operation async can cause large amounts of + // memory usage + chunk = nmsChunkFuture.get(); + } catch (InterruptedException | ExecutionException e) { + LOGGER.error("Could not get chunk at {},{} whilst low memory", chunkX, chunkZ, e); + throw new FaweException( + TextComponent.of("Could not get chunk at " + chunkX + "," + chunkZ + " whilst low memory: " + e.getMessage())); + } + } final int finalCopyKey = copyKey; // Run immediately if possible if (chunk != null) { return tryWrappedInternalCall(set, finalizer, finalCopyKey, chunk, nmsWorld); } + // Submit via the STQE as that will help handle excessive queuing by waiting for the submission count to fall below the + // target size nmsChunkFuture.thenApply(nmsChunk -> owner.submitTaskUnchecked(() -> (T) tryWrappedInternalCall( set, finalizer, @@ -453,6 +467,9 @@ public synchronized > T call(IQueueExtent nmsChunk, nmsWorld ))); + // If we have re-submitted, return a completed future to prevent potential deadlocks where a future reliant on the + // above submission is halting the BlockingExecutor, and preventing the above task from actually running. The futures + // submitted above will still be added to the STQE submissions. return (T) (Future) CompletableFuture.completedFuture(null); } diff --git a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightPlatformAdapter.java index 2fc4bf0189..a247adf126 100644 --- a/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_21_4/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_21_4/PaperweightPlatformAdapter.java @@ -55,7 +55,6 @@ import net.minecraft.world.level.chunk.status.ChunkStatus; import net.minecraft.world.level.entity.PersistentEntitySectionManager; import org.apache.logging.log4j.Logger; -import org.bukkit.Bukkit; import org.bukkit.craftbukkit.CraftChunk; import javax.annotation.Nonnull; @@ -78,8 +77,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.Semaphore; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import java.util.function.IntFunction; import static java.lang.invoke.MethodType.methodType;