From 7aba70ea6548929dbae382eace7cab9f5c23b271 Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 19 May 2020 16:54:12 +0100 Subject: [PATCH] Make plot clears/sets less crashy. There's still the issue of Spigot's (or paper's) terrible GC that does nothing and stores everything in PS old gen memory so the server just numpties itself though. --- .../bukkit/queue/BukkitLocalQueue.java | 21 +++++++++++++++---- .../com/plotsquared/core/PlotSquared.java | 2 +- .../core/configuration/Settings.java | 7 +++++++ .../core/queue/GlobalBlockQueue.java | 17 +++++++++++---- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitLocalQueue.java b/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitLocalQueue.java index 3f93ebb63..c8d6d929f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitLocalQueue.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/queue/BukkitLocalQueue.java @@ -31,6 +31,7 @@ import com.plotsquared.core.PlotSquared; import com.plotsquared.core.queue.BasicLocalBlockQueue; import com.plotsquared.core.util.BlockUtil; import com.plotsquared.core.util.MainUtil; +import com.plotsquared.core.util.task.TaskManager; import com.sk89q.jnbt.CompoundTag; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.WorldEdit; @@ -50,7 +51,6 @@ import org.bukkit.block.Biome; import org.bukkit.block.Block; import org.bukkit.block.Container; import org.bukkit.block.data.BlockData; -import org.bukkit.inventory.BlockInventoryHolder; import java.util.concurrent.ExecutionException; import java.util.function.Consumer; @@ -117,9 +117,6 @@ public class BukkitLocalQueue extends BasicLocalBlockQueue { @Override public final void setComponents(LocalChunk lc) throws ExecutionException, InterruptedException { setBaseBlocks(lc); - if (setBiome() && lc.biomes != null) { - setBiomes(lc); - } } public void setBaseBlocks(LocalChunk localChunk) { @@ -165,6 +162,22 @@ public class BukkitLocalQueue extends BasicLocalBlockQueue { } } } + if (setBiome() && localChunk.biomes != null) { + for (int x = 0; x < localChunk.biomes.length; x++) { + BiomeType[] biomeZ = localChunk.biomes[x]; + if (biomeZ != null) { + for (int z = 0; z < biomeZ.length; z++) { + if (biomeZ[z] != null) { + BiomeType biomeType = biomeZ[z]; + + Biome biome = BukkitAdapter.adapt(biomeType); + worldObj.setBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z, + biome); + } + } + } + } + } }; if (isForceSync()) { chunkConsumer.accept(getChunk(worldObj, localChunk)); diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 452493181..4cb0731d5 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -270,7 +270,7 @@ public class PlotSquared { // create setup util class SetupUtils.manager = this.IMP.initSetupUtils(); // Set block - GlobalBlockQueue.IMP = new GlobalBlockQueue(IMP.initBlockQueue(), 1); + GlobalBlockQueue.IMP = new GlobalBlockQueue(IMP.initBlockQueue(), 1, Settings.QUEUE.TARGET_TIME); GlobalBlockQueue.IMP.runTask(); // Set chunk ChunkManager.manager = this.IMP.initChunkManager(); diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index 044642baa..3fbd71394 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -489,6 +489,13 @@ public class Settings extends Config { public static boolean TILE_ENTITY_CHECK = true; } + @Comment("Settings relating to PlotSquared's GlobalBlockQueue") + public static final class QUEUE { + @Comment({"Average time per tick spent completing chunk tasks in ms. Target average TPS = 20 * 50 / TARGET_TIME.", + "Waits (chunk task time / target_time) ticks before completely the next task."}) + public static int TARGET_TIME = 65; + } + @Comment({"Enable or disable parts of the plugin", "Note: A cache will use some memory if enabled"}) diff --git a/Core/src/main/java/com/plotsquared/core/queue/GlobalBlockQueue.java b/Core/src/main/java/com/plotsquared/core/queue/GlobalBlockQueue.java index 68a059583..3b63a94c0 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/GlobalBlockQueue.java +++ b/Core/src/main/java/com/plotsquared/core/queue/GlobalBlockQueue.java @@ -44,8 +44,8 @@ public class GlobalBlockQueue { private final ConcurrentLinkedDeque inactiveQueues; private final ConcurrentLinkedDeque runnables; private final AtomicBoolean running; + private final int targetTime; private QueueProvider provider; - /** * Used to calculate elapsed time in milliseconds and ensure block placement doesn't lag the * server @@ -53,6 +53,7 @@ public class GlobalBlockQueue { private long last; private long secondLast; private long lastSuccess; + private double lastPeriod = 0; private final RunnableVal2 SET_TASK = new RunnableVal2() { @Override public void run(Long free, LocalBlockQueue queue) { @@ -65,17 +66,19 @@ public class GlobalBlockQueue { } return; } - } while (((GlobalBlockQueue.this.secondLast = System.currentTimeMillis()) - - GlobalBlockQueue.this.last) < free); + } while ((lastPeriod = + ((GlobalBlockQueue.this.secondLast = System.currentTimeMillis()) + - GlobalBlockQueue.this.last)) < free); } }; - public GlobalBlockQueue(QueueProvider provider, int threads) { + public GlobalBlockQueue(QueueProvider provider, int threads, int targetTime) { this.provider = provider; this.activeQueues = new ConcurrentLinkedDeque<>(); this.inactiveQueues = new ConcurrentLinkedDeque<>(); this.runnables = new ConcurrentLinkedDeque<>(); this.running = new AtomicBoolean(); + this.targetTime = targetTime; this.PARALLEL_THREADS = threads; } @@ -112,9 +115,15 @@ public class GlobalBlockQueue { @Override public void run() { if (inactiveQueues.isEmpty() && activeQueues.isEmpty()) { lastSuccess = System.currentTimeMillis(); + lastPeriod = 0; GlobalBlockQueue.this.runEmptyTasks(); return; } + // Server laggy? Skip. + if (lastPeriod > targetTime) { + lastPeriod -= targetTime; + return; + } SET_TASK.value1 = 50 + Math.min( (50 + GlobalBlockQueue.this.last) - (GlobalBlockQueue.this.last = System.currentTimeMillis()),