From 5360df601235785d82636b77f072a2e66746251c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 18 Jul 2020 16:08:37 +0200 Subject: [PATCH] Add PlotRangeIterator for the many places where it'll be used. All one of them, in fact. This was not a waste of time. I am very happy I did this. This was worthwhile. Yup. --- .../entity/ReplicatingEntityWrapper.java | 6 +-- .../bukkit/entity/TeleportEntityWrapper.java | 2 +- .../com/plotsquared/core/command/Auto.java | 15 +++--- .../plotsquared/core/location/PlotLoc.java | 5 +- .../com/plotsquared/core/plot/PlotId.java | 50 +++++++++++++++++++ 5 files changed, 64 insertions(+), 14 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/entity/ReplicatingEntityWrapper.java b/Bukkit/src/main/java/com/plotsquared/bukkit/entity/ReplicatingEntityWrapper.java index 8cae4cef4..74dfbdf07 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/entity/ReplicatingEntityWrapper.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/entity/ReplicatingEntityWrapper.java @@ -154,7 +154,7 @@ public final class ReplicatingEntityWrapper extends EntityWrapper { case "ITEM_FRAME": this.x = Math.floor(this.getX()); this.y = Math.floor(this.getY()); - this.z = Math.floor(this.z); + this.z = Math.floor(this.getZ()); ItemFrame itemFrame = (ItemFrame) entity; this.dataByte = getOrdinal(Rotation.values(), itemFrame.getRotation()); this.stack = itemFrame.getItem().clone(); @@ -162,7 +162,7 @@ public final class ReplicatingEntityWrapper extends EntityWrapper { case "PAINTING": this.x = Math.floor(this.getX()); this.y = Math.floor(this.getY()); - this.z = Math.floor(this.z); + this.z = Math.floor(this.getZ()); Painting painting = (Painting) entity; Art art = painting.getArt(); this.dataByte = getOrdinal(BlockFace.values(), painting.getFacing()); @@ -409,7 +409,7 @@ public final class ReplicatingEntityWrapper extends EntityWrapper { Location location = lived.getLeashHolder().getLocation(); this.lived.leashX = (short) (this.getX() - location.getBlockX()); this.lived.leashY = (short) (this.getY() - location.getBlockY()); - this.lived.leashZ = (short) (this.z - location.getBlockZ()); + this.lived.leashZ = (short) (this.getZ() - location.getBlockZ()); } EntityEquipment equipment = lived.getEquipment(); this.lived.equipped = equipment != null; diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/entity/TeleportEntityWrapper.java b/Bukkit/src/main/java/com/plotsquared/bukkit/entity/TeleportEntityWrapper.java index dfa918c0b..2393d2fa4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/entity/TeleportEntityWrapper.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/entity/TeleportEntityWrapper.java @@ -69,7 +69,7 @@ public class TeleportEntityWrapper extends EntityWrapper { this.oldLocation = oldLocation.clone(); this.oldLocation.setX(this.getX()); this.oldLocation.setY(this.getY()); - this.oldLocation.setZ(this.z); + this.oldLocation.setZ(this.getZ()); this.gravityOld = this.getEntity().hasGravity(); this.getEntity().setGravity(false); diff --git a/Core/src/main/java/com/plotsquared/core/command/Auto.java b/Core/src/main/java/com/plotsquared/core/command/Auto.java index cfc7cd94d..fd66fa848 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -298,16 +298,15 @@ public class Auto extends SubCommand { PlotId end = PlotId.of(start.getX() + size_x - 1, start.getY() + size_z - 1); if (plotarea.canClaim(player, start, end)) { plotarea.setMeta("lastPlot", start); - for (int i = start.getX(); i <= end.getX(); i++) { - for (int j = start.getY(); j <= end.getY(); j++) { - Plot plot = plotarea.getPlotAbs(PlotId.of(i, j)); - boolean teleport = i == end.getX() && j == end.getY(); - if (plot == null) { - return false; - } - plot.claim(player, teleport, null); + + for (final PlotId plotId : PlotId.PlotRangeIterator.range(start, end)) { + final Plot plot = plotarea.getPlot(plotId); + if (plot == null) { + return false; } + plot.claim(player, plotId.equals(end), null); } + ArrayList plotIds = MainUtil.getPlotSelectionIds(start, end); final PlotId pos1 = plotIds.get(0); final PlotAutoMergeEvent mergeEvent = this.eventDispatcher diff --git a/Core/src/main/java/com/plotsquared/core/location/PlotLoc.java b/Core/src/main/java/com/plotsquared/core/location/PlotLoc.java index 76135262d..4db83bace 100644 --- a/Core/src/main/java/com/plotsquared/core/location/PlotLoc.java +++ b/Core/src/main/java/com/plotsquared/core/location/PlotLoc.java @@ -92,7 +92,7 @@ public final class PlotLoc { int result = 1; result = (prime * result) + this.getX(); result = (prime * result) + this.getY(); - result = (prime * result) + this.z; + result = (prime * result) + this.getZ(); return result; } @@ -111,6 +111,7 @@ public final class PlotLoc { return false; } final PlotLoc other = (PlotLoc) obj; - return (this.getX() == other.getX()) && (this.getY() == other.getY()) && (this.z == other.z); + return (this.getX() == other.getX()) && (this.getY() == + other.getY()) && (this.getZ() == other.getZ()); } } diff --git a/Core/src/main/java/com/plotsquared/core/plot/PlotId.java b/Core/src/main/java/com/plotsquared/core/plot/PlotId.java index 64c28a2d5..27004ba3c 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/PlotId.java +++ b/Core/src/main/java/com/plotsquared/core/plot/PlotId.java @@ -30,6 +30,7 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Iterator; /** * Plot (X,Y) tuples for plot locations @@ -242,4 +243,53 @@ public class PlotId { return this.hash; } + + public static final class PlotRangeIterator implements Iterator, Iterable { + + private final PlotId start; + private final PlotId end; + + private int x; + private int y; + + private PlotRangeIterator(@Nonnull final PlotId start, @Nonnull final PlotId end) { + this.start = start; + this.end = end; + this.x = this.start.getX(); + this.y = this.start.getY(); + } + + public static PlotRangeIterator range(@Nonnull final PlotId start, @Nonnull final PlotId end) { + return new PlotRangeIterator(start, end); + } + + @Override public boolean hasNext() { + if (this.x < this.end.getX()) { + return true; + } else if (this.x == this.end.getX()) { + return this.y < this.end.getY(); + } else { + return false; + } + } + + @Override public PlotId next() { + if (!hasNext()) { + throw new IndexOutOfBoundsException("The iterator has no more entries"); + } + if (this.y == this.end.getY()) { + this.x++; + this.y = 0; + } else { + this.y++; + } + return PlotId.of(this.start.getX() + this.x, this.start.getY() + this.y); + } + + @Nonnull @Override public Iterator iterator() { + return this; + } + + } + }