Skip to content

Instantly share code, notes, and snippets.

@xZise
Created June 11, 2011 10:13
Show Gist options
  • Save xZise/1020431 to your computer and use it in GitHub Desktop.
Save xZise/1020431 to your computer and use it in GitHub Desktop.
Example Bukkit location classes
/*
* Missing properties:
* - world
* - getDirection
* - zero
* - equals
* - hashcode
* - clone
* - to<Type> (similar to the Location.toVector())
* - toString()
*/
interface LocationGetter {
int getBlockX();
int getBlockY();
int getBlockZ();
double getX();
double getY();
double getZ();
double lengthSquared(); // This value is a int, if the location is int based (r = x² + y² + z² where x, y, z ∈ integer → r ∈ integer), but could be overflow
double length();
double distance(LocationGetter location);
double distanceSquared(LocationGetter location);
}
class FixedIntLocation implements LocationGetter {
private final int x, y, z;
public FixedIntLocation(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getBlockX() {
return this.x;
}
public double getX() {
return this.x;
}
}
class FixedDoubleLocation implements LocationGetter {…}
interface LocationModifier<T, B> {
void setX(B x);
void setY(B y);
void setZ(B z);
T add(B x, B y, B z);
T add(LocationGetter loc);
T subtract(B x, B y, B z);
T subtract(LocationGetter loc);
T multiply(B factor);
T zero();
}
// Maybe extend from LocationGetter
interface DirectionalLocationGetter {
float getYaw();
float getPitch();
}
// Maybe extend from DirectionalLocationGetter
interface DirectionalLocation {
void setYaw(float yaw);
void setPitch(float pitch);
}
class BlockLocation implements LocationModifier<BlockLocation, Integer>, LocationGetter {
private int x;
private int y;
private int z;
public BlockLocation(int x, int y, int z) {…}
public BlockLocation(LocationGetter location) {…}
}
class EntityLocation implements LocationModifier<EntityLocation, Double>, LocationGetter {
private double x;
private double y;
private double z;
public EntityLocation(double x, double y, double z) {…}
public EntityLocation(LocationGetter location) {…}
public EntityLocation add(LocationGetter location) {
return this.add(location.getX(), location.getY(), location.getZ());
}
public EntityLocation add(double x, double y, double z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
}
class EntityDirectionalLocation extends EntityLocation implements DirectionalLocation, DirectionalLocationGetter {
private float yaw;
private float pitch;
public EntityDirectionalLocation(double x, double y, double z) {…}
public EntityDirectionalLocation(double x, double y, double z, float yaw, float pitch) {…}
public EntityDirectionalLocation(LocationGetter location) {…}
public EntityDirectionalLocation(DirectionalLocationGetter location) {…}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment