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,26 +53,31 @@ 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) {
String[] parts = string.split(","); if (string == null || "side".equalsIgnoreCase(string)) {
return null;
float yaw; } else if (StringMan.isEqualIgnoreCaseToAny(string, "center", "middle", "centre")) {
float pitch; return MIDDLE;
if (parts.length == 5) {
yaw = Float.parseFloat(parts[3]);
pitch = Float.parseFloat(parts[4]);
} else { } else {
return new BlockLoc(0, 0, 0); String[] parts = string.split(",");
}
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
int z = Integer.parseInt(parts[2]);
return new BlockLoc(x, y, z, yaw, pitch); float yaw;
float pitch;
if (parts.length == 5) {
yaw = Float.parseFloat(parts[3]);
pitch = Float.parseFloat(parts[4]);
} else if (parts.length == 3) {
yaw = 0;
pitch = 0;
} else {
return ZERO;
}
int x = Integer.parseInt(parts[0]);
int y = Integer.parseInt(parts[1]);
int z = Integer.parseInt(parts[2]);
return new BlockLoc(x, y, z, yaw, pitch);
}
} }
@Override @Override
@ -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;
} }