Moved to new package, created PSFormater system, added static comparators such as locationDistance and inSameUniverse

This commit is contained in:
Olof Larsson 2013-03-12 17:35:14 +01:00
parent 19a21d25a1
commit 3b0a3eff59
9 changed files with 387 additions and 16 deletions

View File

@ -18,6 +18,8 @@ import com.massivecraft.mcore.cmd.CmdMcore;
import com.massivecraft.mcore.integration.protocollib.ProtocolLibFeatures;
import com.massivecraft.mcore.mixin.ScheduledTeleportEngine;
import com.massivecraft.mcore.mixin.SenderIdMixinDefault;
import com.massivecraft.mcore.ps.PS2;
import com.massivecraft.mcore.ps.PS2Adapter;
import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.Db;
import com.massivecraft.mcore.store.MStore;

View File

@ -1,4 +1,4 @@
package com.massivecraft.mcore;
package com.massivecraft.mcore.ps;
import java.io.Serializable;
import java.util.Map.Entry;
@ -11,6 +11,10 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;
import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.usys.Aspect;
import com.massivecraft.mcore.usys.Multiverse;
import com.massivecraft.mcore.util.MUtil;
import com.massivecraft.mcore.xlib.gson.JsonElement;
import com.massivecraft.mcore.xlib.gson.JsonObject;
import com.massivecraft.mcore.xlib.gson.annotations.SerializedName;
@ -73,11 +77,28 @@ public final class PS2 implements Cloneable, Serializable
public static final transient String NAME_FULL_CHUNKX = "chunkX";
public static final transient String NAME_FULL_CHUNKZ = "chunkZ";
public static final transient String NAME_FULL_PITCH = "pitch";
public static final transient String NAME_FULL_YAW = "yay";
public static final transient String NAME_FULL_YAW = "yaw";
public static final transient String NAME_FULL_VELOCITYX = "velocityX";
public static final transient String NAME_FULL_VELOCITYY = "velocityY";
public static final transient String NAME_FULL_VELOCITYZ = "velocityZ";
public static final transient String NAME_VERBOOSE_WORLD = "World";
public static final transient String NAME_VERBOOSE_BLOCKX = "Block X";
public static final transient String NAME_VERBOOSE_BLOCKY = "Block Y";
public static final transient String NAME_VERBOOSE_BLOCKZ = "Block Z";
public static final transient String NAME_VERBOOSE_LOCATIONX = "Location X";
public static final transient String NAME_VERBOOSE_LOCATIONY = "Location Y";
public static final transient String NAME_VERBOOSE_LOCATIONZ = "Location Z";
public static final transient String NAME_VERBOOSE_CHUNKX = "Chunk X";
public static final transient String NAME_VERBOOSE_CHUNKZ = "Chunk Z";
public static final transient String NAME_VERBOOSE_PITCH = "Pitch";
public static final transient String NAME_VERBOOSE_YAW = "Yaw";
public static final transient String NAME_VERBOOSE_VELOCITYX = "Velocity X";
public static final transient String NAME_VERBOOSE_VELOCITYY = "Velocity Y";
public static final transient String NAME_VERBOOSE_VELOCITYZ = "Velocity Z";
public static final transient String SPACE_WASNT_SET = " wasn't set";
// -------------------------------------------- //
// STANDARD INSTANCES
// -------------------------------------------- //
@ -536,7 +557,7 @@ public final class PS2 implements Cloneable, Serializable
public static World asBukkitWorld(String world) throws IllegalStateException, NullPointerException
{
if (world == null) throw new NullPointerException("world wasn't set");
if (world == null) throw new NullPointerException(NAME_FULL_WORLD + SPACE_WASNT_SET);
World ret = Bukkit.getWorld(world);
if (ret == null) throw new IllegalStateException("the world "+world+" does not exist on "+MCore.getServerId());
return ret;
@ -547,13 +568,13 @@ public final class PS2 implements Cloneable, Serializable
World world = ps.asBukkitWorld();
Integer blockX = ps.getBlockX();
if (blockX == null) throw new IllegalStateException("blockX wasn't set");
if (blockX == null) throw new IllegalStateException(NAME_FULL_BLOCKX + SPACE_WASNT_SET);
Integer blockY = ps.getBlockY();
if (blockY == null) throw new IllegalStateException("blockY wasn't set");
if (blockY == null) throw new IllegalStateException(NAME_FULL_BLOCKY + SPACE_WASNT_SET);
Integer blockZ = ps.getBlockZ();
if (blockZ == null) throw new IllegalStateException("blockZ wasn't set");
if (blockZ == null) throw new IllegalStateException(NAME_FULL_BLOCKZ + SPACE_WASNT_SET);
return world.getBlockAt(blockX, blockY, blockZ);
}
@ -563,13 +584,13 @@ public final class PS2 implements Cloneable, Serializable
World world = ps.asBukkitWorld();
Double locationX = ps.getLocationX();
if (locationX == null) throw new IllegalStateException("locationX wasn't set");
if (locationX == null) throw new IllegalStateException(NAME_FULL_LOCATIONX + SPACE_WASNT_SET);
Double locationY = ps.getLocationY();
if (locationY == null) throw new IllegalStateException("locationY wasn't set");
if (locationY == null) throw new IllegalStateException(NAME_FULL_LOCATIONY + SPACE_WASNT_SET);
Double locationZ = ps.getLocationZ();
if (locationZ == null) throw new IllegalStateException("locationZ wasn't set");
if (locationZ == null) throw new IllegalStateException(NAME_FULL_LOCATIONZ + SPACE_WASNT_SET);
Float pitch = ps.getPitch();
if (pitch == null) pitch = DEFAULT_BUKKIT_PITCH;
@ -585,10 +606,10 @@ public final class PS2 implements Cloneable, Serializable
World world = ps.asBukkitWorld();
Integer chunkX = ps.getChunkX();
if (chunkX == null) throw new IllegalStateException("chunkX wasn't set");
if (chunkX == null) throw new IllegalStateException(NAME_FULL_CHUNKX + SPACE_WASNT_SET);
Integer chunkZ = ps.getChunkZ();
if (chunkZ == null) throw new IllegalStateException("chunkZ wasn't set");
if (chunkZ == null) throw new IllegalStateException(NAME_FULL_CHUNKZ + SPACE_WASNT_SET);
return world.getChunkAt(chunkX, chunkZ);
}
@ -596,13 +617,13 @@ public final class PS2 implements Cloneable, Serializable
public static Vector asBukkitVelocity(PS2 ps) throws IllegalStateException
{
Double velocityX = ps.getVelocityX();
if (velocityX == null) throw new IllegalStateException("velocityX wasn't set");
if (velocityX == null) throw new IllegalStateException(NAME_FULL_VELOCITYX + SPACE_WASNT_SET);
Double velocityY = ps.getVelocityY();
if (velocityY == null) throw new IllegalStateException("velocityY wasn't set");
if (velocityY == null) throw new IllegalStateException(NAME_FULL_VELOCITYY + SPACE_WASNT_SET);
Double velocityZ = ps.getVelocityZ();
if (velocityZ == null) throw new IllegalStateException("velocityZ wasn't set");
if (velocityZ == null) throw new IllegalStateException(NAME_FULL_VELOCITYZ + SPACE_WASNT_SET);
return new Vector(velocityX, velocityY, velocityZ);
}
@ -618,6 +639,111 @@ public final class PS2 implements Cloneable, Serializable
return world.getName();
}
// -------------------------------------------- //
// TO STRING
// -------------------------------------------- //
// TODO: Remove this alias!
public String getShortDesc()
{
return this.toString(PS2FormatDesc.get());
}
@Override
public String toString()
{
return this.toString(PS2FormatFormal.get());
}
public String toString(PS2Format format)
{
return format.format(this);
}
public static String toString(PS2 ps, PS2Format format)
{
return format.format(ps);
}
// -------------------------------------------- //
// PARTIAL COMPARES
// -------------------------------------------- //
public static Double locationDistanceSquared(PS2 one, PS2 two)
{
if (one == null) return null;
if (two == null) return null;
String w1 = one.getWorld();
String w2 = two.getWorld();
if (!MUtil.equals(w1, w2)) return null;
Double x1 = one.getLocationX(true);
if (x1 == null) return null;
Double y1 = one.getLocationY(true);
if (y1 == null) return null;
Double z1 = one.getLocationZ(true);
if (z1 == null) return null;
Double x2 = two.getLocationX(true);
if (x2 == null) return null;
Double y2 = two.getLocationY(true);
if (y2 == null) return null;
Double z2 = two.getLocationZ(true);
if (z2 == null) return null;
return Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2);
}
public static Double locationDistance(PS2 one, PS2 two)
{
Double ret = locationDistanceSquared(one, two);
if (ret == null) return null;
return Math.sqrt(ret);
}
public static boolean inSameWorld(PS2 one, PS2 two)
{
if (one == null) return false;
if (two == null) return false;
String w1 = one.getWorld();
String w2 = two.getWorld();
if (w1 == null) return false;
if (w2 == null) return false;
return w1.equalsIgnoreCase(w2);
}
public static boolean inSameUniverse(PS2 one, PS2 two, Multiverse multiverse)
{
if (one == null) return false;
if (two == null) return false;
String w1 = one.getWorld();
String w2 = two.getWorld();
if (w1 == null) return false;
if (w2 == null) return false;
String m1 = multiverse.getUniverseForWorldName(w1);
String m2 = multiverse.getUniverseForWorldName(w2);
return m1.equalsIgnoreCase(m2);
}
public static boolean inSameUniverse(PS2 one, PS2 two, Aspect aspect)
{
return inSameUniverse(one, two, aspect.multiverse());
}
// -------------------------------------------- //
// HASHCODE (CACHED)
// -------------------------------------------- //

