diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java index f5536c330..93ec5a3df 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Condense.java @@ -29,6 +29,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; import org.apache.commons.lang3.StringUtils; @@ -70,7 +71,7 @@ public class Condense extends SubCommand { return false; } String worldname = args[0]; - World world = Bukkit.getWorld(worldname); + final World world = Bukkit.getWorld(worldname); if (world == null || !PlotMain.isPlotWorld(worldname)) { PlayerFunctions.sendMessage(plr, "INVALID WORLD"); return false; @@ -81,11 +82,92 @@ public class Condense extends SubCommand { PlayerFunctions.sendMessage(plr, "/plot condense " + worldname + " start "); return false; } - PlayerFunctions.sendMessage(plr, "NOT IMPLEMENTED"); + if (TASK) { + PlayerFunctions.sendMessage(plr, "TASK ALREADY STARTED"); + return false; + } + if (args.length == 2) { + PlayerFunctions.sendMessage(plr, "/plot condense " + worldname + " start "); + return false; + } + if (!StringUtils.isNumeric(args[2])) { + PlayerFunctions.sendMessage(plr, "INVALID RADIUS"); + return false; + } + int radius = Integer.parseInt(args[2]); + Collection plots = PlotMain.getPlots(worldname).values(); + int size = plots.size(); + int minimum_radius = (int) Math.ceil((Math.sqrt(size)/2) + 1); + if (radius < minimum_radius) { + PlayerFunctions.sendMessage(plr, "RADIUS TOO SMALL"); + return false; + } + final List to_move = new ArrayList<>(getPlots(plots, radius)); + final List free = new ArrayList<>(); + PlotId start = new PlotId(0,0); + while (start.x <= minimum_radius && start.y <= minimum_radius) { + Plot plot = PlotHelper.getPlot(world, start); + if (!plot.hasOwner()) { + free.add(plot.id); + } + start = Auto.getNextPlot(start, 1); + } + PlotHelper.move(world, to_move.get(0), free.get(0), new Runnable() { + @Override + public void run() { + if (!TASK) { + sendMessage("CONDENSE TASK CANCELLED"); + return; + } + to_move.remove(0); + free.remove(0); + int index = 0; + for (PlotId id : to_move) { + Plot plot = PlotHelper.getPlot(world, id); + if (plot.hasOwner()) { + break; + } + index++; + } + for (int i = 0; i plots = PlotMain.getPlots(worldname).values(); int size = plots.size(); int minimum_radius = (int) Math.ceil((Math.sqrt(size)/2) + 1); + if (radius < minimum_radius) { + PlayerFunctions.sendMessage(plr, "RADIUS TOO SMALL"); + return false; + } int max_move = getPlots(plots, minimum_radius).size(); int user_move = getPlots(plots, radius).size(); PlayerFunctions.sendMessage(plr, "=== DEFAULT EVAL ==="); @@ -109,6 +195,7 @@ public class Condense extends SubCommand { PlayerFunctions.sendMessage(plr, "=== INPUT EVAL ==="); PlayerFunctions.sendMessage(plr, "INPUT RADIUS: " + radius); PlayerFunctions.sendMessage(plr, "ESTIMATED MOVES: " + user_move); + PlayerFunctions.sendMessage(plr, "ESTIMATED TIME: " + (user_move * 16) +" seconds"); PlayerFunctions.sendMessage(plr, "&e - Radius is measured in plot width"); return true; } @@ -117,11 +204,11 @@ public class Condense extends SubCommand { return false; } - public Set getPlots(Collection plots, int radius) { - HashSet outside = new HashSet<>(); + public Set getPlots(Collection plots, int radius) { + HashSet outside = new HashSet<>(); for (Plot plot : plots) { if (plot.id.x > radius || plot.id.x < -radius || plot.id.y > radius || plot.id.y < -radius) { - outside.add(plot); + outside.add(plot.id); } } return outside; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java index baf5ee4c4..a5c945204 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Move.java @@ -68,7 +68,7 @@ public class Move extends SubCommand { PlayerFunctions.sendMessage(plr, "DUPLICATE ID"); return false; } - if (move(world, plot1, plot2, new Runnable() { + if (PlotHelper.move(world, plot1, plot2, new Runnable() { @Override public void run() { PlayerFunctions.sendMessage(plr, "MOVE SUCCESS"); @@ -81,42 +81,4 @@ public class Move extends SubCommand { return false; } } - - public boolean move(final World world, final PlotId current, PlotId newPlot, final Runnable whenDone) { - final Location bot1 = PlotHelper.getPlotBottomLoc(world, current); - Location bot2 = PlotHelper.getPlotBottomLoc(world, newPlot); - final Location top = PlotHelper.getPlotTopLoc(world, current); - final Plot currentPlot = PlotHelper.getPlot(world, current); - if (currentPlot.owner == null) { - return false; - } - Plot pos1 = PlayerFunctions.getBottomPlot(world, currentPlot); - Plot pos2 = PlayerFunctions.getTopPlot(world, currentPlot); - PlotId size = PlotHelper.getSize(world, currentPlot); - if (!PlotHelper.isUnowned(world, newPlot, new PlotId(newPlot.x + size.x - 1, newPlot.y + size.y - 1))) { - return false; - } - - int offset_x = newPlot.x - current.x; - int offset_y = newPlot.y - current.y; - final ArrayList selection = PlayerFunctions.getPlotSelectionIds(pos1.id, pos2.id); - String worldname = world.getName(); - for (PlotId id : selection) { - DBFunc.movePlot(world.getName(), new PlotId(id.x, id.y), new PlotId(id.x + offset_x, id.y + offset_y)); - Plot plot = PlotMain.getPlots(worldname).get(id); - PlotMain.getPlots(worldname).remove(id); - plot.id.x += offset_x; - plot.id.y += offset_y; - PlotMain.getPlots(worldname).put(plot.id, plot); - } - ChunkManager.copyRegion(bot1, top, bot2, new Runnable() { - @Override - public void run() { - Location bot = bot1.clone().add(1, 0, 1); - ChunkManager.regenerateRegion(bot, top, null); - TaskManager.runTaskLater(whenDone, 1); - } - }); - return true; - } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java index 71fed7038..96fb8c053 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java @@ -977,6 +977,44 @@ import com.intellectualcrafters.plot.object.PlotWorld; return true; } + public static boolean move(final World world, final PlotId current, PlotId newPlot, final Runnable whenDone) { + final Location bot1 = PlotHelper.getPlotBottomLoc(world, current); + Location bot2 = PlotHelper.getPlotBottomLoc(world, newPlot); + final Location top = PlotHelper.getPlotTopLoc(world, current); + final Plot currentPlot = PlotHelper.getPlot(world, current); + if (currentPlot.owner == null) { + return false; + } + Plot pos1 = PlayerFunctions.getBottomPlot(world, currentPlot); + Plot pos2 = PlayerFunctions.getTopPlot(world, currentPlot); + PlotId size = PlotHelper.getSize(world, currentPlot); + if (!PlotHelper.isUnowned(world, newPlot, new PlotId(newPlot.x + size.x - 1, newPlot.y + size.y - 1))) { + return false; + } + + int offset_x = newPlot.x - pos1.id.x; + int offset_y = newPlot.y - pos1.id.y; + final ArrayList selection = PlayerFunctions.getPlotSelectionIds(pos1.id, pos2.id); + String worldname = world.getName(); + for (PlotId id : selection) { + DBFunc.movePlot(world.getName(), new PlotId(id.x, id.y), new PlotId(id.x + offset_x, id.y + offset_y)); + Plot plot = PlotMain.getPlots(worldname).get(id); + PlotMain.getPlots(worldname).remove(id); + plot.id.x += offset_x; + plot.id.y += offset_y; + PlotMain.getPlots(worldname).put(plot.id, plot); + } + ChunkManager.copyRegion(bot1, top, bot2, new Runnable() { + @Override + public void run() { + Location bot = bot1.clone().add(1, 0, 1); + ChunkManager.regenerateRegion(bot, top, null); + TaskManager.runTaskLater(whenDone, 1); + } + }); + return true; + } + public static PlotId getSize(World world, Plot plot) { PlotSettings settings = plot.settings; if (!settings.isMerged()) {