Skip to content

Instantly share code, notes, and snippets.

@Cryptite
Last active November 11, 2017 15:20
Show Gist options
  • Save Cryptite/b7aeb0de00d91f1293b8 to your computer and use it in GitHub Desktop.
Save Cryptite/b7aeb0de00d91f1293b8 to your computer and use it in GitHub Desktop.
StringLocation class for safely storing Location data
import com.sk89q.worldedit.Vector;
import org.bukkit.*;
import org.bukkit.block.Block;
import java.util.Objects;
import static org.bukkit.util.NumberConversions.square;
public class StringLocation {
public String world;
public double x, y, z;
private float yaw;
private float pitch;
public StringLocation(Block b) {
this.world = b.getWorld().getName();
this.x = b.getX();
this.y = b.getY();
this.z = b.getZ();
}
public StringLocation(Location l) {
this.world = l.getWorld().getName();
this.x = l.getBlockX();
this.y = l.getBlockY();
this.z = l.getBlockZ();
this.yaw = l.getYaw();
this.pitch = l.getPitch();
}
public StringLocation(String string) {
if (string == null) return;
String[] elems = string.split(",");
world = elems[0];
x = Double.parseDouble(elems[1]);
y = Double.parseDouble(elems[2]);
z = Double.parseDouble(elems[3]);
yaw = elems.length > 4 ? Float.parseFloat(elems[4]) : 0f;
pitch = elems.length > 5 ? Float.parseFloat(elems[5]) : 0f;
}
public Chunk getChunk() {
return Bukkit.getWorld(world).getChunkAt(getLocation());
}
public Location getLocation() {
if (yaw != 0 || pitch != 0) {
return new Location(getWorld(), x, y, z, yaw, pitch);
}
return new Location(getWorld(), x, y, z);
}
public void add(int x, int y, int z) {
this.x += x;
this.y += y;
this.z += z;
}
public void subtract(int x, int y, int z) {
this.x -= x;
this.y -= y;
this.z -= z;
}
public boolean equals(Location l) {
World world = l.getWorld();
return this.world != null
&& world != null
&& this.world.equals(world.getName())
&& Double.doubleToLongBits(this.x) == Double.doubleToLongBits(l.getX())
&& Double.doubleToLongBits(this.y) == Double.doubleToLongBits(l.getY())
&& Double.doubleToLongBits(this.z) == Double.doubleToLongBits(l.getZ())
&& Float.floatToIntBits(this.pitch) == Float.floatToIntBits(l.getPitch())
&& Float.floatToIntBits(this.yaw) == Float.floatToIntBits(l.getYaw());
}
public boolean looseEquals(Location l) {
return getBlockX() == l.getBlockX()
&& getBlockY() == l.getBlockY()
&& getBlockZ() == l.getBlockZ();
}
public Location getCenter() {
return getBlock().getLocation().add(.5, .5, .5);
}
public Block getBlock() {
return world != null
? Bukkit.getWorld(world).getBlockAt((int) x, (int) y, (int) z)
: Bukkit.getWorld("world").getBlockAt(0, 0, 0);
}
Material getType() {
return getBlock().getType();
}
void setType(Material m) {
getBlock().setType(m);
}
public int getBlockX() {
return (int) x;
}
public int getBlockY() {
return (int) y;
}
public int getBlockZ() {
return (int) z;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setZ(int z) {
this.z = z;
}
public void setPitch(float pitch) {
this.pitch = pitch;
}
public void setYaw(float yaw) {
this.yaw = yaw;
}
public float getPitch() {
return pitch;
}
public float getYaw() {
return yaw;
}
public double distance(Location location) {
return getLocation().distance(location);
}
public double distance(StringLocation loc) {
return Math.sqrt(square(this.x - loc.x) + square(this.y - loc.y) + square(this.z - loc.z));
}
public org.bukkit.World getWorld() {
return Bukkit.getWorld(world);
}
public Vector toVector() {
return new Vector(getBlockX(), getBlockY(), getBlockZ());
}
public void setLocation(Location l) {
world = l.getWorld().getName();
x = (int) l.getX();
y = (int) l.getY();
z = (int) l.getZ();
yaw = l.getYaw();
pitch = l.getPitch();
}
@Override
public int hashCode() {
return Objects.hash(world, x, y, z, yaw, pitch);
}
@Override
public boolean equals(Object other) {
if (!(other instanceof StringLocation)) {
return false;
}
StringLocation o = (StringLocation) other;
return x == o.x && y == o.y && z == o.z &&
world.equals(o.world);
}
@Override
public String toString() {
return LocationUtil.coordsToString(this);
}
public String toStringBasic() {
return LocationUtil.coordsToStringBasic(getLocation());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment