From cfbfe9b4aa04be4bdfad5395099e1b9e00fa7253 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 12 Mar 2013 11:23:34 +0100 Subject: [PATCH] Builder and value of JsonElement --- src/com/massivecraft/mcore/MCore.java | 3 +- src/com/massivecraft/mcore/PS2.java | 120 ++++++++++++++++++--- src/com/massivecraft/mcore/PS2Adapter.java | 39 +++++++ src/com/massivecraft/mcore/PS2Builder.java | 110 +++++++++++++++++++ 4 files changed, 255 insertions(+), 17 deletions(-) create mode 100644 src/com/massivecraft/mcore/PS2Adapter.java create mode 100644 src/com/massivecraft/mcore/PS2Builder.java diff --git a/src/com/massivecraft/mcore/MCore.java b/src/com/massivecraft/mcore/MCore.java index a22a7b18..9c3b5a24 100644 --- a/src/com/massivecraft/mcore/MCore.java +++ b/src/com/massivecraft/mcore/MCore.java @@ -65,7 +65,8 @@ public class MCore extends MPlugin .registerTypeAdapter(MongoURI.class, MongoURIAdapter.get()) .registerTypeAdapter(ItemStack.class, ItemStackAdapter.get()) .registerTypeAdapter(Inventory.class, InventoryAdapter.get()) - .registerTypeAdapter(PS.class, new PSAdapter()); + .registerTypeAdapter(PS.class, new PSAdapter()) + .registerTypeAdapter(PS2.class, PS2Adapter.get()); } public static String getServerId() { return Conf.serverid; } diff --git a/src/com/massivecraft/mcore/PS2.java b/src/com/massivecraft/mcore/PS2.java index b28dc316..99f602c0 100644 --- a/src/com/massivecraft/mcore/PS2.java +++ b/src/com/massivecraft/mcore/PS2.java @@ -1,10 +1,13 @@ package com.massivecraft.mcore; import java.io.Serializable; +import java.util.Map.Entry; import org.bukkit.Bukkit; import org.bukkit.World; +import com.massivecraft.mcore.xlib.gson.JsonElement; +import com.massivecraft.mcore.xlib.gson.JsonObject; import com.massivecraft.mcore.xlib.gson.annotations.SerializedName; public final class PS2 implements Serializable @@ -30,6 +33,12 @@ public final class PS2 implements Serializable public static final transient String SERIALIZED_NAME_VELOCITYY = "vy"; public static final transient String SERIALIZED_NAME_VELOCITYZ = "vz"; + // -------------------------------------------- // + // STANDARD INSTANCES + // -------------------------------------------- // + + public static final transient PS2 NULL = new PS2(null, null, null, null, null, null, null, null, null, null, null, null, null, null); + // -------------------------------------------- // // FIELDS: RAW // -------------------------------------------- // @@ -111,6 +120,26 @@ public final class PS2 implements Serializable return Bukkit.getWorld(worldName); } + // -------------------------------------------- // + // FIELDS: WITH + // -------------------------------------------- // + + public PS2 withWorldName(String worldName) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withWorld(World world) { return new PS2(calcWorldName(world), blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withBlockX(Integer blockX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withBlockY(Integer blockY) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withBlockZ(Integer blockZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withLocationX(Double locationX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withLocationY(Double locationY) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withLocationZ(Double locationZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withChunkX(Integer chunkX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withChunkZ(Integer chunkZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withPitch(Float pitch) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withYaw(Float yaw) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withVelocityX(Double velocityX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withVelocityY(Double velocityY) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2 withVelocityZ(Double velocityZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + // -------------------------------------------- // // PRIVATE CONSTRUCTOR // -------------------------------------------- // @@ -134,24 +163,83 @@ public final class PS2 implements Serializable } // -------------------------------------------- // - // FIELDS: WITH + // BUILDER // -------------------------------------------- // - public PS2 withWorldName(String worldName) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withWorld(World world) { return new PS2(calcWorldName(world), blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withBlockX(Integer blockX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withBlockY(Integer blockY) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withBlockZ(Integer blockZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withLocationX(Double locationX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withLocationY(Double locationY) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withLocationZ(Double locationZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withChunkX(Integer chunkX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withChunkZ(Integer chunkZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withPitch(Float pitch) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withYaw(Float yaw) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withVelocityX(Double velocityX) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withVelocityY(Double velocityY) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } - public PS2 withVelocityZ(Double velocityZ) { return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); } + public PS2Builder builder() + { + return new PS2Builder(this); + } + + // -------------------------------------------- // + // FACTORY: VALUE OF + // -------------------------------------------- // + + public static PS2 valueOf(String worldName, Integer blockX, Integer blockY, Integer blockZ, Double locationX, Double locationY, Double locationZ, Integer chunkX, Integer chunkZ, Float pitch, Float yaw, Double velocityX, Double velocityY, Double velocityZ) + { + return new PS2(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); + } + + public static PS2 valueOf(JsonElement jsonElement) + { + if (jsonElement == null) return null; + if (jsonElement.isJsonNull()) return null; + + JsonObject jsonObject = jsonElement.getAsJsonObject(); + PS2Builder builder = new PS2Builder(); + for (Entry entry : jsonObject.entrySet()) + { + final String key = entry.getKey(); + final JsonElement value = entry.getValue(); + + switch(key) + { + case SERIALIZED_NAME_WORLDNAME: + builder.worldName(value.getAsString()); + break; + case SERIALIZED_NAME_BLOCKX: + builder.blockX(value.getAsInt()); + break; + case SERIALIZED_NAME_BLOCKY: + builder.blockY(value.getAsInt()); + break; + case SERIALIZED_NAME_BLOCKZ: + builder.blockZ(value.getAsInt()); + break; + case SERIALIZED_NAME_LOCATIONX: + builder.locationX(value.getAsDouble()); + break; + case SERIALIZED_NAME_LOCATIONY: + builder.locationY(value.getAsDouble()); + break; + case SERIALIZED_NAME_LOCATIONZ: + builder.locationZ(value.getAsDouble()); + break; + case SERIALIZED_NAME_CHUNKX: + builder.chunkX(value.getAsInt()); + break; + case SERIALIZED_NAME_CHUNKZ: + builder.chunkZ(value.getAsInt()); + break; + case SERIALIZED_NAME_PITCH: + builder.pitch(value.getAsFloat()); + break; + case SERIALIZED_NAME_YAW: + builder.yaw(value.getAsFloat()); + break; + case SERIALIZED_NAME_VELOCITYX: + builder.velocityX(value.getAsDouble()); + break; + case SERIALIZED_NAME_VELOCITYY: + builder.velocityY(value.getAsDouble()); + break; + case SERIALIZED_NAME_VELOCITYZ: + builder.velocityZ(value.getAsDouble()); + break; + } + } + return builder.build(); + } // -------------------------------------------- // // HASHCODE (CACHED) diff --git a/src/com/massivecraft/mcore/PS2Adapter.java b/src/com/massivecraft/mcore/PS2Adapter.java new file mode 100644 index 00000000..b8ec9db6 --- /dev/null +++ b/src/com/massivecraft/mcore/PS2Adapter.java @@ -0,0 +1,39 @@ +package com.massivecraft.mcore; + +import java.lang.reflect.Type; + +import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; +import com.massivecraft.mcore.xlib.gson.JsonDeserializer; +import com.massivecraft.mcore.xlib.gson.JsonElement; +import com.massivecraft.mcore.xlib.gson.JsonParseException; + +public class PS2Adapter implements JsonDeserializer +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static PS2Adapter i = new PS2Adapter(); + public static PS2Adapter get() { return i; } + private PS2Adapter() {} + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public PS2 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + return deserialize(json); + } + + // -------------------------------------------- // + // STATIC LOGIC + // -------------------------------------------- // + + public static PS2 deserialize(JsonElement json) + { + return PS2.valueOf(json); + } + +} diff --git a/src/com/massivecraft/mcore/PS2Builder.java b/src/com/massivecraft/mcore/PS2Builder.java new file mode 100644 index 00000000..f8c7bf1c --- /dev/null +++ b/src/com/massivecraft/mcore/PS2Builder.java @@ -0,0 +1,110 @@ +package com.massivecraft.mcore; + +import org.bukkit.World; + +public class PS2Builder +{ + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private String worldName = null; + public String worldName() { return this.worldName; } + public World world() { return PS2.calcWorld(this.worldName); } + public PS2Builder worldName(String worldName) { this.worldName = worldName; return this; } + public PS2Builder world(World world) { this.worldName = PS2.calcWorldName(world); return this; } + + private Integer blockX = null; + public Integer blockX() { return this.blockX; } + public PS2Builder blockX(Integer blockX) { this.blockX = blockX; return this; } + + private Integer blockY = null; + public Integer blockY() { return this.blockY; } + public PS2Builder blockY(Integer blockY) { this.blockY = blockY; return this; } + + private Integer blockZ = null; + public Integer blockZ() { return this.blockZ; } + public PS2Builder blockZ(Integer blockZ) { this.blockZ = blockZ; return this; } + + private Double locationX = null; + public Double locationX() { return this.locationX; } + public PS2Builder locationX(Double locationX) { this.locationX = locationX; return this; } + + private Double locationY = null; + public Double locationY() { return this.locationY; } + public PS2Builder locationY(Double locationY) { this.locationY = locationY; return this; } + + private Double locationZ = null; + public Double locationZ() { return this.locationZ; } + public PS2Builder locationZ(Double locationZ) { this.locationZ = locationZ; return this; } + + private Integer chunkX = null; + public Integer chunkX() { return this.chunkX; } + public PS2Builder chunkX(Integer chunkX) { this.chunkX = chunkX; return this; } + + private Integer chunkZ = null; + public Integer chunkZ() { return this.chunkZ; } + public PS2Builder chunkZ(Integer chunkZ) { this.chunkZ = chunkZ; return this; } + + private Float pitch = null; + public Float pitch() { return this.pitch; } + public PS2Builder pitch(Float pitch) { this.pitch = pitch; return this; } + + private Float yaw = null; + public Float yaw() { return this.yaw; } + public PS2Builder yaw(Float yaw) { this.yaw = yaw; return this; } + + private Double velocityX = null; + public Double velocityX() { return this.velocityX; } + public PS2Builder velocityX(Double velocityX) { this.velocityX = velocityX; return this; } + + private Double velocityY = null; + public Double velocityY() { return this.velocityY; } + public PS2Builder velocityY(Double velocityY) { this.velocityY = velocityY; return this; } + + private Double velocityZ = null; + public Double velocityZ() { return this.velocityZ; } + public PS2Builder velocityZ(Double velocityZ) { this.velocityZ = velocityZ; return this; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public PS2Builder(String worldName, Integer blockX, Integer blockY, Integer blockZ, Double locationX, Double locationY, Double locationZ, Integer chunkX, Integer chunkZ, Float pitch, Float yaw, Double velocityX, Double velocityY, Double velocityZ) + { + this.worldName = worldName; + this.blockX = blockX; + this.blockY = blockY; + this.blockZ = blockZ; + this.locationX = locationX; + this.locationY = locationY; + this.locationZ = locationZ; + this.chunkX = chunkX; + this.chunkZ = chunkZ; + this.pitch = pitch; + this.yaw = yaw; + this.velocityX = velocityX; + this.velocityY = velocityY; + this.velocityZ = velocityZ; + } + + public PS2Builder(PS2 ps) + { + this(ps.getWorldName(), ps.getBlockX(), ps.getBlockY(), ps.getBlockZ(), ps.getLocationX(), ps.getLocationY(), ps.getLocationZ(), ps.getChunkX(), ps.getChunkZ(), ps.getPitch(), ps.getYaw(), ps.getVelocityX(), ps.getVelocityY(), ps.getVelocityZ()); + } + + public PS2Builder() + { + + } + + // -------------------------------------------- // + // BUILD + // -------------------------------------------- // + + public PS2 build() + { + return PS2.valueOf(worldName, blockX, blockY, blockZ, locationX, locationY, locationZ, chunkX, chunkZ, pitch, yaw, velocityX, velocityY, velocityZ); + } + +}