From 0d4af3023d034aa8a65e5aea8083b8212a926fc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 7 Apr 2020 23:18:36 +0200 Subject: [PATCH] Extract TaskManager lambdas for better debugging --- .../plotsquared/plot/commands/Auto.java | 31 ++---------- .../plotsquared/plot/object/Plot.java | 8 +-- .../plot/util/AutoClaimFinishTask.java | 50 +++++++++++++++++++ .../plot/util/ObjectTaskRunnable.java | 28 +++++++++++ .../util/RuntimeExceptionRunnableVal.java | 28 +++++++++++ .../plotsquared/plot/util/TaskManager.java | 33 +----------- 6 files changed, 112 insertions(+), 66 deletions(-) create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/AutoClaimFinishTask.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ObjectTaskRunnable.java create mode 100644 Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/RuntimeExceptionRunnableVal.java diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Auto.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Auto.java index 51c84f5b4..a04540168 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Auto.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Auto.java @@ -8,9 +8,7 @@ import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.database.DBFunc; import com.github.intellectualsites.plotsquared.plot.events.PlayerAutoPlotEvent; import com.github.intellectualsites.plotsquared.plot.events.PlotAutoMergeEvent; -import com.github.intellectualsites.plotsquared.plot.events.PlotMergeEvent; import com.github.intellectualsites.plotsquared.plot.events.Result; -import com.github.intellectualsites.plotsquared.plot.object.Direction; import com.github.intellectualsites.plotsquared.plot.object.Expression; import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.github.intellectualsites.plotsquared.plot.object.PlotArea; @@ -19,6 +17,7 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotId; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; import com.github.intellectualsites.plotsquared.plot.object.TeleportCause; +import com.github.intellectualsites.plotsquared.plot.util.AutoClaimFinishTask; import com.github.intellectualsites.plotsquared.plot.util.EconHandler; import com.github.intellectualsites.plotsquared.plot.util.MainUtil; import com.github.intellectualsites.plotsquared.plot.util.Permissions; @@ -42,7 +41,7 @@ public class Auto extends SubCommand { return id.getNextId(step); } - private static boolean checkAllowedPlots(PlotPlayer player, PlotArea plotarea, + public static boolean checkAllowedPlots(PlotPlayer player, PlotArea plotarea, @Nullable Integer allowedPlots, int sizeX, int sizeZ) { if (allowedPlots == null) { allowedPlots = player.getAllowedPlots(); @@ -126,31 +125,7 @@ public class Auto extends SubCommand { player.setMeta(Auto.class.getName(), true); autoClaimFromDatabase(player, area, start, new RunnableVal() { @Override public void run(final Plot plot) { - TaskManager.IMP.sync(new RunnableVal() { - @Override public void run(Object ignore) { - player.deleteMeta(Auto.class.getName()); - if (plot == null) { - MainUtil.sendMessage(player, Captions.NO_FREE_PLOTS); - return; - } - - if (checkAllowedPlots(player, area, allowedPlots, 1, 1)) { - plot.claim(player, true, schematic, false); - if (area.isAutoMerge()) { - PlotMergeEvent event = PlotSquared.get().getEventDispatcher() - .callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player); - if (event.getEventResult() == Result.DENY) { - sendMessage(player, Captions.EVENT_DENIED, "Auto merge"); - } else { - plot.autoMerge(event.getDir(), event.getMax(), player.getUUID(), - true); - } - } - } else { - DBFunc.delete(plot); - } - } - }); + TaskManager.IMP.sync(new AutoClaimFinishTask(player, plot, area, allowedPlots, schematic)); } }); } 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 3e9ba35ce..9da86b646 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 @@ -1042,10 +1042,6 @@ public class Plot { if (!isLoaded()) { return; } - if (!PlotSquared.get().isMainThread(Thread.currentThread())) { - TaskManager.runTask(() -> Plot.this.setSign(name)); - return; - } PlotManager manager = this.area.getPlotManager(); if (this.area.allowSigns()) { Location location = manager.getSignLoc(this); @@ -1058,9 +1054,7 @@ public class Plot { "%plr%", name), Captions.OWNER_SIGN_LINE_4.formatted().replaceAll("%id%", id).replaceAll( "%plr%", name)}; - WorldUtil.IMP - .setSign(this.getWorldName(), location.getX(), location.getY(), location.getZ(), - lines); + WorldUtil.IMP.setSign(this.getWorldName(), location.getX(), location.getY(), location.getZ(), lines); } } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/AutoClaimFinishTask.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/AutoClaimFinishTask.java new file mode 100644 index 000000000..c0fc30302 --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/AutoClaimFinishTask.java @@ -0,0 +1,50 @@ +package com.github.intellectualsites.plotsquared.plot.util; + +import com.github.intellectualsites.plotsquared.plot.PlotSquared; +import com.github.intellectualsites.plotsquared.plot.commands.Auto; +import com.github.intellectualsites.plotsquared.plot.config.Captions; +import com.github.intellectualsites.plotsquared.plot.database.DBFunc; +import com.github.intellectualsites.plotsquared.plot.events.PlotMergeEvent; +import com.github.intellectualsites.plotsquared.plot.events.Result; +import com.github.intellectualsites.plotsquared.plot.object.Direction; +import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.github.intellectualsites.plotsquared.plot.object.PlotArea; +import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; +import lombok.RequiredArgsConstructor; + +import static com.github.intellectualsites.plotsquared.plot.util.MainUtil.sendMessage; + +@RequiredArgsConstructor public final class AutoClaimFinishTask extends RunnableVal { + + private final PlotPlayer player; + private final Plot plot; + private final PlotArea area; + private final int allowedPlots; + private final String schematic; + + @Override public void run(Object value) { + player.deleteMeta(Auto.class.getName()); + if (plot == null) { + sendMessage(player, Captions.NO_FREE_PLOTS); + return; + } + + if (Auto.checkAllowedPlots(player, area, allowedPlots, 1, 1)) { + plot.claim(player, true, schematic, false); + if (area.isAutoMerge()) { + PlotMergeEvent event = PlotSquared.get().getEventDispatcher() + .callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player); + if (event.getEventResult() == Result.DENY) { + sendMessage(player, Captions.EVENT_DENIED, "Auto merge"); + } else { + plot.autoMerge(event.getDir(), event.getMax(), player.getUUID(), + true); + } + } + } else { + DBFunc.delete(plot); + } + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ObjectTaskRunnable.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ObjectTaskRunnable.java new file mode 100644 index 000000000..ba404d1cb --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/ObjectTaskRunnable.java @@ -0,0 +1,28 @@ +package com.github.intellectualsites.plotsquared.plot.util; + +import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; +import lombok.RequiredArgsConstructor; + +import java.util.Iterator; + +@RequiredArgsConstructor public class ObjectTaskRunnable implements Runnable { + + private final Iterator iterator; + private final RunnableVal task; + private final Runnable whenDone; + + @Override public void run() { + long start = System.currentTimeMillis(); + boolean hasNext; + while ((hasNext = iterator.hasNext()) && System.currentTimeMillis() - start < 5) { + task.value = iterator.next(); + task.run(); + } + if (!hasNext) { + TaskManager.runTaskLater(whenDone, 1); + } else { + TaskManager.runTaskLater(this, 1); + } + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/RuntimeExceptionRunnableVal.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/RuntimeExceptionRunnableVal.java new file mode 100644 index 000000000..81acddc9f --- /dev/null +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/RuntimeExceptionRunnableVal.java @@ -0,0 +1,28 @@ +package com.github.intellectualsites.plotsquared.plot.util; + +import com.github.intellectualsites.plotsquared.plot.object.RunnableVal; +import lombok.RequiredArgsConstructor; + +import java.util.concurrent.atomic.AtomicBoolean; + +@RequiredArgsConstructor public class RuntimeExceptionRunnableVal extends RunnableVal { + + private final RunnableVal function; + private final AtomicBoolean running; + + @Override public void run(RuntimeException value) { + try { + function.run(); + } catch (RuntimeException e) { + this.value = e; + } catch (Throwable neverHappens) { + neverHappens.printStackTrace(); + } finally { + running.set(false); + } + synchronized (function) { + function.notifyAll(); + } + } + +} diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/TaskManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/TaskManager.java index f6e86cd92..a20e80f6e 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/TaskManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/TaskManager.java @@ -93,21 +93,7 @@ public abstract class TaskManager { public static void objectTask(Collection objects, final RunnableVal task, final Runnable whenDone) { final Iterator iterator = objects.iterator(); - TaskManager.runTask(new Runnable() { - @Override public void run() { - long start = System.currentTimeMillis(); - boolean hasNext; - while ((hasNext = iterator.hasNext()) && System.currentTimeMillis() - start < 5) { - task.value = iterator.next(); - task.run(); - } - if (!hasNext) { - TaskManager.runTaskLater(whenDone, 1); - } else { - TaskManager.runTaskLater(this, 1); - } - } - }); + TaskManager.runTask(new ObjectTaskRunnable<>(iterator, task, whenDone)); } public T sync(final RunnableVal function) { @@ -120,22 +106,7 @@ public abstract class TaskManager { return function.value; } final AtomicBoolean running = new AtomicBoolean(true); - RunnableVal run = new RunnableVal() { - @Override public void run(RuntimeException value) { - try { - function.run(); - } catch (RuntimeException e) { - this.value = e; - } catch (Throwable neverHappens) { - neverHappens.printStackTrace(); - } finally { - running.set(false); - } - synchronized (function) { - function.notifyAll(); - } - } - }; + final RuntimeExceptionRunnableVal run = new RuntimeExceptionRunnableVal<>(function, running); TaskManager.IMP.task(run); try { synchronized (function) {