Break out isLive utility method. Add Entry adapter.

This commit is contained in:
Olof Larsson 2015-03-31 15:54:20 +02:00
parent 182b48abd4
commit b15bf3bebb
3 changed files with 110 additions and 4 deletions

View File

@ -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);
}

View File

@ -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<Entry<?, ?>>, JsonSerializer<Entry<?, ?>>
{
// -------------------------------------------- //
// 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<Object, Object>(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];
}
}

View File

@ -101,15 +101,22 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
// SYNC AND IO ACTIONS
// -------------------------------------------- //
public void changed()
public boolean isLive()
{
String id = this.getId();
if (id == null) return;
if (id == null) return false;
Coll<E> 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.