From 2f4eea118a4f9012e320848fda678f4a462bee6d Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Tue, 24 Mar 2020 08:08:32 +0000 Subject: [PATCH] add clear option to /ps purge (#2714) --- .../plotsquared/plot/commands/Purge.java | 56 +++++++++++++++---- .../plotsquared/plot/database/SQLManager.java | 3 +- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Purge.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Purge.java index 7d31439aa..c61074f27 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Purge.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Purge.java @@ -11,14 +11,17 @@ import com.github.intellectualsites.plotsquared.plot.object.PlotId; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; import com.github.intellectualsites.plotsquared.plot.util.CmdConfirm; import com.github.intellectualsites.plotsquared.plot.util.StringMan; +import com.github.intellectualsites.plotsquared.plot.util.TaskManager; import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.Map.Entry; import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; -@CommandDeclaration(usage = "/plot purge world: area: id: owner: shared: unknown:[true|false]", +@CommandDeclaration(usage = "/plot purge world: area: id: owner: shared: unknown:[true|false] clear:[true|false]", command = "purge", permission = "plots.admin", description = "Purge all plots for a world", @@ -29,6 +32,7 @@ public class Purge extends SubCommand { @Override public boolean onCommand(final PlotPlayer player, String[] args) { if (args.length == 0) { + Captions.COMMAND_SYNTAX.send(player, getUsage()); return false; } @@ -38,6 +42,7 @@ public class Purge extends SubCommand { UUID owner = null; UUID added = null; boolean unknown = false; + boolean clear = false; for (String arg : args) { String[] split = arg.split(":"); if (split.length != 2) { @@ -87,6 +92,13 @@ public class Purge extends SubCommand { case "u": unknown = Boolean.parseBoolean(split[1]); break; + case "clear": + case "c": + case "delete": + case "d": + case "del": + clear = Boolean.parseBoolean(split[1]); + break; default: Captions.COMMAND_SYNTAX.send(player, getUsage()); return false; @@ -145,21 +157,43 @@ public class Purge extends SubCommand { } String cmd = "/plot purge " + StringMan.join(args, " ") + " (" + toDelete.size() + " plots)"; + boolean finalClear = clear; Runnable run = () -> { PlotSquared.debug("Calculating plots to purge, please wait..."); HashSet ids = new HashSet<>(); - for (Plot plot : toDelete) { - if (plot.temp != Integer.MAX_VALUE) { - ids.add(plot.temp); - plot.getArea().removePlot(plot.getId()); - for (PlotPlayer pp : plot.getPlayersInPlot()) { - PlotListener.plotEntry(pp, plot); + Iterator iterator = toDelete.iterator(); + AtomicBoolean cleared = new AtomicBoolean(true); + Runnable runasync = new Runnable() { + @Override public void run() { + while (iterator.hasNext() && cleared.get()) { + cleared.set(false); + Plot plot = iterator.next(); + if (plot.temp != Integer.MAX_VALUE) { + ids.add(plot.temp); + if (finalClear) { + plot.clear(false, true, () -> PlotSquared + .debug("Plot " + plot.getId() + " cleared by purge.")); + } else { + plot.removeSign(); + } + plot.getArea().removePlot(plot.getId()); + for (PlotPlayer pp : plot.getPlayersInPlot()) { + PlotListener.plotEntry(pp, plot); + } + } + cleared.set(true); + } + if (iterator.hasNext()) { + TaskManager.runTaskAsync(this); + } else { + TaskManager.runTask(() -> { + DBFunc.purgeIds(ids); + Captions.PURGE_SUCCESS.send(player, ids.size() + "/" + toDelete.size()); + }); } - plot.removeSign(); } - } - DBFunc.purgeIds(ids); - Captions.PURGE_SUCCESS.send(player, ids.size() + "/" + toDelete.size()); + }; + TaskManager.runTaskAsync(runasync); }; if (hasConfirmation(player)) { CmdConfirm.addPending(player, cmd, run); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java index 544a8d9dd..ee4bf35fa 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/database/SQLManager.java @@ -2142,7 +2142,8 @@ import java.util.concurrent.atomic.AtomicInteger; int count = 0; int last = -1; for (int j = 0; j <= amount; j++) { - PlotSquared.debug("Purging " + (j * packet) + " / " + size); + int purging = Math.max(j * packet, size); + PlotSquared.debug("Purging " + purging + " / " + size); List subList = uniqueIdsList.subList(j * packet, Math.min(size, (j + 1) * packet)); if (subList.isEmpty()) {