View File

@ -1,4 +1,4 @@
package com.massivecraft.mcore;
package com.massivecraft.mcore.ps;
import java.lang.reflect.Type;

View File

@ -1,4 +1,4 @@
package com.massivecraft.mcore;
package com.massivecraft.mcore.ps;
import org.bukkit.World;

View File

@ -0,0 +1,6 @@
package com.massivecraft.mcore.ps;
public interface PS2Format
{
public String format(PS2 ps);
}

View File

@ -0,0 +1,131 @@
package com.massivecraft.mcore.ps;
import java.util.ArrayList;
import java.util.List;
import com.massivecraft.mcore.util.Txt;
public class PS2FormatAbstract implements PS2Format
{
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
private final String strNull;
private final String strStart;
private final String formatWorld;
private final String formatBlockX;
private final String formatBlockY;
private final String formatBlockZ;
private final String formatLocationX;
private final String formatLocationY;
private final String formatLocationZ;
private final String formatChunkX;
private final String formatChunkZ;
private final String formatPitch;
private final String formatYaw;
private final String formatVelocityX;
private final String formatVelocityY;
private final String formatVelocityZ;
private final String strGlue;
private final String strStop;
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public PS2FormatAbstract(String strNull, String strStart, String formatWorld, String formatBlockX, String formatBlockY, String formatBlockZ, String formatLocationX, String formatLocationY, String formatLocationZ, String formatChunkX, String formatChunkZ, String formatPitch, String formatYaw, String formatVelocityX, String formatVelocityY, String formatVelocityZ, String strGlue, String strStop)
{
this.strNull = strNull;
this.strStart = strStart;
this.formatWorld = formatWorld;
this.formatBlockX = formatBlockX;
this.formatBlockY = formatBlockY;
this.formatBlockZ = formatBlockZ;
this.formatLocationX = formatLocationX;
this.formatLocationY = formatLocationY;
this.formatLocationZ = formatLocationZ;
this.formatChunkX = formatChunkX;
this.formatChunkZ = formatChunkZ;
this.formatPitch = formatPitch;
this.formatYaw = formatYaw;
this.formatVelocityX = formatVelocityX;
this.formatVelocityY = formatVelocityY;
this.formatVelocityZ = formatVelocityZ;
this.strGlue = strGlue;
this.strStop = strStop;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public String format(PS2 ps)
{
if (ps == null) return this.strNull;
List<String> entries = this.formatEntries(ps);
return this.strStart + Txt.implode(entries, this.strGlue) + this.strStop;
}
// -------------------------------------------- //
// UTIL
// -------------------------------------------- //
public List<String> formatEntries(PS2 ps)
{
List<String> ret = new ArrayList<String>();
Object val = null;
val = ps.getWorld();
if (val != null) ret.add(String.format(this.formatWorld, val));
val = ps.getBlockX();
if (val != null) ret.add(String.format(this.formatBlockX, val));
val = ps.getBlockY();
if (val != null) ret.add(String.format(this.formatBlockY, val));
val = ps.getBlockZ();
if (val != null) ret.add(String.format(this.formatBlockZ, val));
val = ps.getLocationX();
if (val != null) ret.add(String.format(this.formatLocationX, val));
val = ps.getLocationY();
if (val != null) ret.add(String.format(this.formatLocationY, val));
val = ps.getLocationZ();
if (val != null) ret.add(String.format(this.formatLocationZ, val));
val = ps.getChunkX();
if (val != null) ret.add(String.format(this.formatChunkX, val));
val = ps.getChunkZ();
if (val != null) ret.add(String.format(this.formatChunkZ, val));
val = ps.getPitch();
if (val != null) ret.add(String.format(this.formatPitch, val));
val = ps.getYaw();
if (val != null) ret.add(String.format(this.formatYaw, val));
val = ps.getVelocityX();
if (val != null) ret.add(String.format(this.formatVelocityX, val));
val = ps.getVelocityY();
if (val != null) ret.add(String.format(this.formatVelocityY, val));
val = ps.getVelocityZ();
if (val != null) ret.add(String.format(this.formatVelocityZ, val));
return ret;
}
}

View File

@ -0,0 +1,36 @@
package com.massivecraft.mcore.ps;
import com.massivecraft.mcore.util.Txt;
public class PS2FormatDesc extends PS2FormatAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PS2FormatDesc i = new PS2FormatDesc();
public static PS2FormatDesc get() { return i; }
private PS2FormatDesc()
{
super(
Txt.parse("<silver><em>NULL"),
"",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_WORLD + Txt.parse(" <v>") + "%s",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_BLOCKX + Txt.parse(" <v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_BLOCKY + Txt.parse(" <v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_BLOCKZ + Txt.parse(" <v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_LOCATIONX + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_LOCATIONY + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_LOCATIONZ + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_CHUNKX + Txt.parse(" <v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_CHUNKZ + Txt.parse(" <v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_PITCH + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_YAW + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_VELOCITYX + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_VELOCITYY + Txt.parse(" <v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_VELOCITYZ + Txt.parse(" <v>") + "%.2f",
" ",
""
);
}
}

View File

@ -0,0 +1,34 @@
package com.massivecraft.mcore.ps;
public class PS2FormatFormal extends PS2FormatAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PS2FormatFormal i = new PS2FormatFormal();
public static PS2FormatFormal get() { return i; }
private PS2FormatFormal()
{
super(
"PS{NULL}",
"PS{",
PS2.NAME_SERIALIZED_WORLD + ": %s",
PS2.NAME_SERIALIZED_BLOCKX + ": %d",
PS2.NAME_SERIALIZED_BLOCKY + ": %d",
PS2.NAME_SERIALIZED_BLOCKZ + ": %d",
PS2.NAME_SERIALIZED_LOCATIONX + ": %.2f",
PS2.NAME_SERIALIZED_LOCATIONY + ": %.2f",
PS2.NAME_SERIALIZED_LOCATIONZ + ": %.2f",
PS2.NAME_SERIALIZED_CHUNKX + ": %d",
PS2.NAME_SERIALIZED_CHUNKZ + ": %d",
PS2.NAME_SERIALIZED_PITCH + ": %.2f",
PS2.NAME_SERIALIZED_YAW + ": %.2f",
PS2.NAME_SERIALIZED_VELOCITYX + ": %.2f",
PS2.NAME_SERIALIZED_VELOCITYY + ": %.2f",
PS2.NAME_SERIALIZED_VELOCITYZ + ": %.2f",
", ",
"}"
);
}
}

View File

@ -0,0 +1,36 @@
package com.massivecraft.mcore.ps;
import com.massivecraft.mcore.util.Txt;
public class PS2FormatSlug extends PS2FormatAbstract
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static PS2FormatSlug i = new PS2FormatSlug();
public static PS2FormatSlug get() { return i; }
private PS2FormatSlug()
{
super(
Txt.parse("<silver><em>NULL"),
"",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_WORLD + Txt.parse("<v>") + "%s",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_BLOCKX + Txt.parse("<v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_BLOCKY + Txt.parse("<v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_BLOCKZ + Txt.parse("<v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_LOCATIONX + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_LOCATIONY + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_LOCATIONZ + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_CHUNKX + Txt.parse("<v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_CHUNKZ + Txt.parse("<v>") + "%d",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_PITCH + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_YAW + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_VELOCITYX + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_VELOCITYY + Txt.parse("<v>") + "%.2f",
Txt.parse("<k>") + PS2.NAME_SERIALIZED_VELOCITYZ + Txt.parse("<v>") + "%.2f",
"",
""
);
}
}