From 3e6b0b83946cd9a8a0745f4b3529b0870f887daa Mon Sep 17 00:00:00 2001 From: MattBDev <4009945+MattBDev@users.noreply.github.com> Date: Tue, 13 Aug 2019 22:01:23 -0400 Subject: [PATCH] Work on directions and merging --- .../bukkit/events/PlotMergeEvent.java | 7 +- .../plotsquared/plot/commands/Merge.java | 24 +++-- .../plotsquared/plot/object/Direction.java | 9 ++ .../plotsquared/plot/object/Plot.java | 95 +++++-------------- 4 files changed, 52 insertions(+), 83 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java index 493145f2a..55488011e 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/events/PlotMergeEvent.java @@ -6,8 +6,7 @@ import lombok.Setter; import org.bukkit.World; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; - -import javax.annotation.Nonnull; +import org.jetbrains.annotations.NotNull; /** * Event called when several plots are merged @@ -29,8 +28,8 @@ public final class PlotMergeEvent extends PlotEvent implements Cancellable { * @param dir The direction of the merge * @param max Max merge size */ - public PlotMergeEvent(@Nonnull final World world, @Nonnull final Plot plot, - @Nonnull final int dir, @Nonnull final int max) { + public PlotMergeEvent(@NotNull final World world, @NotNull final Plot plot, + @NotNull final int dir, @NotNull final int max) { super(plot); this.world = world; this.dir = dir; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Merge.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Merge.java index fcf5650b2..bbd0979cd 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Merge.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Merge.java @@ -3,6 +3,7 @@ package com.github.intellectualsites.plotsquared.plot.commands; import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Settings; +import com.github.intellectualsites.plotsquared.plot.object.Direction; import com.github.intellectualsites.plotsquared.plot.object.Expression; import com.github.intellectualsites.plotsquared.plot.object.Location; import com.github.intellectualsites.plotsquared.plot.object.Plot; @@ -17,6 +18,8 @@ import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler; import java.util.UUID; +import static com.github.intellectualsites.plotsquared.plot.object.Direction.getFromIndex; + @CommandDeclaration(command = "merge", aliases = "m", description = "Merge the plot you are standing on, with another plot", permission = "plots.merge", usage = "/plot merge [removeroads]", @@ -82,20 +85,20 @@ public class Merge extends SubCommand { MainUtil.sendMessage(player, Captions.NO_PERMISSION, "plots.merge." + (size + 1)); return false; } - int direction = -1; + Direction direction = Direction.ALL; if (args.length == 0) { switch (direction(player.getLocationFull().getYaw())) { case "NORTH": - direction = 0; + direction = Direction.NORTH; break; case "EAST": - direction = 1; + direction = Direction.EAST; break; case "SOUTH": - direction = 2; + direction = Direction.SOUTH; break; case "WEST": - direction = 3; + direction = Direction.WEST; break; } } else { @@ -110,7 +113,7 @@ public class Merge extends SubCommand { Captions.PERMISSION_MERGE_KEEP_ROAD.getTranslated()); return true; } - if (plot.autoMerge(-1, maxSize, uuid, terrain)) { + if (plot.autoMerge(Direction.ALL, maxSize, uuid, terrain)) { if (EconHandler.manager != null && plotArea.USE_ECONOMY && price > 0d) { EconHandler.manager.withdrawMoney(player, price); sendMessage(player, Captions.REMOVED_BALANCE, String.valueOf(price)); @@ -124,12 +127,12 @@ public class Merge extends SubCommand { } for (int i = 0; i < values.length; i++) { if (args[0].equalsIgnoreCase(values[i]) || args[0].equalsIgnoreCase(aliases[i])) { - direction = i; + direction = getFromIndex(i); break; } } } - if (direction == -1) { + if (direction == Direction.ALL) { MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot merge <" + StringMan.join(values, "|") + "> [removeroads]"); MainUtil.sendMessage(player, @@ -156,7 +159,8 @@ public class Merge extends SubCommand { return true; } Plot adjacent = plot.getRelative(direction); - if (adjacent == null || !adjacent.hasOwner() || adjacent.getMerged((direction + 2) % 4) + if (adjacent == null || !adjacent.hasOwner() || adjacent + .getMerged((direction.getIndex() + 2) % 4) || adjacent.isOwner(uuid)) { MainUtil.sendMessage(player, Captions.NO_AVAILABLE_AUTOMERGE); return false; @@ -173,7 +177,7 @@ public class Merge extends SubCommand { continue; } isOnline = true; - final int dir = direction; + final Direction dir = direction; Runnable run = () -> { MainUtil.sendMessage(accepter, Captions.MERGE_ACCEPTED); plot.autoMerge(dir, maxSize - size, owner, terrain); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Direction.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Direction.java index e39dfba0e..3c3900dc1 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Direction.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/Direction.java @@ -16,6 +16,15 @@ public enum Direction { this.name = name; } + public static Direction getFromIndex(int index) { + for (Direction value : values()) { + if (value.getIndex() == index) { + return value; + } + } + return NORTH; + } + public int getIndex() { return index; } 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 1dc7d9f81..f130041c6 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 @@ -1545,7 +1545,7 @@ public class Plot { DBFunc.createPlotAndSettings(this, () -> { PlotArea plotworld = Plot.this.area; if (notify && plotworld.AUTO_MERGE) { - Plot.this.autoMerge(-1, Integer.MAX_VALUE, uuid, true); + Plot.this.autoMerge(Direction.ALL, Integer.MAX_VALUE, uuid, true); } }); return true; @@ -2022,56 +2022,12 @@ public class Plot { * Sets the raw merge data
* - Updates DB
* - Does not modify terrain
- * ----------
- * 0 = north
- * 1 = east
- * 2 = south
- * 3 = west
- * ----------
- * - * @param direction - * @param value - */ - public void setMerged(int direction, boolean value) { - if (this.getSettings().setMerged(direction, value)) { - if (value) { - Plot other = this.getRelative(direction).getBasePlot(false); - if (!other.equals(this.getBasePlot(false))) { - Plot base = other.id.y < this.id.y - || other.id.y == this.id.y && other.id.x < this.id.x ? other : this.origin; - this.origin.origin = base; - other.origin = base; - this.origin = base; - connected_cache = null; - } - } else { - if (this.origin != null) { - this.origin.origin = null; - this.origin = null; - } - connected_cache = null; - } - DBFunc.setMerged(this, this.getSettings().getMerged()); - regions_cache = null; - } - } - - /** - * Sets the raw merge data
- * - Updates DB
- * - Does not modify terrain
- * ----------
- * 0 = north
- * 1 = east
- * 2 = south
- * 3 = west
- * ----------
* * @param direction * @param value */ public void setMerged(Direction direction, boolean value) { - if (this.getSettings().setMerged(direction.getIndex(), value)) { + if (this.getSettings().setMerged(direction, value)) { if (value) { Plot other = this.getRelative(direction).getBasePlot(false); if (!other.equals(this.getBasePlot(false))) { @@ -2249,24 +2205,21 @@ public class Plot { } /** - * Auto merge a plot in a specific direction
+ * Auto merge a plot in a specific direction. * - * @param dir The direction to merge
- * -1 = All directions
- * 0 = north
- * 1 = east
- * 2 = south
- * 3 = west
- * @param max The max number of merges to do - * @param uuid The UUID it is allowed to merge with + * @param dir the direction to merge + * @param max the max number of merges to do + * @param uuid the UUID it is allowed to merge with * @param removeRoads whether to remove roads * @return true if a merge takes place */ - public boolean autoMerge(int dir, int max, UUID uuid, boolean removeRoads) { + public boolean autoMerge(Direction dir, int max, UUID uuid, boolean removeRoads) { + //Ignore merging if there is no owner for the plot if (this.owner == null) { return false; } - if (!EventUtil.manager.callMerge(this, dir, max)) { + //Call the merge event + if (!EventUtil.manager.callMerge(this, dir.getIndex(), max)) { return false; } Set connected = this.getConnectedPlots(); @@ -2282,7 +2235,7 @@ public class Plot { } visited.add(current); Set plots; - if ((dir == -1 || dir == 0) && !getMerged(Direction.NORTH)) { + if ((dir == Direction.ALL || dir == Direction.NORTH) && !getMerged(Direction.NORTH)) { Plot other = current.getRelative(Direction.NORTH); if (other != null && other.isOwner(uuid) && ( other.getBasePlot(false).equals(current.getBasePlot(false)) @@ -2301,7 +2254,8 @@ public class Plot { } } } - if (max >= 0 && (dir == -1 || dir == 1) && !current.getMerged(Direction.EAST)) { + if (max >= 0 && (dir == Direction.ALL || dir == Direction.EAST) && !current + .getMerged(Direction.EAST)) { Plot other = current.getRelative(Direction.EAST); if (other != null && other.isOwner(uuid) && ( other.getBasePlot(false).equals(current.getBasePlot(false)) @@ -2320,7 +2274,8 @@ public class Plot { } } } - if (max >= 0 && (dir == -1 || dir == 2) && !getMerged(Direction.SOUTH)) { + if (max >= 0 && (dir == Direction.ALL || dir == Direction.SOUTH) && !getMerged( + Direction.SOUTH)) { Plot other = current.getRelative(Direction.SOUTH); if (other != null && other.isOwner(uuid) && ( other.getBasePlot(false).equals(current.getBasePlot(false)) @@ -2339,7 +2294,8 @@ public class Plot { } } } - if (max >= 0 && (dir == -1 || dir == 3) && !getMerged(Direction.WEST)) { + if (max >= 0 && (dir == Direction.ALL || dir == Direction.WEST) && !getMerged( + Direction.WEST)) { Plot other = current.getRelative(Direction.WEST); if (other != null && other.isOwner(uuid) && ( other.getBasePlot(false).equals(current.getBasePlot(false)) @@ -2502,10 +2458,10 @@ public class Plot { // invalid merge PlotSquared.debug("Fixing invalid merge: " + this); if (tmp.isOwnerAbs(this.owner)) { - tmp.getSettings().setMerged(2, true); + tmp.getSettings().setMerged(Direction.SOUTH, true); DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); } else { - this.getSettings().setMerged(0, false); + this.getSettings().setMerged(Direction.NORTH, false); DBFunc.setMerged(this, this.getSettings().getMerged()); } } @@ -2518,10 +2474,10 @@ public class Plot { // invalid merge PlotSquared.debug("Fixing invalid merge: " + this); if (tmp.isOwnerAbs(this.owner)) { - tmp.getSettings().setMerged(3, true); + tmp.getSettings().setMerged(Direction.WEST, true); DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); } else { - this.getSettings().setMerged(1, false); + this.getSettings().setMerged(Direction.EAST, false); DBFunc.setMerged(this, this.getSettings().getMerged()); } } @@ -2534,10 +2490,10 @@ public class Plot { // invalid merge PlotSquared.debug("Fixing invalid merge: " + this); if (tmp.isOwnerAbs(this.owner)) { - tmp.getSettings().setMerged(0, true); + tmp.getSettings().setMerged(Direction.NORTH, true); DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); } else { - this.getSettings().setMerged(2, false); + this.getSettings().setMerged(Direction.SOUTH, false); DBFunc.setMerged(this, this.getSettings().getMerged()); } } @@ -2550,10 +2506,10 @@ public class Plot { // invalid merge PlotSquared.debug("Fixing invalid merge: " + this); if (tmp.isOwnerAbs(this.owner)) { - tmp.getSettings().setMerged(1, true); + tmp.getSettings().setMerged(Direction.EAST, true); DBFunc.setMerged(tmp, tmp.getSettings().getMerged()); } else { - this.getSettings().setMerged(3, false); + this.getSettings().setMerged(Direction.WEST, false); DBFunc.setMerged(this, this.getSettings().getMerged()); } } @@ -2951,6 +2907,7 @@ public class Plot { } } } + } /**