Make PS comparable and have equals make use of the compare methods.

This commit is contained in:
Olof Larsson 2013-04-11 08:19:15 +02:00
parent 95f1dc8dd0
commit 6d0e2f7669
2 changed files with 71 additions and 82 deletions

View File

@ -70,7 +70,7 @@ public class WorldMixinDefault extends WorldMixinAbstract
World world = Bukkit.getWorld(worldId); World world = Bukkit.getWorld(worldId);
if (world == null) return; if (world == null) return;
spawnPs = spawnPs.withWorld(world); spawnPs = spawnPs.withWorld(world.getName());
Location location = null; Location location = null;
try try

View File

@ -41,7 +41,7 @@ import com.massivecraft.mcore.xlib.gson.annotations.SerializedName;
* entity: world, locationX, locationY, locationZ, pitch, yaw, velocityX, velocityY, velocityZ * entity: world, locationX, locationY, locationZ, pitch, yaw, velocityX, velocityY, velocityZ
*/ */
public final class PS implements Cloneable, Serializable public final class PS implements Cloneable, Serializable, Comparable<PS>
{ {
// -------------------------------------------- // // -------------------------------------------- //
// CONSTANTS // CONSTANTS
@ -170,7 +170,6 @@ public final class PS implements Cloneable, Serializable
// -------------------------------------------- // // -------------------------------------------- //
public PS withWorld(String world) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } public PS withWorld(String world) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); }
public PS withWorld(World world) { return new PS(calcWorldName(world), blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); }
public PS withBlockX(Integer blockX) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } public PS withBlockX(Integer blockX) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); }
public PS withBlockY(Integer blockY) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } public PS withBlockY(Integer blockY) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); }
public PS withBlockZ(Integer blockZ) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } public PS withBlockZ(Integer blockZ) { return new PS(world, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); }
@ -796,90 +795,80 @@ public final class PS implements Cloneable, Serializable
// -------------------------------------------- // // -------------------------------------------- //
@Override @Override
public boolean equals(Object obj) public boolean equals(Object derpObject)
{ {
return PS.equals(this, obj); return equals(this, derpObject);
} }
public static boolean equals(PS ps, Object obj) public static boolean equals(PS herp, Object derpObject)
{ {
if (ps == obj) return true; return compareTo(herp, derpObject) == 0;
if (ps == null) return false; }
if (obj == null) return false;
if (!(obj instanceof PS)) return false;
PS derp = (PS) obj;
if (ps.blockX == null) // -------------------------------------------- //
{ // COMPARE
if (derp.blockX != null) return false; // -------------------------------------------- //
}
else if (!ps.blockX.equals(derp.blockX)) return false; @Override
if (ps.blockY == null) public int compareTo(PS derp)
{ {
if (derp.blockY != null) return false; return compareTo(this, derp);
} }
else if (!ps.blockY.equals(derp.blockY)) return false;
if (ps.blockZ == null) public static int compareTo(PS herp, Object derpObject)
{ {
if (derp.blockZ != null) return false; if (herp == null && derpObject == null) return 0;
} if (herp == null) return -1;
else if (!ps.blockZ.equals(derp.blockZ)) return false; if (derpObject == null) return +1;
if (ps.chunkX == null)
{ if (!(derpObject instanceof PS)) return -1;
if (derp.chunkX != null) return false; PS derp = (PS) derpObject;
}
else if (!ps.chunkX.equals(derp.chunkX)) return false; int ret;
if (ps.chunkZ == null)
{ ret = MUtil.compare(herp.world, derp.world);
if (derp.chunkZ != null) return false; if (ret != 0) return ret;
}
else if (!ps.chunkZ.equals(derp.chunkZ)) return false; ret = MUtil.compare(herp.chunkX, derp.chunkX);
if (ps.locationX == null) if (ret != 0) return ret;
{
if (derp.locationX != null) return false; ret = MUtil.compare(herp.chunkZ, derp.chunkZ);
} if (ret != 0) return ret;
else if (!ps.locationX.equals(derp.locationX)) return false;
if (ps.locationY == null) ret = MUtil.compare(herp.blockX, derp.blockX);
{ if (ret != 0) return ret;
if (derp.locationY != null) return false;
} ret = MUtil.compare(herp.blockY, derp.blockY);
else if (!ps.locationY.equals(derp.locationY)) return false; if (ret != 0) return ret;
if (ps.locationZ == null)
{ ret = MUtil.compare(herp.blockZ, derp.blockZ);
if (derp.locationZ != null) return false; if (ret != 0) return ret;
}
else if (!ps.locationZ.equals(derp.locationZ)) return false; ret = MUtil.compare(herp.locationX, derp.locationX);
if (ps.pitch == null) if (ret != 0) return ret;
{
if (derp.pitch != null) return false; ret = MUtil.compare(herp.locationX, derp.locationX);
} if (ret != 0) return ret;
else if (!ps.pitch.equals(derp.pitch)) return false;
if (ps.velocityX == null) ret = MUtil.compare(herp.locationZ, derp.locationZ);
{ if (ret != 0) return ret;
if (derp.velocityX != null) return false;
} ret = MUtil.compare(herp.pitch, derp.pitch);
else if (!ps.velocityX.equals(derp.velocityX)) return false; if (ret != 0) return ret;
if (ps.velocityY == null)
{ ret = MUtil.compare(herp.yaw, derp.yaw);
if (derp.velocityY != null) return false; if (ret != 0) return ret;
}
else if (!ps.velocityY.equals(derp.velocityY)) return false; ret = MUtil.compare(herp.velocityX, derp.velocityX);
if (ps.velocityZ == null) if (ret != 0) return ret;
{
if (derp.velocityZ != null) return false; ret = MUtil.compare(herp.velocityY, derp.velocityY);
} if (ret != 0) return ret;
else if (!ps.velocityZ.equals(derp.velocityZ)) return false;
if (ps.world == null) ret = MUtil.compare(herp.velocityZ, derp.velocityZ);
{ if (ret != 0) return ret;
if (derp.world != null) return false;
} return 0;
else if (!ps.world.equals(derp.world)) return false;
if (ps.yaw == null)
{
if (derp.yaw != null) return false;
}
else if (!ps.yaw.equals(derp.yaw)) return false;
return true;
} }
// -------------------------------------------- // // -------------------------------------------- //