Fix BlockLoc to the requirements for use as a home location

- Add home locations to #fromString
 - Switch to y=Integer#minValue more
 - May help with #3321
This commit is contained in:
dordsor21 2021-11-04 20:47:56 +00:00
parent 3d4d413de8
commit a5cf7bf2c2
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
3 changed files with 38 additions and 22 deletions

View File

@ -25,8 +25,14 @@
*/ */
package com.plotsquared.core.location; package com.plotsquared.core.location;
import com.plotsquared.core.util.StringMan;
public class BlockLoc { public class BlockLoc {
public static final BlockLoc ZERO = new BlockLoc(0, 0, 0);
public static final BlockLoc MINY = new BlockLoc(0, Integer.MIN_VALUE, 0);
private static final BlockLoc MIDDLE = new BlockLoc(Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE);
private final int x; private final int x;
private final int y; private final int y;
private final int z; private final int z;
@ -34,6 +40,10 @@ public class BlockLoc {
private final float yaw; private final float yaw;
private final float pitch; private final float pitch;
public BlockLoc(int x, int y, int z) {
this(x, y, z, 0f, 0f);
}
public BlockLoc(int x, int y, int z, float yaw, float pitch) { public BlockLoc(int x, int y, int z, float yaw, float pitch) {
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -43,11 +53,12 @@ public class BlockLoc {
this.pitch = pitch; this.pitch = pitch;
} }
public BlockLoc(int x, int y, int z) {
this(x, y, z, 0f, 0f);
}
public static BlockLoc fromString(String string) { public static BlockLoc fromString(String string) {
if (string == null || "side".equalsIgnoreCase(string)) {
return null;
} else if (StringMan.isEqualIgnoreCaseToAny(string, "center", "middle", "centre")) {
return MIDDLE;
} else {
String[] parts = string.split(","); String[] parts = string.split(",");
float yaw; float yaw;
@ -55,8 +66,11 @@ public class BlockLoc {
if (parts.length == 5) { if (parts.length == 5) {
yaw = Float.parseFloat(parts[3]); yaw = Float.parseFloat(parts[3]);
pitch = Float.parseFloat(parts[4]); pitch = Float.parseFloat(parts[4]);
} else if (parts.length == 3) {
yaw = 0;
pitch = 0;
} else { } else {
return new BlockLoc(0, 0, 0); return ZERO;
} }
int x = Integer.parseInt(parts[0]); int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]); int y = Integer.parseInt(parts[1]);
@ -64,6 +78,7 @@ public class BlockLoc {
return new BlockLoc(x, y, z, yaw, pitch); return new BlockLoc(x, y, z, yaw, pitch);
} }
}
@Override @Override
public int hashCode() { public int hashCode() {
@ -88,12 +103,12 @@ public class BlockLoc {
} }
BlockLoc other = (BlockLoc) obj; BlockLoc other = (BlockLoc) obj;
return this.getX() == other.getX() && this.getY() == other.getY() && this.getZ() == other return this.getX() == other.getX() && this.getY() == other.getY() && this.getZ() == other
.getZ(); .getZ() && this.getYaw() == other.getYaw() && this.getPitch() == other.getPitch();
} }
@Override @Override
public String toString() { public String toString() {
if (this.getX() == 0 && this.getY() == 0 && this.getZ() == 0) { if (this.getX() == 0 && this.getY() == 0 && this.getZ() == 0 && this.getYaw() == 0 && this.getPitch() == 0) {
return ""; return "";
} }
return this.getX() + "," + this.getY() + ',' + this.getZ() + ',' + this.getYaw() + ',' return this.getX() + "," + this.getY() + ',' + this.getZ() + ',' + this.getYaw() + ','

View File

@ -1477,7 +1477,7 @@ public class Plot {
*/ */
public void setHome(BlockLoc location) { public void setHome(BlockLoc location) {
Plot plot = this.getBasePlot(false); Plot plot = this.getBasePlot(false);
if (location != null && new BlockLoc(0, 0, 0).equals(location)) { if (BlockLoc.ZERO.equals(location) || BlockLoc.MINY.equals(location)) {
return; return;
} }
plot.getSettings().setPosition(location); plot.getSettings().setPosition(location);
@ -2149,8 +2149,9 @@ public class Plot {
} }
/** /**
* Gets the set home location or 0,0,0 if no location is set<br> * Gets the set home location or 0,Integer#MIN_VALUE,0 if no location is set<br>
* - Does not take the default home location into account * - Does not take the default home location into account
* - PlotSquared will internally find the correct place to teleport to if y = Integer#MIN_VALUE when teleporting to the plot.
* *
* @return home location * @return home location
*/ */

View File

@ -107,7 +107,7 @@ public class PlotSettings {
public BlockLoc getPosition() { public BlockLoc getPosition() {
if (this.position == null) { if (this.position == null) {
return new BlockLoc(0, 0, 0); return BlockLoc.MINY;
} }
return this.position; return this.position;
} }