From e5913faa94c4a17d67a6fea8a57354939364fac7 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 3 May 2023 11:01:31 +0200 Subject: [PATCH] make autoMerge way more readable --- .../core/plot/PlotModificationManager.java | 82 +++++-------------- 1 file changed, 19 insertions(+), 63 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java b/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java index ed764444d..cd307fee2 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java @@ -549,6 +549,8 @@ public final class PlotModificationManager { return false; } + private static final Direction[] DIRECTIONS = {Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST}; + /** * Auto merge a plot in a specific direction. * @@ -583,71 +585,25 @@ public final class PlotModificationManager { } visited.add(current); Set plots; - if ((dir == Direction.ALL || dir == Direction.NORTH) && !current.isMerged(Direction.NORTH)) { - Plot other = current.getRelative(Direction.NORTH); - if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) - || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) { - current.mergePlot(other, removeRoads, queue); - merged.add(current.getId()); - merged.add(other.getId()); - toReturn = true; - - if (removeRoads) { - ArrayList ids = new ArrayList<>(); - ids.add(current.getId()); - ids.add(other.getId()); - this.plot.getManager().finishPlotMerge(ids, queue); - } + for (final Direction direction : DIRECTIONS) { + if (max <= 0) { + break; } - } - if (max >= 0 && (dir == Direction.ALL || dir == Direction.EAST) && !current.isMerged(Direction.EAST)) { - Plot other = current.getRelative(Direction.EAST); - if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) - || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) { - current.mergePlot(other, removeRoads, queue); - merged.add(current.getId()); - merged.add(other.getId()); - toReturn = true; + if ((dir == Direction.ALL || dir == direction) && !current.isMerged(direction)) { + Plot other = current.getRelative(direction); + if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) + || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) { + current.mergePlot(other, removeRoads, queue); + merged.add(current.getId()); + merged.add(other.getId()); + toReturn = true; - if (removeRoads) { - ArrayList ids = new ArrayList<>(); - ids.add(current.getId()); - ids.add(other.getId()); - this.plot.getManager().finishPlotMerge(ids, queue); - } - } - } - if (max >= 0 && (dir == Direction.ALL || dir == Direction.SOUTH) && !current.isMerged(Direction.SOUTH)) { - Plot other = current.getRelative(Direction.SOUTH); - if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) - || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) { - current.mergePlot(other, removeRoads, queue); - merged.add(current.getId()); - merged.add(other.getId()); - toReturn = true; - - if (removeRoads) { - ArrayList ids = new ArrayList<>(); - ids.add(current.getId()); - ids.add(other.getId()); - this.plot.getManager().finishPlotMerge(ids, queue); - } - } - } - if (max >= 0 && (dir == Direction.ALL || dir == Direction.WEST) && !current.isMerged(Direction.WEST)) { - Plot other = current.getRelative(Direction.WEST); - if (other != null && other.isOwner(uuid) && (other.getBasePlot(false).equals(current.getBasePlot(false)) - || (plots = other.getConnectedPlots()).size() <= max && frontier.addAll(plots) && (max -= plots.size()) != -1)) { - current.mergePlot(other, removeRoads, queue); - merged.add(current.getId()); - merged.add(other.getId()); - toReturn = true; - - if (removeRoads) { - ArrayList ids = new ArrayList<>(); - ids.add(current.getId()); - ids.add(other.getId()); - this.plot.getManager().finishPlotMerge(ids, queue); + if (removeRoads) { + ArrayList ids = new ArrayList<>(); + ids.add(current.getId()); + ids.add(other.getId()); + this.plot.getManager().finishPlotMerge(ids, queue); + } } } }