From cf1b027db9492d097f06d34b3aa0319f369f4783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Thu, 9 Jul 2020 22:28:46 +0200 Subject: [PATCH] Start making locations immutable --- .../com/plotsquared/core/command/Set.java | 8 +- .../core/listener/WESubscriber.java | 5 +- .../plotsquared/core/location/Location.java | 368 +++++++++++------- .../plotsquared/core/player/PlotPlayer.java | 2 +- .../java/com/plotsquared/core/plot/Plot.java | 83 ++-- .../core/queue/LocalBlockQueue.java | 10 +- .../core/util/SchematicHandler.java | 17 +- .../core/plot/object/LocationTest.java | 47 --- 8 files changed, 294 insertions(+), 246 deletions(-) delete mode 100644 Core/src/test/java/com/plotsquared/core/plot/object/LocationTest.java diff --git a/Core/src/main/java/com/plotsquared/core/command/Set.java b/Core/src/main/java/com/plotsquared/core/command/Set.java index ec75fd9eb..3a06ca6e2 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Set.java +++ b/Core/src/main/java/com/plotsquared/core/command/Set.java @@ -31,6 +31,7 @@ import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotManager; import com.plotsquared.core.queue.GlobalBlockQueue; import com.plotsquared.core.util.MainUtil; @@ -75,7 +76,12 @@ public class Set extends SubCommand { } @Override public boolean set(PlotPlayer player, final Plot plot, String value) { - PlotManager manager = player.getLocation().getPlotManager(); + final PlotArea plotArea = player.getLocation().getPlotArea(); + if (plotArea == null) { + return false; + } + final PlotManager manager = plotArea.getPlotManager(); + String[] components = manager.getPlotComponents(plot.getId()); String[] args = value.split(" "); diff --git a/Core/src/main/java/com/plotsquared/core/listener/WESubscriber.java b/Core/src/main/java/com/plotsquared/core/listener/WESubscriber.java index fcb1219e4..d2ea15dba 100644 --- a/Core/src/main/java/com/plotsquared/core/listener/WESubscriber.java +++ b/Core/src/main/java/com/plotsquared/core/listener/WESubscriber.java @@ -66,9 +66,8 @@ public class WESubscriber { if (plotPlayer == null) { Player player = (Player) actor; Location location = player.getLocation(); - com.plotsquared.core.location.Location pLoc = - new com.plotsquared.core.location.Location(player.getWorld().getName(), - location.getBlockX(), location.getBlockX(), location.getBlockZ()); + com.plotsquared.core.location.Location pLoc = com.plotsquared.core.location.Location.at(player.getWorld().getName(), + location.toVector().toBlockPoint()); Plot plot = pLoc.getPlot(); if (plot == null) { event.setExtent(new NullExtent()); diff --git a/Core/src/main/java/com/plotsquared/core/location/Location.java b/Core/src/main/java/com/plotsquared/core/location/Location.java index 29c9a30fa..c27ac6d5a 100644 --- a/Core/src/main/java/com/plotsquared/core/location/Location.java +++ b/Core/src/main/java/com/plotsquared/core/location/Location.java @@ -25,91 +25,137 @@ */ package com.plotsquared.core.location; +import com.google.common.base.Preconditions; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; -import com.plotsquared.core.plot.PlotManager; -import com.plotsquared.core.util.MathMan; import com.sk89q.worldedit.math.BlockVector2; import com.sk89q.worldedit.math.BlockVector3; +import lombok.EqualsAndHashCode; import lombok.Getter; -import lombok.Setter; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.khelekore.prtree.MBR; import org.khelekore.prtree.SimpleMBR; -public class Location implements Cloneable, Comparable { +/** + * An unmodifiable 6-tuple (world,x,y,z,yaw,pitch) + */ +@EqualsAndHashCode +public final class Location implements Comparable { - private int x; - private int y; - private int z; - @Getter @Setter private float yaw; - @Getter @Setter private float pitch; - @Getter @Setter private String world; - @Getter private BlockVector3 blockVector3; + @Getter private final float yaw; + @Getter private final float pitch; + @Getter private final String world; + @Getter private final BlockVector3 blockVector3; - public Location(String world, int x, int y, int z, float yaw, float pitch) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; + private Location(@NotNull final String world, @NotNull final BlockVector3 blockVector3, + final float yaw, final float pitch) { + this.world = Preconditions.checkNotNull(world, "World may not be null"); + this.blockVector3 = Preconditions.checkNotNull(blockVector3, "Vector may not be null"); this.yaw = yaw; this.pitch = pitch; - this.blockVector3 = BlockVector3.at(x, y, z); } - public Location(String world, int x, int y, int z) { - this(world, x, y, z, 0f, 0f); + /** + * Construct a new location + * + * @param world World + * @param blockVector3 (x,y,z) vector + * @param yaw yaw + * @param pitch pitch + * @return New location + */ + @NotNull public static Location at(@NotNull final String world, + @NotNull final BlockVector3 blockVector3, final float yaw, final float pitch) { + return new Location(world, blockVector3, yaw, pitch); } + /** + * Construct a new location with yaw and pitch equal to 0 + * + * @param world World + * @param blockVector3 (x,y,z) vector + * @return New location + */ + @NotNull public static Location at(@NotNull final String world, + @NotNull final BlockVector3 blockVector3) { + return at(world, blockVector3, 0f, 0f); + } + + /** + * Construct a new location + * + * @param world World + * @param x X coordinate + * @param y Y coordinate + * @param z Z coordinate + * @param yaw Yaw + * @param pitch Pitch + * @return New location + */ + @NotNull public static Location at(@NotNull final String world, final int x, final int y, + final int z, final float yaw, final float pitch) { + return at(world, BlockVector3.at(x, y, z), yaw, pitch); + } + + /** + * Construct a new location with yaw and pitch equal to 0 + * + * @param world World + * @param x X coordinate + * @param y Y coordinate + * @param z Z coordinate + * @return New location + */ + @NotNull public static Location at(@NotNull final String world, final int x, final int y, + final int z) { + return at(world, BlockVector3.at(x, y, z)); + } + + /** + * Get the X coordinate + * + * @return X coordinate + */ public int getX() { - return this.x; - } - - public void setX(int x) { - this.x = x; - this.blockVector3 = BlockVector3.at(x, y, z); + return this.blockVector3.getBlockX(); } + /** + * Get the Y coordinate + * + * @return Y coordinate + */ public int getY() { - return this.y; - } - - public void setY(int y) { - this.y = y; - this.blockVector3 = BlockVector3.at(x, y, z); + return this.blockVector3.getY(); } + /** + * Get the Z coordinate + * + * @return Z coordinate + */ public int getZ() { - return this.z; - } - - public void setZ(int z) { - this.z = z; - this.blockVector3 = BlockVector3.at(x, y, z); - } - - public void setBlockVector3(BlockVector3 blockVector3) { - this.blockVector3 = blockVector3; - this.x = blockVector3.getX(); - this.y = blockVector3.getY(); - this.z = blockVector3.getZ(); - } - - @Override public Location clone() { - try { - return (Location) super.clone(); - } catch (CloneNotSupportedException e) { - throw new AssertionError(); //can't happen - } + return this.blockVector3.getZ(); } + /** + * Get the {@link PlotArea}, if any, that contains this location + * + * @return Plot area containing the location, or {@code null} + */ @Nullable public PlotArea getPlotArea() { return PlotSquared.get().getPlotAreaManager().getPlotArea(this); } - public Plot getOwnedPlot() { - PlotArea area = getPlotArea(); + /** + * Get the owned {@link Plot}, if any, that contains this location + * + * @return Plot containing the location, or {@code null} + */ + @Nullable public Plot getOwnedPlot() { + final PlotArea area = this.getPlotArea(); if (area != null) { return area.getOwnedPlot(this); } else { @@ -117,8 +163,13 @@ public class Location implements Cloneable, Comparable { } } - public Plot getOwnedPlotAbs() { - PlotArea area = getPlotArea(); + /** + * Get the (absolute) owned {@link Plot}, if any, that contains this location + * + * @return (Absolute) plot containing the location, or {@code null} + */ + @Nullable public Plot getOwnedPlotAbs() { + final PlotArea area = this.getPlotArea(); if (area != null) { return area.getOwnedPlotAbs(this); } else { @@ -126,36 +177,42 @@ public class Location implements Cloneable, Comparable { } } + /** + * Check whether or not the location belongs to a plot area + * + * @return {@code true} if the location belongs to a plot area, else {@link false} + */ public boolean isPlotArea() { - return getPlotArea() != null; + return this.getPlotArea() != null; } + /** + * Check whether or not the location belongs to a plot road + * + * @return {@code true} if the location belongs to a plot road, else {@link false} + */ public boolean isPlotRoad() { - PlotArea area = getPlotArea(); + final PlotArea area = this.getPlotArea(); return area != null && area.getPlotAbs(this) == null; } /** * Checks if anyone owns a plot at the current location. * - * @return true if the location is a road, not a plot area, or if the plot is unclaimed. + * @return {@link true} if the location is a road, not a plot area, or if the plot is unclaimed. */ public boolean isUnownedPlotArea() { - PlotArea area = getPlotArea(); + final PlotArea area = this.getPlotArea(); return area != null && area.getOwnedPlotAbs(this) == null; } - public PlotManager getPlotManager() { - PlotArea pa = getPlotArea(); - if (pa != null) { - return pa.getPlotManager(); - } else { - return null; - } - } - - public Plot getPlotAbs() { - PlotArea area = getPlotArea(); + /** + * Get the absolute {@link Plot}, if any, that contains this location + * + * @return (Absolute) plot containing the location, or {code null} + */ + @Nullable public Plot getPlotAbs() { + final PlotArea area = this.getPlotArea(); if (area != null) { return area.getPlotAbs(this); } else { @@ -163,8 +220,13 @@ public class Location implements Cloneable, Comparable { } } - public Plot getPlot() { - PlotArea area = getPlotArea(); + /** + * Get the {@link Plot}, if any, that contains this location + * + * @return plot containing the location, or {code null} + */ + @Nullable public Plot getPlot() { + final PlotArea area = this.getPlotArea(); if (area != null) { return area.getPlot(this); } else { @@ -172,87 +234,133 @@ public class Location implements Cloneable, Comparable { } } - public BlockVector2 getBlockVector2() { - return BlockVector2.at(this.x >> 4, this.z >> 4); + /** + * Get the coordinates of the chunk that contains this location + * + * @return Chunk coordinates + */ + @NotNull public BlockVector2 getChunkLocation() { + return BlockVector2.at(this.getX() >> 4, this.getZ() >> 4); } - public Location add(int x, int y, int z) { - this.x += x; - this.y += y; - this.z += z; - return this; + /** + * Return a new location offset by the given coordinates + * + * @param x X offset + * @param y Y offset + * @param z Z offset + * @return New location + */ + @NotNull public Location add(final int x, final int y, final int z) { + return new Location(this.world, this.blockVector3.add(x, y, z), this.yaw, this.pitch); } - public double getEuclideanDistanceSquared(Location l2) { + /** + * Return a new location using the given X coordinate + * + * @param x New X coordinate + * @return New location + */ + @NotNull public Location withX(final int x) { + return new Location(this.world, this.blockVector3.withX(x), this.yaw, this.pitch); + } + + /** + * Return a new location using the given Y coordinate + * + * @param y New Y coordinate + * @return New location + */ + @NotNull public Location withY(final int y) { + return new Location(this.world, this.blockVector3.withY(y), this.yaw, this.pitch); + } + + /** + * Return a new location using the given Z coordinate + * + * @param z New Z coordinate + * @return New location + */ + @NotNull public Location withZ(final int z) { + return new Location(this.world, this.blockVector3.withZ(z), this.yaw, this.pitch); + } + + /** + * Return a new location using the given yaw + * + * @param yaw New yaw + * @return New location + */ + @NotNull public Location withYaw(final float yaw) { + return new Location(this.world, this.blockVector3, yaw, this.pitch); + } + + /** + * Return a new location using the given pitch + * + * @param pitch New pitch + * @return New location + */ + @NotNull public Location withPitch(final float pitch) { + return new Location(this.world, this.blockVector3, this.yaw, pitch); + } + + /** + * Return a new location using the given world + * + * @param world New world + * @return New location + */ + @NotNull public Location withWorld(@NotNull final String world) { + return new Location(world, this.blockVector3, this.yaw, this.pitch); + } + + public double getEuclideanDistanceSquared(@NotNull final Location l2) { double x = getX() - l2.getX(); double y = getY() - l2.getY(); double z = getZ() - l2.getZ(); return x * x + y * y + z * z; } - public double getEuclideanDistance(Location l2) { + public double getEuclideanDistance(@NotNull final Location l2) { return Math.sqrt(getEuclideanDistanceSquared(l2)); } - public boolean isInSphere(Location origin, int radius) { - return getEuclideanDistanceSquared(origin) < radius * radius; + /** + * Return a new location offset by (-) the given coordinates + * + * @param x X offset + * @param y Y offset + * @param z Z offset + * @return New location + */ + @NotNull public Location subtract(int x, int y, int z) { + return this.add(-x, -y, -z); } - @Override public int hashCode() { - return MathMan.pair((short) this.x, (short) this.z) * 17 + this.y; + /** + * Get a minimum bounding rectangle that contains this location only + * + * @return Minimum bounding rectangle + */ + @NotNull public MBR toMBR() { + return new SimpleMBR(this.getX(), this.getX(), this.getY(), this.getY(), this.getZ(), + this.getZ()); } - public boolean isInAABB(Location min, Location max) { - return this.x >= min.getX() && this.x <= max.getX() && this.y >= min.getY() && this.y <= max - .getY() && this.z >= min.getX() && this.z < max.getZ(); - } - - public void lookTowards(int x, int y) { - double l = this.x - x; - double c = Math.sqrt(l * l + 0.0); - if (Math.asin(0 / c) / Math.PI * 180 > 90) { - setYaw((float) (180 - -Math.asin(l / c) / Math.PI * 180)); - } else { - setYaw((float) (-Math.asin(l / c) / Math.PI * 180)); - } - } - - public Location subtract(int x, int y, int z) { - this.x -= x; - this.y -= y; - this.z -= z; - return this; - } - - public MBR toMBR() { - return new SimpleMBR(this.getX(), this.getX(), this.getY(), this.getY(), this.getZ(), this.getZ()); - } - - @Override public boolean equals(Object o) { - if (o == null) { - return false; - } - if (!(o instanceof Location)) { - return false; - } - Location l = (Location) o; - return this.x == l.getX() && this.y == l.getY() && this.z == l.getZ() && this.world - .equals(l.getWorld()) && this.yaw == l.getYaw() && this.pitch == l.getPitch(); - } - - @Override public int compareTo(Location o) { - if (this.x == o.getX() && this.y == o.getY() || this.z == o.getZ()) { + @Override public int compareTo(@NotNull final Location o) { + if (this.getX() == o.getX() && this.getY() == o.getY() || this.getZ() == o.getZ()) { return 0; } - if (this.x < o.getX() && this.y < o.getY() && this.z < o.getZ()) { + if (this.getX() < o.getX() && this.getY() < o.getY() && this.getZ() < o.getZ()) { return -1; } return 1; } @Override public String toString() { - return "\"plotsquaredlocation\":{\"x\":" + this.x + ",\"y\":" + this.y + ",\"z\":" + this.z - + ",\"yaw\":" + this.yaw + ",\"pitch\":" + this.pitch + ",\"world\":\"" + this.world - + "\"}"; + return "\"plotsquaredlocation\":{\"x\":" + this.getX() + ",\"y\":" + this.getY() + ",\"z\":" + + this.getZ() + ",\"yaw\":" + this.yaw + ",\"pitch\":" + this.pitch + ",\"world\":\"" + + this.world + "\"}"; } } diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index 1f2533dc2..800e2d875 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -673,7 +673,7 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer return; } - final Location location = new Location(plot.getWorldName(), x, y, z); + final Location location = Location.at(plot.getWorldName(), x, y, z); if (plot.isLoaded()) { TaskManager.runTask(() -> { if (getMeta("teleportOnLogin", true)) { diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index c1a03a660..dc6449d80 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -1359,8 +1359,7 @@ public class Plot { Location[] corners = getCorners(); Location top = corners[0]; Location bot = corners[1]; - Location location = - new Location(this.getWorldName(), MathMan.average(bot.getX(), top.getX()), + Location location = Location.at(this.getWorldName(), MathMan.average(bot.getX(), top.getX()), MathMan.average(bot.getY(), top.getY()), MathMan.average(bot.getZ(), top.getZ())); if (!isLoaded()) { result.accept(location); @@ -1371,8 +1370,7 @@ public class Plot { if (area.allowSigns()) { height = Math.max(y, getManager().getSignLoc(this).getY()); } - location.setY(1 + height); - result.accept(location); + result.accept(location.withY(1 + height)); }); } @@ -1383,9 +1381,8 @@ public class Plot { Location[] corners = getCorners(); Location top = corners[0]; Location bot = corners[1]; - Location location = - new Location(this.getWorldName(), MathMan.average(bot.getX(), top.getX()), - MathMan.average(bot.getY(), top.getY()), MathMan.average(bot.getZ(), top.getZ())); + Location location = Location.at(this.getWorldName(), MathMan.average(bot.getX(), top.getX()), + MathMan.average(bot.getY(), top.getY()), MathMan.average(bot.getZ(), top.getZ())); if (!isLoaded()) { return location; } @@ -1394,8 +1391,7 @@ public class Plot { if (area.allowSigns()) { y = Math.max(y, getManager().getSignLoc(this).getY()); } - location.setY(1 + y); - return location; + return location.withY(1 + y); } /** @@ -1411,7 +1407,7 @@ public class Plot { if (area.allowSigns() && (y <= 0 || y >= 255)) { y = Math.max(y, manager.getSignLoc(this).getY() - 1); } - return new Location(getWorldName(), x, y + 1, z); + return Location.at(getWorldName(), x, y + 1, z); } public void getSide(Consumer result) { @@ -1426,14 +1422,14 @@ public class Plot { if (area.allowSigns() && (y <= 0 || y >= 255)) { height = Math.max(y, manager.getSignLoc(this).getY() - 1); } - result.accept(new Location(getWorldName(), x, height + 1, z)); + result.accept(Location.at(getWorldName(), x, height + 1, z)); }); } else { int y = 62; if (area.allowSigns()) { y = Math.max(y, manager.getSignLoc(this).getY() - 1); } - result.accept(new Location(getWorldName(), x, y + 1, z)); + result.accept(Location.at(getWorldName(), x, y + 1, z)); } } @@ -1446,14 +1442,14 @@ public class Plot { return this.getDefaultHomeSynchronous(true); } else { Location bottom = this.getBottomAbs(); - Location location = new Location(bottom.getWorld(), bottom.getX() + home.getX(), + Location location = Location.at(bottom.getWorld(), bottom.getX() + home.getX(), bottom.getY() + home.getY(), bottom.getZ() + home.getZ(), home.getYaw(), home.getPitch()); if (!isLoaded()) { return location; } if (!WorldUtil.IMP.getBlockSynchronous(location).getBlockType().getMaterial().isAir()) { - location.setY(Math.max(1 + WorldUtil.IMP + location = location.withY(Math.max(1 + WorldUtil.IMP .getHighestBlockSynchronous(this.getWorldName(), location.getX(), location.getZ()), bottom.getY())); } @@ -1470,7 +1466,7 @@ public class Plot { this.getDefaultHome(result); } else { Location bottom = this.getBottomAbs(); - Location location = new Location(bottom.getWorld(), bottom.getX() + home.getX(), + Location location = Location.at(bottom.getWorld(), bottom.getX() + home.getX(), bottom.getY() + home.getY(), bottom.getZ() + home.getZ(), home.getYaw(), home.getPitch()); if (!isLoaded()) { @@ -1481,10 +1477,7 @@ public class Plot { if (!block.getBlockType().getMaterial().isAir()) { WorldUtil.IMP .getHighestBlock(this.getWorldName(), location.getX(), location.getZ(), - y -> { - location.setY(Math.max(1 + y, bottom.getY())); - result.accept(location); - }); + y -> result.accept(location.withY(Math.max(1 + y, bottom.getY())))); } else { result.accept(location); } @@ -1545,7 +1538,7 @@ public class Plot { WorldUtil.IMP.getHighestBlockSynchronous(plot.getWorldName(), x, z) + 1 : 63) : loc.getY(); - return new Location(plot.getWorldName(), x, y, z); + return Location.at(plot.getWorldName(), x, y, z); } // Side return plot.getSideSynchronous(); @@ -1573,12 +1566,12 @@ public class Plot { if (loc.getY() < 1) { if (isLoaded()) { WorldUtil.IMP.getHighestBlock(plot.getWorldName(), x, z, - y -> result.accept(new Location(plot.getWorldName(), x, y + 1, z))); + y -> result.accept(Location.at(plot.getWorldName(), x, y + 1, z))); } else { - result.accept(new Location(plot.getWorldName(), x, 63, z)); + result.accept(Location.at(plot.getWorldName(), x, 63, z)); } } else { - result.accept(new Location(plot.getWorldName(), x, loc.getY(), z)); + result.accept(Location.at(plot.getWorldName(), x, loc.getY(), z)); } return; } @@ -1875,9 +1868,7 @@ public class Plot { * Returns the top location for the plot. */ public Location getTopAbs() { - Location top = getManager().getPlotTopLocAbs(this.id); - top.setWorld(getWorldName()); - return top; + return this.getManager().getPlotTopLocAbs(this.id).withWorld(this.getWorldName()); } //TODO Better documentation needed. @@ -1886,9 +1877,7 @@ public class Plot { * Returns the bottom location for the plot. */ public Location getBottomAbs() { - Location location = getManager().getPlotBottomLocAbs(this.id); - location.setWorld(getWorldName()); - return location; + return this.getManager().getPlotBottomLocAbs(this.id).withWorld(this.getWorldName()); } /** @@ -1965,10 +1954,10 @@ public class Plot { return top; } if (this.getMerged(Direction.SOUTH)) { - top.setZ(this.getRelative(Direction.SOUTH).getBottomAbs().getZ() - 1); + top = top.withZ(this.getRelative(Direction.SOUTH).getBottomAbs().getZ() - 1); } if (this.getMerged(Direction.EAST)) { - top.setX(this.getRelative(Direction.EAST).getBottomAbs().getX() - 1); + top = top.withX(this.getRelative(Direction.EAST).getBottomAbs().getX() - 1); } return top; } @@ -1986,10 +1975,10 @@ public class Plot { return bot; } if (this.getMerged(Direction.NORTH)) { - bot.setZ(this.getRelative(Direction.NORTH).getTopAbs().getZ() + 1); + bot = bot.withZ(this.getRelative(Direction.NORTH).getTopAbs().getZ() + 1); } if (this.getMerged(Direction.WEST)) { - bot.setX(this.getRelative(Direction.WEST).getTopAbs().getX() + 1); + bot = bot.withX(this.getRelative(Direction.WEST).getTopAbs().getX() + 1); } return bot; } @@ -2019,8 +2008,8 @@ public class Plot { Plot other = this.getRelative(Direction.EAST); Location bot = other.getBottomAbs(); Location top = this.getTopAbs(); - Location pos1 = new Location(this.getWorldName(), top.getX(), 0, bot.getZ()); - Location pos2 = new Location(this.getWorldName(), bot.getX(), MAX_HEIGHT, top.getZ()); + Location pos1 = Location.at(this.getWorldName(), top.getX(), 0, bot.getZ()); + Location pos2 = Location.at(this.getWorldName(), bot.getX(), MAX_HEIGHT, top.getZ()); RegionManager.manager.regenerateRegion(pos1, pos2, true, null); } else if (this.area.getTerrain() != PlotAreaTerrainType.ALL) { // no road generated => no road to remove @@ -2391,8 +2380,8 @@ public class Plot { Plot other = this.getRelative(Direction.SOUTH); Location bot = other.getBottomAbs(); Location top = this.getTopAbs(); - Location pos1 = new Location(this.getWorldName(), bot.getX(), 0, top.getZ()); - Location pos2 = new Location(this.getWorldName(), top.getX(), MAX_HEIGHT, bot.getZ()); + Location pos1 = Location.at(this.getWorldName(), bot.getX(), 0, top.getZ()); + Location pos2 = Location.at(this.getWorldName(), top.getX(), MAX_HEIGHT, bot.getZ()); RegionManager.manager.regenerateRegion(pos1, pos2, true, null); } else if (this.area.getTerrain() != PlotAreaTerrainType.ALL) { // no road generated => no road to remove @@ -2567,10 +2556,8 @@ public class Plot { if (this.area.getType() != PlotAreaType.NORMAL && this.area.getTerrain() == PlotAreaTerrainType.ROAD) { Plot other = this.getRelative(1, 1); - Location pos1 = this.getTopAbs().add(1, 0, 1); - Location pos2 = other.getBottomAbs().subtract(1, 0, 1); - pos1.setY(0); - pos2.setY(MAX_HEIGHT); + Location pos1 = this.getTopAbs().add(1, 0, 1).withY(0); + Location pos2 = other.getBottomAbs().subtract(1, 0, 1).withY(MAX_HEIGHT); RegionManager.manager.regenerateRegion(pos1, pos2, true, null); } else if (this.area.getTerrain() != PlotAreaTerrainType.ALL) { // no road generated => no road to remove @@ -2963,7 +2950,7 @@ public class Plot { int x = (int) MathMan.inverseRound(coords[0]); int z = (int) MathMan.inverseRound(coords[1]); if (type != 4) { - locs.add(new Location(this.getWorldName(), x, 0, z)); + locs.add(Location.at(this.getWorldName(), x, 0, z)); } } return locs; @@ -3229,10 +3216,8 @@ public class Plot { Location[] corners = MainUtil.getCorners(getWorldName(), region); Location pos1 = corners[0]; Location pos2 = corners[1]; - Location pos3 = pos1.clone().add(offsetX, 0, offsetZ); - Location pos4 = pos2.clone().add(offsetX, 0, offsetZ); - pos3.setWorld(destination.getWorldName()); - pos4.setWorld(destination.getWorldName()); + Location pos3 = pos1.add(offsetX, 0, offsetZ).withWorld(destination.getWorldName()); + Location pos4 = pos2.add(offsetX, 0, offsetZ).withWorld(destination.getWorldName()); RegionManager.manager.swap(pos1, pos2, pos3, pos4, this); } } @@ -3263,8 +3248,7 @@ public class Plot { Location[] corners = MainUtil.getCorners(getWorldName(), region); final Location pos1 = corners[0]; final Location pos2 = corners[1]; - Location newPos = pos1.clone().add(offsetX, 0, offsetZ); - newPos.setWorld(destination.getWorldName()); + Location newPos = pos1.add(offsetX, 0, offsetZ).withWorld(destination.getWorldName()); RegionManager.manager.copyRegion(pos1, pos2, newPos, task); } }.run(); @@ -3358,8 +3342,7 @@ public class Plot { Location[] corners = MainUtil.getCorners(getWorldName(), region); Location pos1 = corners[0]; Location pos2 = corners[1]; - Location newPos = pos1.clone().add(offsetX, 0, offsetZ); - newPos.setWorld(destination.getWorldName()); + Location newPos = pos1 .add(offsetX, 0, offsetZ).withWorld(destination.getWorldName()); RegionManager.manager.copyRegion(pos1, pos2, newPos, this); } }; diff --git a/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java b/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java index 9f270ff32..fc55f8942 100644 --- a/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java +++ b/Core/src/main/java/com/plotsquared/core/queue/LocalBlockQueue.java @@ -59,8 +59,8 @@ public abstract class LocalBlockQueue { public ScopedLocalBlockQueue getForChunk(int x, int z) { int bx = x << 4; int bz = z << 4; - return new ScopedLocalBlockQueue(this, new Location(getWorld(), bx, 0, bz), - new Location(getWorld(), bx + 15, 255, bz + 15)); + return new ScopedLocalBlockQueue(this, Location.at(getWorld(), bx, 0, bz), + Location.at(getWorld(), bx + 15, 255, bz + 255)); } public abstract boolean next(); @@ -125,13 +125,11 @@ public abstract class LocalBlockQueue { for (final PlotPlayer pp : PlotSquared.platform().getPlayerManager().getPlayers()) { Location pLoc = pp.getLocation(); - if (!StringMan.isEqual(getWorld(), pLoc.getWorld()) || !pLoc.getBlockVector2() + if (!StringMan.isEqual(getWorld(), pLoc.getWorld()) || !pLoc.getChunkLocation() .equals(loc)) { continue; } - pLoc.setY( - WorldUtil.IMP.getHighestBlockSynchronous(getWorld(), pLoc.getX(), pLoc.getZ())); - pp.teleport(pLoc); + pp.teleport(pLoc.withY(WorldUtil.IMP.getHighestBlockSynchronous(getWorld(), pLoc.getX(), pLoc.getZ()))); } } diff --git a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java index 22ae0ff9f..36a4f0b15 100644 --- a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java @@ -232,10 +232,11 @@ public abstract class SchematicHandler { } else { y_offset_actual = yOffset; } - Location pos1 = - new Location(plot.getWorldName(), region.getMinimumPoint().getX() + xOffset, - y_offset_actual, region.getMinimumPoint().getZ() + zOffset); - Location pos2 = pos1.clone().add(WIDTH - 1, HEIGHT - 1, LENGTH - 1); + + final Location pos1 = Location.at(plot.getWorldName(), region.getMinimumPoint().getX() + xOffset, y_offset_actual, + region.getMinimumPoint().getZ() + zOffset); + final Location pos2 = pos1.add(WIDTH - 1, HEIGHT - 1, LENGTH - 1); + final int p1x = pos1.getX(); final int p1z = pos1.getZ(); final int p2x = pos2.getX(); @@ -554,10 +555,10 @@ public abstract class SchematicHandler { } final Runnable regionTask = this; CuboidRegion region = queue.poll(); - Location pos1 = new Location(world, region.getMinimumPoint().getX(), - region.getMinimumPoint().getY(), region.getMinimumPoint().getZ()); - Location pos2 = new Location(world, region.getMaximumPoint().getX(), - region.getMaximumPoint().getY(), region.getMaximumPoint().getZ()); + + final Location pos1 = Location.at(world, region.getMinimumPoint()); + final Location pos2 = Location.at(world, region.getMaximumPoint()); + final int p1x = pos1.getX(); final int sy = pos1.getY(); final int p1z = pos1.getZ(); diff --git a/Core/src/test/java/com/plotsquared/core/plot/object/LocationTest.java b/Core/src/test/java/com/plotsquared/core/plot/object/LocationTest.java deleted file mode 100644 index cef1b4c94..000000000 --- a/Core/src/test/java/com/plotsquared/core/plot/object/LocationTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * _____ _ _ _____ _ - * | __ \| | | | / ____| | | - * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | - * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | - * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | - * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| - * | | - * |_| - * PlotSquared plot management system for Minecraft - * Copyright (C) 2020 IntellectualSites - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.plotsquared.core.plot.object; - -import com.plotsquared.core.location.Location; -import org.junit.Test; - -import java.util.logging.Logger; - -public class LocationTest { - - private static final Logger logger = Logger.getLogger(LocationTest.class.getName()); - - @Test public void cloning() { - String world = "plotworld"; - Location location1 = new Location(world, 0, 0, 0); - logger.info(location1.toString()); - Location clone = location1.clone(); - world = "normal"; - logger.info(clone.toString()); - //location1.getBlockVector3(); - - } -}