Break out isLive utility method. Add Entry adapter.
This commit is contained in:
parent
182b48abd4
commit
b15bf3bebb
@ -2,6 +2,7 @@ package com.massivecraft.massivecore;
|
|||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -12,6 +13,7 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
|
|
||||||
import com.massivecraft.massivecore.adapter.BackstringEnumSetAdapter;
|
import com.massivecraft.massivecore.adapter.BackstringEnumSetAdapter;
|
||||||
|
import com.massivecraft.massivecore.adapter.EntryAdapter;
|
||||||
import com.massivecraft.massivecore.adapter.InventoryAdapter;
|
import com.massivecraft.massivecore.adapter.InventoryAdapter;
|
||||||
import com.massivecraft.massivecore.adapter.ItemStackAdapter;
|
import com.massivecraft.massivecore.adapter.ItemStackAdapter;
|
||||||
import com.massivecraft.massivecore.adapter.JsonElementAdapter;
|
import com.massivecraft.massivecore.adapter.JsonElementAdapter;
|
||||||
@ -116,6 +118,7 @@ public class MassiveCore extends MassivePlugin
|
|||||||
.registerTypeAdapter(MassiveTreeSetDef.class, MassiveTreeSetAdapter.get())
|
.registerTypeAdapter(MassiveTreeSetDef.class, MassiveTreeSetAdapter.get())
|
||||||
|
|
||||||
.registerTypeAdapter(BackstringEnumSet.class, BackstringEnumSetAdapter.get())
|
.registerTypeAdapter(BackstringEnumSet.class, BackstringEnumSetAdapter.get())
|
||||||
|
.registerTypeAdapter(Entry.class, EntryAdapter.get())
|
||||||
|
|
||||||
.registerTypeAdapterFactory(ModdedEnumTypeAdapter.ENUM_FACTORY);
|
.registerTypeAdapterFactory(ModdedEnumTypeAdapter.ENUM_FACTORY);
|
||||||
}
|
}
|
||||||
|
96
src/com/massivecraft/massivecore/adapter/EntryAdapter.java
Normal file
96
src/com/massivecraft/massivecore/adapter/EntryAdapter.java
Normal 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];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -101,15 +101,22 @@ public abstract class Entity<E extends Entity<E>> implements Comparable<E>
|
|||||||
// SYNC AND IO ACTIONS
|
// SYNC AND IO ACTIONS
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
public void changed()
|
public boolean isLive()
|
||||||
{
|
{
|
||||||
String id = this.getId();
|
String id = this.getId();
|
||||||
if (id == null) return;
|
if (id == null) return false;
|
||||||
|
|
||||||
Coll<E> coll = this.getColl();
|
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.
|
// UNKNOWN is very unimportant really.
|
||||||
// LOCAL_ATTACH is for example much more important and should not be replaced.
|
// LOCAL_ATTACH is for example much more important and should not be replaced.
|
||||||
|
Loading…
Reference in New Issue
Block a user