2011-07-18 22:06:02 +02:00
|
|
|
package com.massivecraft.factions;
|
2011-03-19 13:00:03 +01:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
import com.massivecraft.factions.util.MiscUtil;
|
2014-10-19 07:37:25 +02:00
|
|
|
|
2012-01-15 19:50:13 +01:00
|
|
|
import org.bukkit.Bukkit;
|
2011-03-19 13:00:03 +01:00
|
|
|
import org.bukkit.Location;
|
2012-01-15 19:50:13 +01:00
|
|
|
import org.bukkit.World;
|
2011-03-19 13:00:03 +01:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.entity.Player;
|
2011-07-18 22:06:02 +02:00
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
import java.io.Serializable;
|
2014-04-04 20:55:21 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
public class FLocation implements Serializable {
|
|
|
|
private static final long serialVersionUID = -8292915234027387983L;
|
2014-04-04 20:55:21 +02:00
|
|
|
private String worldName = "world";
|
|
|
|
private int x = 0;
|
|
|
|
private int z = 0;
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Constructors
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public FLocation() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public FLocation(String worldName, int x, int z) {
|
2014-07-01 22:10:18 +02:00
|
|
|
this.worldName = worldName;
|
|
|
|
this.x = x;
|
|
|
|
this.z = z;
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public FLocation(Location location) {
|
|
|
|
this(location.getWorld().getName(), blockToChunk(location.getBlockX()), blockToChunk(location.getBlockZ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public FLocation(Player player) {
|
|
|
|
this(player.getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
public FLocation(FPlayer fplayer) {
|
|
|
|
this(fplayer.getPlayer());
|
|
|
|
}
|
2011-03-19 13:00:03 +01:00
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
public FLocation(Block block) {
|
|
|
|
this(block.getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Getters and Setters
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public String getWorldName() {
|
|
|
|
return worldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public World getWorld() {
|
|
|
|
return Bukkit.getWorld(worldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setWorldName(String worldName) {
|
|
|
|
this.worldName = worldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getX() {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setX(int x) {
|
|
|
|
this.x = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getZ() {
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setZ(int z) {
|
|
|
|
this.z = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getCoordString() {
|
|
|
|
return "" + x + "," + z;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "[" + this.getWorldName() + "," + this.getCoordString() + "]";
|
|
|
|
}
|
|
|
|
|
2014-10-19 07:37:25 +02:00
|
|
|
public static FLocation fromString(String string) {
|
|
|
|
int index = string.indexOf(",", 0);
|
|
|
|
int start = 1;
|
|
|
|
String worldName = string.substring(start, index);
|
|
|
|
start = index + 1;
|
|
|
|
index = string.indexOf(",", start);
|
|
|
|
int x = Integer.valueOf(string.substring(start, index));
|
|
|
|
int y = Integer.valueOf(string.substring(index + 1, string.length() - 1));
|
|
|
|
return new FLocation(worldName, x, y);
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Block/Chunk/Region Value Transformation
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
// bit-shifting is used because it's much faster than standard division and multiplication
|
|
|
|
public static int blockToChunk(int blockVal) { // 1 chunk is 16x16 blocks
|
|
|
|
return blockVal >> 4; // ">> 4" == "/ 16"
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int blockToRegion(int blockVal) { // 1 region is 512x512 blocks
|
|
|
|
return blockVal >> 9; // ">> 9" == "/ 512"
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int chunkToRegion(int chunkVal) { // 1 region is 32x32 chunks
|
|
|
|
return chunkVal >> 5; // ">> 5" == "/ 32"
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int chunkToBlock(int chunkVal) {
|
|
|
|
return chunkVal << 4; // "<< 4" == "* 16"
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int regionToBlock(int regionVal) {
|
|
|
|
return regionVal << 9; // "<< 9" == "* 512"
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int regionToChunk(int regionVal) {
|
|
|
|
return regionVal << 5; // "<< 5" == "* 32"
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Misc Geometry
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
public FLocation getRelative(int dx, int dz) {
|
|
|
|
return new FLocation(this.worldName, this.x + dx, this.z + dz);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getDistanceTo(FLocation that) {
|
2014-07-01 22:10:18 +02:00
|
|
|
double dx = that.x - this.x;
|
|
|
|
double dz = that.z - this.z;
|
|
|
|
return Math.sqrt(dx * dx + dz * dz);
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
|
2014-07-01 23:51:39 +02:00
|
|
|
public double getDistanceSquaredTo(FLocation that) {
|
|
|
|
double dx = that.x - this.x;
|
|
|
|
double dz = that.z - this.z;
|
|
|
|
return dx * dx + dz * dz;
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:55:21 +02:00
|
|
|
//----------------------------------------------//
|
|
|
|
// Some Geometry
|
|
|
|
//----------------------------------------------//
|
|
|
|
public Set<FLocation> getCircle(double radius) {
|
2014-07-01 23:51:39 +02:00
|
|
|
double radiusSquared = radius * radius;
|
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
Set<FLocation> ret = new LinkedHashSet<FLocation>();
|
|
|
|
if (radius <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
2014-07-01 22:10:18 +02:00
|
|
|
int xfrom = (int) Math.floor(this.x - radius);
|
|
|
|
int xto = (int) Math.ceil(this.x + radius);
|
|
|
|
int zfrom = (int) Math.floor(this.z - radius);
|
|
|
|
int zto = (int) Math.ceil(this.z + radius);
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
for (int x = xfrom; x <= xto; x++) {
|
|
|
|
for (int z = zfrom; z <= zto; z++) {
|
|
|
|
FLocation potential = new FLocation(this.worldName, x, z);
|
2014-07-01 23:51:39 +02:00
|
|
|
if (this.getDistanceSquaredTo(potential) <= radiusSquared) {
|
2014-07-01 22:10:18 +02:00
|
|
|
ret.add(potential);
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static HashSet<FLocation> getArea(FLocation from, FLocation to) {
|
|
|
|
HashSet<FLocation> ret = new HashSet<FLocation>();
|
|
|
|
|
|
|
|
for (long x : MiscUtil.range(from.getX(), to.getX())) {
|
|
|
|
for (long z : MiscUtil.range(from.getZ(), to.getZ())) {
|
|
|
|
ret.add(new FLocation(from.getWorldName(), (int) x, (int) z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------//
|
|
|
|
// Comparison
|
|
|
|
//----------------------------------------------//
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
// should be fast, with good range and few hash collisions: (x * 512) + z + worldName.hashCode
|
|
|
|
return (this.x << 9) + this.z + (this.worldName != null ? this.worldName.hashCode() : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
2014-07-01 22:10:18 +02:00
|
|
|
if (obj == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!(obj instanceof FLocation)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 20:55:21 +02:00
|
|
|
|
|
|
|
FLocation that = (FLocation) obj;
|
|
|
|
return this.x == that.x && this.z == that.z && (this.worldName == null ? that.worldName == null : this.worldName.equals(that.worldName));
|
|
|
|
}
|
2011-03-19 13:00:03 +01:00
|
|
|
}
|