diff --git a/src/com/massivecraft/massivecore/MassiveCore.java b/src/com/massivecraft/massivecore/MassiveCore.java index b7b3b505..1219a9c7 100644 --- a/src/com/massivecraft/massivecore/MassiveCore.java +++ b/src/com/massivecraft/massivecore/MassiveCore.java @@ -2,6 +2,7 @@ package com.massivecraft.massivecore; import java.lang.reflect.Modifier; import java.util.List; +import java.util.Map.Entry; import java.util.Random; import java.util.Set; import java.util.UUID; @@ -12,6 +13,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import com.massivecraft.massivecore.adapter.BackstringEnumSetAdapter; +import com.massivecraft.massivecore.adapter.EntryAdapter; import com.massivecraft.massivecore.adapter.InventoryAdapter; import com.massivecraft.massivecore.adapter.ItemStackAdapter; import com.massivecraft.massivecore.adapter.JsonElementAdapter; @@ -116,6 +118,7 @@ public class MassiveCore extends MassivePlugin .registerTypeAdapter(MassiveTreeSetDef.class, MassiveTreeSetAdapter.get()) .registerTypeAdapter(BackstringEnumSet.class, BackstringEnumSetAdapter.get()) + .registerTypeAdapter(Entry.class, EntryAdapter.get()) .registerTypeAdapterFactory(ModdedEnumTypeAdapter.ENUM_FACTORY); } diff --git a/src/com/massivecraft/massivecore/adapter/EntryAdapter.java b/src/com/massivecraft/massivecore/adapter/EntryAdapter.java new file mode 100644 index 00000000..6a977323 --- /dev/null +++ b/src/com/massivecraft/massivecore/adapter/EntryAdapter.java @@ -0,0 +1,96 @@ +package com.massivecraft.massivecore.adapter; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Map.Entry; +import java.util.AbstractMap.SimpleEntry; + +import com.massivecraft.massivecore.xlib.gson.JsonArray; +import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext; +import com.massivecraft.massivecore.xlib.gson.JsonDeserializer; +import com.massivecraft.massivecore.xlib.gson.JsonElement; +import com.massivecraft.massivecore.xlib.gson.JsonNull; +import com.massivecraft.massivecore.xlib.gson.JsonParseException; +import com.massivecraft.massivecore.xlib.gson.JsonSerializationContext; +import com.massivecraft.massivecore.xlib.gson.JsonSerializer; + +public class EntryAdapter implements JsonDeserializer>, JsonSerializer> +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EntryAdapter i = new EntryAdapter(); + public static EntryAdapter get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public JsonElement serialize(Entry src, Type type, JsonSerializationContext context) + { + // NULL + if (src == null) return JsonNull.INSTANCE; + + // Create Ret + JsonArray ret = new JsonArray(); + + // Fill Ret + Object key = src.getKey(); + Object value = src.getValue(); + + Type keyType = getKeyType(type); + Type valueType = getValueType(type); + + JsonElement keyJson = context.serialize(key, keyType); + JsonElement valueJson = context.serialize(value, valueType); + + ret.add(keyJson); + ret.add(valueJson); + + // Return Ret + return ret; + } + + @Override + public Entry deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException + { + // NULL + if (json == null) return null; + if (json instanceof JsonNull) return null; + + JsonArray jsonArray = (JsonArray)json; + + JsonElement keyJson = jsonArray.get(0); + JsonElement valueJson = jsonArray.get(1); + + Type keyType = getKeyType(type); + Type valueType = getValueType(type); + + Object key = context.deserialize(keyJson, keyType); + Object value = context.deserialize(valueJson, valueType); + + return new SimpleEntry(key, value); + } + + // -------------------------------------------- // + // UTIL + // -------------------------------------------- // + + public static Type getKeyType(Type type) + { + return getType(type, 0); + } + public static Type getValueType(Type type) + { + return getType(type, 1); + } + public static Type getType(Type type, int index) + { + ParameterizedType ptype = (ParameterizedType)type; + Type[] types = ptype.getActualTypeArguments(); + return types[index]; + } + +} diff --git a/src/com/massivecraft/massivecore/store/Entity.java b/src/com/massivecraft/massivecore/store/Entity.java index 4f9e11d6..2ca1a636 100644 --- a/src/com/massivecraft/massivecore/store/Entity.java +++ b/src/com/massivecraft/massivecore/store/Entity.java @@ -101,15 +101,22 @@ public abstract class Entity> implements Comparable // SYNC AND IO ACTIONS // -------------------------------------------- // - public void changed() + public boolean isLive() { String id = this.getId(); - if (id == null) return; + if (id == null) return false; Coll coll = this.getColl(); - if (coll == null) return; + if (coll == null) return false; - if ( ! coll.inited()) return; + if ( ! coll.inited()) return false; + + return true; + } + + public void changed() + { + if ( ! this.isLive()) return; // UNKNOWN is very unimportant really. // LOCAL_ATTACH is for example much more important and should not be replaced.