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.

This commit is contained in:
Alexander Söderberg 2020-07-18 16:08:37 +02:00 committed by Alexander Söderberg
parent 39fdaa367c
commit 5360df6012
5 changed files with 64 additions and 14 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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<PlotId> plotIds = MainUtil.getPlotSelectionIds(start, end);
final PlotId pos1 = plotIds.get(0);
final PlotAutoMergeEvent mergeEvent = this.eventDispatcher

View File

@ -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());
}
}

View File

@ -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<PlotId>, Iterable<PlotId> {
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<PlotId> iterator() {
return this;
}
}
}