diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java index a9c6b2518..5735d0b94 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitChunkManager.java @@ -228,7 +228,7 @@ public class BukkitChunkManager extends ChunkManager { } } queue.enqueue(); - GlobalBlockQueue.IMP.addTask(() -> { + GlobalBlockQueue.IMP.addEmptyTask(() -> { //map.restoreBlocks(newWorld, 0, 0); map.restoreEntities(newWorld, relX, relZ); TaskManager.runTask(whenDone); @@ -444,7 +444,7 @@ public class BukkitChunkManager extends ChunkManager { maps.add(swapChunk(world1, world2, chunk1, chunk2, region1, region2)); } } - GlobalBlockQueue.IMP.addTask(() -> { + GlobalBlockQueue.IMP.addEmptyTask(() -> { for (ContentMap map : maps) { map.restoreEntities(world1, 0, 0); TaskManager.runTaskLater(whenDone, 1); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Clear.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Clear.java index b45c52f65..9dcd475a1 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Clear.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Clear.java @@ -42,7 +42,7 @@ import java.util.concurrent.CompletableFuture; final long start = System.currentTimeMillis(); boolean result = plot.clear(true, false, () -> { plot.unlink(); - GlobalBlockQueue.IMP.addTask(() -> { + GlobalBlockQueue.IMP.addEmptyTask(() -> { plot.removeRunning(); // If the state changes, then mark it as no longer done if (plot.getFlag(Flags.DONE).isPresent()) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java index d3c65c8db..582dfb38a 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Set.java @@ -107,7 +107,7 @@ import java.util.stream.IntStream; current.setComponent(component, bucket); } MainUtil.sendMessage(player, Captions.GENERATING_COMPONENT); - GlobalBlockQueue.IMP.addTask(plot::removeRunning); + GlobalBlockQueue.IMP.addEmptyTask(plot::removeRunning); return true; } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Template.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Template.java index 563956c46..fe41e61ab 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Template.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Template.java @@ -158,7 +158,7 @@ import java.util.zip.ZipOutputStream; setup.step = new ConfigurationNode[0]; setup.world = world; SetupUtils.manager.setupWorld(setup); - GlobalBlockQueue.IMP.addTask(() -> { + GlobalBlockQueue.IMP.addEmptyTask(() -> { MainUtil.sendMessage(player, "Done!"); player.teleport(WorldUtil.IMP.getSpawn(world)); }); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java index 320953963..f7ce683ba 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/ClassicPlotManager.java @@ -46,7 +46,7 @@ public class ClassicPlotManager extends SquarePlotManager { @Override public boolean unClaimPlot(Plot plot, Runnable whenDone) { setWallFilling(plot.getId(), classicPlotWorld.WALL_FILLING); setWall(plot.getId(), classicPlotWorld.WALL_BLOCK); - return GlobalBlockQueue.IMP.addTask(whenDone); + return GlobalBlockQueue.IMP.addEmptyTask(whenDone); } public boolean setFloor(PlotId plotId, BlockBucket blocks) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java index 0a3fc4de6..cd711dd6e 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridPlotManager.java @@ -207,7 +207,7 @@ public class HybridPlotManager extends ClassicPlotManager { }, () -> { queue.enqueue(); // And notify whatever called this when plot clearing is done - GlobalBlockQueue.IMP.addTask(whenDone); + GlobalBlockQueue.IMP.addEmptyTask(whenDone); }, 10); return true; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java index 3f4261440..3a5c29988 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/generator/HybridUtils.java @@ -223,7 +223,7 @@ public abstract class HybridUtils { PlotSquared.debug("&d - Potentially skipping 1024 chunks"); PlotSquared.debug("&d - TODO: recommend chunkster if corrupt"); } - GlobalBlockQueue.IMP.addTask(() -> TaskManager.runTaskLater(task, 20)); + GlobalBlockQueue.IMP.addEmptyTask(() -> TaskManager.runTaskLater(task, 20)); }); } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java index 30c5034a8..852a1647b 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Plot.java @@ -869,7 +869,7 @@ public class Plot { manager.claimPlot(current); } } - GlobalBlockQueue.IMP.addTask(run); + GlobalBlockQueue.IMP.addEmptyTask(run); return; } Plot current = queue.poll(); @@ -982,7 +982,7 @@ public class Plot { current.setMerged(merged); } if (createSign) { - GlobalBlockQueue.IMP.addTask(() -> { + GlobalBlockQueue.IMP.addEmptyTask(() -> { for (Plot current : plots) { current.setSign(MainUtil.getName(current.owner)); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/GlobalBlockQueue.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/GlobalBlockQueue.java index 9725af48c..049b13d32 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/GlobalBlockQueue.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/GlobalBlockQueue.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; -import java.util.Vector; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.atomic.AtomicBoolean; @@ -21,8 +20,10 @@ public class GlobalBlockQueue { private final ConcurrentLinkedDeque runnables; private final AtomicBoolean running; private QueueProvider provider; + /** - * Used to calculate elapsed time in milliseconds and ensure block placement doesn't lag the server + * Used to calculate elapsed time in milliseconds and ensure block placement doesn't lag the + * server */ private long last; private long secondLast; @@ -35,11 +36,12 @@ public class GlobalBlockQueue { if (!more) { lastSuccess = last; if (inactiveQueues.size() == 0 && activeQueues.size() == 0) { - tasks(); + runEmptyTasks(); } return; } - } while (((GlobalBlockQueue.this.secondLast = System.currentTimeMillis())- GlobalBlockQueue.this.last) < free); + } while (((GlobalBlockQueue.this.secondLast = System.currentTimeMillis()) + - GlobalBlockQueue.this.last) < free); } }; @@ -85,10 +87,12 @@ public class GlobalBlockQueue { @Override public void run() { if (inactiveQueues.isEmpty() && activeQueues.isEmpty()) { lastSuccess = System.currentTimeMillis(); - GlobalBlockQueue.this.tasks(); + GlobalBlockQueue.this.runEmptyTasks(); return; } - SET_TASK.value1 = 50 + Math.min((50 + GlobalBlockQueue.this.last) - (GlobalBlockQueue.this.last = System.currentTimeMillis()), + SET_TASK.value1 = 50 + Math.min( + (50 + GlobalBlockQueue.this.last) - (GlobalBlockQueue.this.last = + System.currentTimeMillis()), GlobalBlockQueue.this.secondLast - System.currentTimeMillis()); SET_TASK.value2 = GlobalBlockQueue.this.getNextQueue(); if (SET_TASK.value2 == null) { @@ -231,7 +235,7 @@ public class GlobalBlockQueue { public LocalBlockQueue getNextQueue() { long now = System.currentTimeMillis(); - while (activeQueues.size() > 0) { + while (!activeQueues.isEmpty()) { LocalBlockQueue queue = activeQueues.peek(); if (queue != null && queue.size() > 0) { queue.setModified(now); @@ -251,7 +255,7 @@ public class GlobalBlockQueue { long age = now - queue.getModified(); total += queue.size(); if (queue.size() == 0) { - if (age > 1000) { + if (age > 60000) { iter.remove(); } continue; @@ -263,7 +267,7 @@ public class GlobalBlockQueue { firstNonEmpty.setModified(now); return firstNonEmpty; } - if (age > 60000) { + if (age > 1000) { queue.setModified(now); return queue; } @@ -279,10 +283,10 @@ public class GlobalBlockQueue { return activeQueues.size() == 0 && inactiveQueues.size() == 0; } - public boolean addTask(final Runnable whenDone) { + public boolean addEmptyTask(final Runnable whenDone) { if (this.isDone()) { // Run - this.tasks(); + this.runEmptyTasks(); if (whenDone != null) { whenDone.run(); } @@ -294,19 +298,18 @@ public class GlobalBlockQueue { return false; } - public synchronized boolean tasks() { + private synchronized void runEmptyTasks() { if (this.runnables.isEmpty()) { - return false; + return; } - final ArrayList tmp = new ArrayList<>(this.runnables); + final ConcurrentLinkedDeque tmp = new ConcurrentLinkedDeque<>(this.runnables); this.runnables.clear(); for (final Runnable runnable : tmp) { runnable.run(); } - return true; } public enum QueueStage { - INACTIVE, ACTIVE, NONE; + INACTIVE, ACTIVE, NONE } }