From 020b030667cf3d8090dfb3e2f8e63c9f8254041f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 17 Jul 2020 16:38:07 +0200 Subject: [PATCH] More fixes --- .../plotsquared/bukkit/BukkitPlatform.java | 4 +- .../plotsquared/bukkit/util/BukkitUtil.java | 2 +- .../bukkit/util/task/BukkitTaskManager.java | 9 ++++ .../com/plotsquared/core/command/Auto.java | 2 +- .../com/plotsquared/core/command/Claim.java | 2 +- .../plotsquared/core/command/DebugExec.java | 2 +- .../plotsquared/core/command/PluginCmd.java | 2 +- .../com/plotsquared/core/command/Trim.java | 11 ++-- .../core/generator/HybridUtils.java | 3 +- .../core/plot/expiration/ExpireManager.java | 6 +-- .../core/plot/world/SinglePlotArea.java | 2 +- .../core/plot/world/SinglePlotManager.java | 2 +- .../core/queue/BasicLocalBlockQueue.java | 2 +- .../plotsquared/core/util/RegionManager.java | 6 ++- .../core/util/task/ObjectTaskRunnable.java | 7 ++- .../core/util/task/TaskManager.java | 50 ++++++++++--------- 16 files changed, 65 insertions(+), 47 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index 256736ff7..cf236b3e4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -227,7 +227,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; // Stuff that needs to be created before the PlotSquared instance PlotPlayer.registerConverter(Player.class, BukkitUtil::getPlayer); - TaskManager.setImplementation(new BukkitTaskManager(this, timeConverter)); + TaskManager.setPlatformImplementation(new BukkitTaskManager(this, timeConverter)); final PlotSquared plotSquared = new PlotSquared(this, "Bukkit"); @@ -510,7 +510,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; this.startMetrics(); if (Settings.Enabled_Components.WORLDS) { - TaskManager.getImplementation().taskRepeat(this::unload, TaskTime.seconds(1L)); + TaskManager.getPlatformImplementation().taskRepeat(this::unload, TaskTime.seconds(1L)); try { singleWorldListener = getInjector().getInstance(SingleWorldListener.class); } catch (Exception e) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java index 5864b528e..75ac9a1be 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitUtil.java @@ -422,7 +422,7 @@ import java.util.stream.Stream; Block block = getWorld(location.getWorldName()) .getBlockAt(location.getX(), location.getY(), location.getZ()); try { - return TaskManager.getImplementation().sync(() -> { + return TaskManager.getPlatformImplementation().sync(() -> { if (block.getState() instanceof Sign) { Sign sign = (Sign) block.getState(); return sign.getLines(); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/task/BukkitTaskManager.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/task/BukkitTaskManager.java index a8d054fc9..643c4d8f3 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/task/BukkitTaskManager.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/task/BukkitTaskManager.java @@ -28,6 +28,7 @@ package com.plotsquared.bukkit.util.task; import com.google.inject.Inject; import com.google.inject.Singleton; import com.plotsquared.bukkit.BukkitPlatform; +import com.plotsquared.core.PlotSquared; import com.plotsquared.core.util.task.PlotSquaredTask; import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.util.task.TaskTime; @@ -37,6 +38,7 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import java.util.concurrent.Callable; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; /** * Bukkit implementation of {@link TaskManager} using @@ -79,6 +81,13 @@ import java.util.concurrent.Future; } } + @Override public T sync(@Nonnull final Callable function, final int timeout) throws Exception { + if (PlotSquared.get().isMainThread(Thread.currentThread())) { + return function.call(); + } + return this.callMethodSync(function).get(timeout, TimeUnit.MILLISECONDS); + } + @Override public Future callMethodSync(@NotNull final Callable method) { return Bukkit.getScheduler().callSyncMethod(this.bukkitMain, method); } diff --git a/Core/src/main/java/com/plotsquared/core/command/Auto.java b/Core/src/main/java/com/plotsquared/core/command/Auto.java index adfb04ef6..2c2f051e9 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -153,7 +153,7 @@ public class Auto extends SubCommand { autoClaimFromDatabase(player, area, start, new RunnableVal() { @Override public void run(final Plot plot) { try { - TaskManager.getImplementation().sync(new AutoClaimFinishTask(player, plot, area, schematic, + TaskManager.getPlatformImplementation().sync(new AutoClaimFinishTask(player, plot, area, schematic, PlotSquared.get().getEventDispatcher())); } catch (final Exception e) { e.printStackTrace(); diff --git a/Core/src/main/java/com/plotsquared/core/command/Claim.java b/Core/src/main/java/com/plotsquared/core/command/Claim.java index f698bdc10..bb3ae02c3 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Claim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Claim.java @@ -142,7 +142,7 @@ public class Claim extends SubCommand { final String finalSchematic = schematic; DBFunc.createPlotSafe(plot, () -> { try { - TaskManager.getImplementation().sync(() -> { + TaskManager.getPlatformImplementation().sync(() -> { if (!plot.claim(player, true, finalSchematic, false)) { logger.info(Captions.PREFIX.getTranslated() + String .format("Failed to claim plot %s", plot.getId().toCommaSeparatedString())); diff --git a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java index 387320ffb..dbe3a526f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/DebugExec.java +++ b/Core/src/main/java/com/plotsquared/core/command/DebugExec.java @@ -182,7 +182,7 @@ public class DebugExec extends SubCommand { if (this.worldEdit != null) { this.scope.put("WEManager", new WEManager()); } - this.scope.put("TaskManager", TaskManager.getImplementation()); + this.scope.put("TaskManager", TaskManager.getPlatformImplementation()); this.scope.put("ConsolePlayer", ConsolePlayer.getConsole()); this.scope.put("SchematicHandler", this.schematicHandler); this.scope.put("ChunkManager", this.chunkManager); diff --git a/Core/src/main/java/com/plotsquared/core/command/PluginCmd.java b/Core/src/main/java/com/plotsquared/core/command/PluginCmd.java index 60925ccbd..f4069bec1 100644 --- a/Core/src/main/java/com/plotsquared/core/command/PluginCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/PluginCmd.java @@ -40,7 +40,7 @@ import com.plotsquared.core.util.task.TaskManager; public class PluginCmd extends SubCommand { @Override public boolean onCommand(final PlotPlayer player, String[] args) { - TaskManager.getImplementation().taskAsync(() -> { + TaskManager.getPlatformImplementation().taskAsync(() -> { MainUtil.sendMessage(player, String.format( "$2>> $1&l" + PlotSquared.platform().getPluginName() + " $2($1Version$2: $1%s$2)", PlotSquared.get().getVersion())); diff --git a/Core/src/main/java/com/plotsquared/core/command/Trim.java b/Core/src/main/java/com/plotsquared/core/command/Trim.java index abdfb0a23..18aa48475 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Trim.java +++ b/Core/src/main/java/com/plotsquared/core/command/Trim.java @@ -43,6 +43,7 @@ import com.plotsquared.core.util.query.PlotQuery; import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.TaskManager; +import com.plotsquared.core.util.task.TaskTime; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.regions.CuboidRegion; import org.slf4j.Logger; @@ -102,7 +103,7 @@ public class Trim extends SubCommand { MainUtil.sendMessage(null, " - MCA #: " + result.value1.size()); MainUtil.sendMessage(null, " - CHUNKS: " + (result.value1.size() * 1024) + " (max)"); MainUtil.sendMessage(null, " - TIME ESTIMATE: 12 Parsecs"); - TaskManager.objectTask(plots, new RunnableVal() { + TaskManager.getPlatformImplementation().objectTask(plots, new RunnableVal() { @Override public void run(Plot plot) { Location pos1 = plot.getCorners()[0]; Location pos2 = plot.getCorners()[1]; @@ -119,7 +120,8 @@ public class Trim extends SubCommand { } } } - }, result); + }).thenAccept(ignore -> + TaskManager.getPlatformImplementation().taskLater(result, TaskTime.ticks(1))); return true; } @@ -189,11 +191,12 @@ public class Trim extends SubCommand { } } final LocalBlockQueue queue = blockQueue.getNewQueue(world, false); - TaskManager.objectTask(chunks, new RunnableVal() { + TaskManager.getPlatformImplementation().objectTask(chunks, new RunnableVal() { @Override public void run(BlockVector2 value) { queue.regenChunk(value.getX(), value.getZ()); } - }, this); + }).thenAccept(ignore -> TaskManager.getPlatformImplementation() + .taskLater(this, TaskTime.ticks(1))); } }; } else { diff --git a/Core/src/main/java/com/plotsquared/core/generator/HybridUtils.java b/Core/src/main/java/com/plotsquared/core/generator/HybridUtils.java index 3846e9e72..ba1fc503b 100644 --- a/Core/src/main/java/com/plotsquared/core/generator/HybridUtils.java +++ b/Core/src/main/java/com/plotsquared/core/generator/HybridUtils.java @@ -26,7 +26,6 @@ package com.plotsquared.core.generator; import com.google.inject.Inject; -import com.plotsquared.core.PlotSquared; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.events.PlotFlagAddEvent; import com.plotsquared.core.events.Result; @@ -479,7 +478,7 @@ public class HybridUtils { } } if (!chunks.isEmpty()) { - TaskManager.getImplementation().sync(() -> { + TaskManager.getPlatformImplementation().sync(() -> { long start = System.currentTimeMillis(); Iterator iterator = chunks.iterator(); while (System.currentTimeMillis() - start < 20 && !chunks diff --git a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java index af14ed3eb..0835ba8b1 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/expiration/ExpireManager.java @@ -326,7 +326,7 @@ public class ExpireManager { } for (ExpiryTask expiryTask : expired) { if (!expiryTask.needsAnalysis()) { - expiredTask.run(newPlot, () -> TaskManager.getImplementation() + expiredTask.run(newPlot, () -> TaskManager.getPlatformImplementation() .taskLaterAsync(task, TaskTime.ticks(1L)), expiryTask.requiresConfirmation()); return; @@ -338,7 +338,7 @@ public class ExpireManager { passesComplexity(changed, expired, new RunnableVal() { @Override public void run(Boolean confirmation) { expiredTask.run(newPlot, - () -> TaskManager.getImplementation().taskLaterAsync(task, TaskTime.ticks(1L)), + () -> TaskManager.getPlatformImplementation().taskLaterAsync(task, TaskTime.ticks(1L)), confirmation); } }, () -> { @@ -364,7 +364,7 @@ public class ExpireManager { @Override public void run(Boolean value) { doAnalysis.run(); } - }, () -> TaskManager.getImplementation().taskLaterAsync(task, TaskTime.ticks(1L))); + }, () -> TaskManager.getPlatformImplementation().taskLaterAsync(task, TaskTime.ticks(1L))); } else { doAnalysis.run(); } diff --git a/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotArea.java b/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotArea.java index ecda867bc..0d29a8714 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotArea.java +++ b/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotArea.java @@ -141,7 +141,7 @@ public class SinglePlotArea extends GridPlotWorld { } try { - TaskManager.getImplementation().sync(() -> { + TaskManager.getPlatformImplementation().sync(() -> { final String name = id.getX() + "." + id.getY(); if (!PlotSquared.platform().getWorldUtil().isWorld(name)) { PlotSquared.platform().getSetupUtils().setupWorld(builder); diff --git a/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotManager.java b/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotManager.java index 096f3f6c8..a386022a3 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/world/SinglePlotManager.java @@ -64,7 +64,7 @@ public class SinglePlotManager extends PlotManager { @Override public boolean clearPlot(Plot plot, final Runnable whenDone) { PlotSquared.platform().getSetupUtils().unload(plot.getWorldName(), false); final File worldFolder = new File(PlotSquared.platform().getWorldContainer(), plot.getWorldName()); - TaskManager.getImplementation().taskAsync(() -> { + TaskManager.getPlatformImplementation().taskAsync(() -> { MainUtil.deleteDirectory(worldFolder); if (whenDone != null) { whenDone.run(); diff --git a/Core/src/main/java/com/plotsquared/core/queue/BasicLocalBlockQueue.java b/Core/src/main/java/com/plotsquared/core/queue/BasicLocalBlockQueue.java index a5d0734c5..03aa8b437 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/BasicLocalBlockQueue.java +++ b/Core/src/main/java/com/plotsquared/core/queue/BasicLocalBlockQueue.java @@ -185,7 +185,7 @@ public abstract class BasicLocalBlockQueue extends LocalBlockQueue { @Override public void flush() { this.globalBlockQueue.dequeue(this); try { - TaskManager.getImplementation().sync(() -> { + TaskManager.getPlatformImplementation().sync(() -> { while (next()) { } return null; diff --git a/Core/src/main/java/com/plotsquared/core/util/RegionManager.java b/Core/src/main/java/com/plotsquared/core/util/RegionManager.java index 743395523..3afb51724 100644 --- a/Core/src/main/java/com/plotsquared/core/util/RegionManager.java +++ b/Core/src/main/java/com/plotsquared/core/util/RegionManager.java @@ -33,6 +33,7 @@ import com.plotsquared.core.plot.PlotManager; import com.plotsquared.core.queue.LocalBlockQueue; import com.plotsquared.core.util.task.RunnableVal; import com.plotsquared.core.util.task.TaskManager; +import com.plotsquared.core.util.task.TaskTime; import com.sk89q.worldedit.function.pattern.Pattern; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.regions.CuboidRegion; @@ -94,12 +95,13 @@ public abstract class RegionManager { } } } - TaskManager.objectTask(chunks, new RunnableVal() { + TaskManager.getPlatformImplementation().objectTask(chunks, new RunnableVal() { @Override public void run(BlockVector2 value) { chunkManager.loadChunk(world, value, false) .thenRun(() -> task.run(value)); } - }, whenDone); + }).thenAccept(ignore -> + TaskManager.getPlatformImplementation().taskLater(whenDone, TaskTime.ticks(1))); }); } diff --git a/Core/src/main/java/com/plotsquared/core/util/task/ObjectTaskRunnable.java b/Core/src/main/java/com/plotsquared/core/util/task/ObjectTaskRunnable.java index 547541332..d9201ffac 100644 --- a/Core/src/main/java/com/plotsquared/core/util/task/ObjectTaskRunnable.java +++ b/Core/src/main/java/com/plotsquared/core/util/task/ObjectTaskRunnable.java @@ -25,16 +25,19 @@ */ package com.plotsquared.core.util.task; +import lombok.Getter; import lombok.RequiredArgsConstructor; import java.util.Iterator; +import java.util.concurrent.CompletableFuture; @RequiredArgsConstructor public class ObjectTaskRunnable implements Runnable { + @Getter private final CompletableFuture completionFuture = new CompletableFuture<>(); + private final Iterator iterator; private final RunnableVal task; - private final Runnable whenDone; @Override public void run() { long start = System.currentTimeMillis(); @@ -44,7 +47,7 @@ public class ObjectTaskRunnable implements Runnable { task.run(); } if (!hasNext) { - TaskManager.runTaskLater(whenDone, TaskTime.ticks(1L)); + completionFuture.complete(null); } else { TaskManager.runTaskLater(this, TaskTime.ticks(1L)); } diff --git a/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java b/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java index 629057583..fdc04b620 100644 --- a/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java +++ b/Core/src/main/java/com/plotsquared/core/util/task/TaskManager.java @@ -25,7 +25,6 @@ */ package com.plotsquared.core.util.task; -import com.plotsquared.core.PlotSquared; import lombok.Getter; import lombok.Setter; @@ -37,8 +36,8 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; /** @@ -56,7 +55,7 @@ public abstract class TaskManager { public static final HashMap tasks = new HashMap<>(); public static AtomicInteger index = new AtomicInteger(0); - @Getter @Setter private static TaskManager implementation; + @Getter @Setter private static TaskManager platformImplementation; /** * Run a repeating synchronous task. If using a platform scheduler, @@ -69,10 +68,10 @@ public abstract class TaskManager { @Nonnull public static PlotSquaredTask runTaskRepeat(@Nullable final Runnable runnable, @Nonnull final TaskTime taskTime) { if (runnable != null) { - if (getImplementation() == null) { + if (getPlatformImplementation() == null) { throw new IllegalArgumentException("disabled"); } - return getImplementation().taskRepeat(runnable, taskTime); + return getPlatformImplementation().taskRepeat(runnable, taskTime); } return PlotSquaredTask.nullTask(); } @@ -84,11 +83,11 @@ public abstract class TaskManager { */ public static void runTaskAsync(@Nullable final Runnable runnable) { if (runnable != null) { - if (getImplementation() == null) { + if (getPlatformImplementation() == null) { runnable.run(); return; } - getImplementation().taskAsync(runnable); + getPlatformImplementation().taskAsync(runnable); } } @@ -100,11 +99,11 @@ public abstract class TaskManager { */ public static void runTask(@Nullable final Runnable runnable) { if (runnable != null) { - if (getImplementation() == null) { + if (getPlatformImplementation() == null) { runnable.run(); return; } - getImplementation().task(runnable); + getPlatformImplementation().task(runnable); } } @@ -118,11 +117,11 @@ public abstract class TaskManager { public static void runTaskLater(@Nullable final Runnable runnable, @Nonnull final TaskTime taskTime) { if (runnable != null) { - if (getImplementation() == null) { + if (getPlatformImplementation() == null) { runnable.run(); return; } - getImplementation().taskLater(runnable, taskTime); + getPlatformImplementation().taskLater(runnable, taskTime); } } @@ -136,21 +135,28 @@ public abstract class TaskManager { public static void runTaskLaterAsync(@Nullable final Runnable runnable, @Nonnull final TaskTime taskTime) { if (runnable != null) { - if (getImplementation() == null) { + if (getPlatformImplementation() == null) { runnable.run(); return; } - getImplementation().taskLaterAsync(runnable, taskTime); + getPlatformImplementation().taskLaterAsync(runnable, taskTime); } } /** - * Break up a series of tasks so that they can run without lagging the server. + * Break up a series of tasks so that they can run without lagging the server + * + * @param objects Objects to perform the task on + * @param task Task to perform + * @param Object type + * @return Future that completes when the tasks are done */ - public static void objectTask(Collection objects, final RunnableVal task, - final Runnable whenDone) { + public CompletableFuture objectTask(@Nonnull final Collection objects, + @Nonnull final RunnableVal task) { final Iterator iterator = objects.iterator(); - TaskManager.runTask(new ObjectTaskRunnable<>(iterator, task, whenDone)); + final ObjectTaskRunnable taskRunnable = new ObjectTaskRunnable<>(iterator, task); + TaskManager.runTask(taskRunnable); + return taskRunnable.getCompletionFuture(); } /** @@ -161,7 +167,7 @@ public abstract class TaskManager { * @return Method result * @throws Exception If the call fails */ - public T sync(final Callable function) throws Exception { + public T sync(@Nonnull final Callable function) throws Exception { return sync(function, Integer.MAX_VALUE); } @@ -174,12 +180,8 @@ public abstract class TaskManager { * @return Method result * @throws Exception If the call fails */ - public T sync(final Callable function, int timeout) throws Exception { - if (PlotSquared.get().isMainThread(Thread.currentThread())) { - return function.call(); - } - return this.callMethodSync(function).get(timeout, TimeUnit.MILLISECONDS); - } + public abstract T sync(@Nonnull final Callable function, final int timeout) + throws Exception; /** * Call a method synchronously and return a future with