diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java index e64f33fa6..285a80d57 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Swap.java @@ -20,12 +20,18 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; +import java.util.ArrayList; + import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotCluster; +import com.intellectualcrafters.plot.object.PlotClusterId; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.ChunkManager; +import com.intellectualcrafters.plot.util.ClusterManager; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.Permissions; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; @@ -42,6 +48,7 @@ public class Swap extends SubCommand { @Override public boolean execute(final PlotPlayer plr, final String... args) { + MainUtil.sendMessage(plr, "&cThis command has not been optimized for large selections yet. Please bug me if this becomes an issue."); if (args.length < 1) { MainUtil.sendMessage(plr, C.NEED_PLOT_ID); MainUtil.sendMessage(plr, C.SWAP_SYNTAX); @@ -56,38 +63,69 @@ public class Swap extends SubCommand { MainUtil.sendMessage(plr, C.NO_PLOT_PERMS); return false; } - if ((plot != null) && plot.settings.isMerged()) { - MainUtil.sendMessage(plr, C.UNLINK_REQUIRED); + + Plot bot1 = MainUtil.getBottomPlot(plot); + Plot top1 = MainUtil.getTopPlot(plot); + + PlotId id2 = PlotId.fromString(args[0]); + if (id2 == null) { + MainUtil.sendMessage(plr, C.NOT_VALID_PLOT_ID); + MainUtil.sendMessage(plr, C.SWAP_SYNTAX); return false; } - final String id = args[0]; - PlotId plotid; final String world = loc.getWorld(); - try { - plotid = new PlotId(Integer.parseInt(id.split(";")[0]), Integer.parseInt(id.split(";")[1])); - final Plot plot2 = PlotSquared.getPlots(world).get(plotid); - if (((plot2 == null) || !plot2.hasOwner() || !(plot2.owner.equals(plr.getUUID()))) && !Permissions.hasPermission(plr, "plots.admin.command.swap")) { - MainUtil.sendMessage(plr, C.NO_PERM_MERGE, plotid.toString()); - return false; - } - } catch (final Exception e) { - MainUtil.sendMessage(plr, C.NOT_VALID_PLOT_ID); + Plot plot2 = MainUtil.getPlot(world, id2); + PlotId id3 = new PlotId(id2.x + top1.id.x - bot1.id.x, id2.y + top1.id.y - bot1.id.y); + Plot plot3 = MainUtil.getPlot(world, id3); + + // Getting secon selection + Plot bot2 = MainUtil.getBottomPlot(plot2); + Plot top2 = MainUtil.getTopPlot(plot3); + + // cancel swap if intersection + PlotCluster cluster1 = new PlotCluster(world, bot1.id, top1.id, null); + PlotClusterId cluster2id = new PlotClusterId(bot2.id, top2.id); + if (ClusterManager.intersects(cluster1, cluster2id)) { + MainUtil.sendMessage(plr, C.SWAP_OVERLAP); + return false; + } + + // Check dimensions + if (top1.id.x - bot1.id.x != top2.id.x - bot2.id.x || top1.id.y - bot1.id.y != top2.id.y - bot2.id.y ) { + MainUtil.sendMessage(plr, C.SWAP_DIMENSIONS, "1"); MainUtil.sendMessage(plr, C.SWAP_SYNTAX); return false; } - assert plot != null; - if (plot.id.equals(plotid)) { - MainUtil.sendMessage(plr, C.NOT_VALID_PLOT_ID); - MainUtil.sendMessage(plr, C.SWAP_SYNTAX); - return false; + + // Getting selections as ids + final ArrayList selection1 = MainUtil.getPlotSelectionIds(bot1.id, top1.id); + final ArrayList selection2 = MainUtil.getPlotSelectionIds(bot2.id, top2.id); + + // Getting selections as location coordinates + Location pos1 = MainUtil.getPlotBottomLocAbs(world, bot1.id).add(1, 0, 1); + Location pos2 = MainUtil.getPlotTopLocAbs(world, top1.id); + Location pos3 = MainUtil.getPlotBottomLocAbs(world, bot2.id).add(1, 0, 1); + Location pos4 = MainUtil.getPlotTopLocAbs(world, top2.id); + + // Swapping the blocks, states and entites + ChunkManager.manager.swap(world, pos1, pos2, pos3, pos4); + + // Swapping the plot data + for (int i = 0; i < selection1.size(); i++) { + final boolean last = i == selection1.size() - 1; + PlotId swaper = selection1.get(i); + PlotId swapee = selection2.get(i); + MainUtil.swapData(world, swaper, swapee, new Runnable() { + @Override + public void run() { + if (last) { + MainUtil.sendMessage(plr, C.SWAP_SUCCESS); + MainUtil.update(plr.getLocation()); + } + } + }); } - MainUtil.swap(world, plot.id, plotid, new Runnable() { - @Override - public void run() { - // FIXME not implemented yet - } - }); - MainUtil.update(plr.getLocation()); + MainUtil.sendMessage(plr, C.STARTED_SWAP); return true; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java index 5a003e28e..fd0f23c9d 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/config/C.java @@ -95,8 +95,11 @@ public enum C { /* * Swap */ + SWAP_OVERLAP("$2The proposed areas are not allowed to overlap"), + SWAP_DIMENSIONS("$2The proposed areas must have comparable dimensions"), SWAP_SYNTAX("$2/plots swap "), SWAP_SUCCESS("$4Successfully swapped plots"), + STARTED_SWAP("$2Started plot swap task. You will be notified when it finishes"), /* * Comment */