Search in sources :

Example 1 with DataKey

use of net.citizensnpcs.api.util.DataKey in project CitizensAPI by CitizensDev.

the class AbstractNPC method clone.

@Override
public NPC clone() {
    NPC copy = registry.createNPC(getTrait(MobType.class).getType(), getFullName());
    DataKey key = new MemoryDataKey();
    save(key);
    copy.load(key);
    for (Trait trait : copy.getTraits()) {
        trait.onCopy();
    }
    return copy;
}
Also used : DataKey(net.citizensnpcs.api.util.DataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) Trait(net.citizensnpcs.api.trait.Trait)

Example 2 with DataKey

use of net.citizensnpcs.api.util.DataKey in project CitizensAPI by CitizensDev.

the class AbstractNPC method copy.

@Override
public NPC copy() {
    NPC copy = registry.createNPC(getOrAddTrait(MobType.class).getType(), getFullName());
    DataKey key = new MemoryDataKey();
    save(key);
    copy.load(key);
    for (Trait trait : copy.getTraits()) {
        trait.onCopy();
    }
    Bukkit.getPluginManager().callEvent(new NPCCloneEvent(this, copy));
    return copy;
}
Also used : NPCCloneEvent(net.citizensnpcs.api.event.NPCCloneEvent) DataKey(net.citizensnpcs.api.util.DataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) MemoryDataKey(net.citizensnpcs.api.util.MemoryDataKey) Trait(net.citizensnpcs.api.trait.Trait)

Example 3 with DataKey

use of net.citizensnpcs.api.util.DataKey in project CitizensAPI by CitizensDev.

the class PersistenceLoader method deserialiseCollection.

private static void deserialiseCollection(Collection<Object> collection, DataKey root, PersistField field) {
    for (DataKey subKey : root.getRelative(field.key).getSubKeys()) {
        Object loaded = deserialiseCollectionValue(field, subKey, field.persistAnnotation.valueType());
        if (loaded == null)
            continue;
        collection.add(loaded);
    }
}
Also used : DataKey(net.citizensnpcs.api.util.DataKey)

Example 4 with DataKey

use of net.citizensnpcs.api.util.DataKey in project CitizensAPI by CitizensDev.

the class PersistenceLoader method deserialise.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void deserialise(PersistField field, Object instance, Object oldValue, DataKey root) throws Exception {
    Object value;
    Class<?> type = field.getType();
    Class<?> collectionType = field.getCollectionType();
    // TODO: this is pretty ugly.
    if (!Collection.class.isAssignableFrom(collectionType) && !Map.class.isAssignableFrom(collectionType))
        throw loadException;
    if (List.class.isAssignableFrom(type)) {
        List<Object> list = (List<Object>) (!List.class.isAssignableFrom(collectionType) ? Lists.newArrayList() : collectionType.newInstance());
        Object raw = root.getRaw(field.key);
        if (raw instanceof List && collectionType.isAssignableFrom(raw.getClass())) {
            list = (List<Object>) raw;
        } else {
            deserialiseCollection(list, root, field);
        }
        value = list;
    } else if (Set.class.isAssignableFrom(type)) {
        Set set;
        if (Set.class.isAssignableFrom(collectionType)) {
            set = (Set) collectionType.newInstance();
        } else {
            if (field.getType().isEnum()) {
                set = EnumSet.noneOf((Class<? extends Enum>) field.getType());
            } else {
                set = (Set) (oldValue != null && Set.class.isAssignableFrom(oldValue.getClass()) ? oldValue.getClass().newInstance() : Sets.newHashSet());
            }
        }
        Object raw = root.getRaw(field.key);
        if (raw instanceof Set && collectionType.isAssignableFrom(raw.getClass())) {
            set = (Set) raw;
        } else
            deserialiseCollection(set, root, field);
        value = set;
    } else if (Map.class.isAssignableFrom(type)) {
        Map<String, Object> map;
        if (Map.class.isAssignableFrom(collectionType)) {
            map = (Map<String, Object>) collectionType.newInstance();
        } else {
            boolean hasConcreteType = oldValue != null && Map.class.isAssignableFrom(oldValue.getClass()) && !oldValue.getClass().isInterface();
            map = (Map<String, Object>) (hasConcreteType ? oldValue : Maps.newHashMap());
        }
        deserialiseMap(map, root, field);
        value = map;
    } else if (float[].class.isAssignableFrom(type)) {
        List<Float> floats = Lists.newArrayList();
        for (DataKey sub : root.getRelative(field.key).getIntegerSubKeys()) {
            floats.add((float) sub.getDouble(""));
        }
        value = new float[floats.size()];
        for (int i = 0; i < floats.size(); i++) {
            ((float[]) value)[i] = floats.get(i);
        }
    } else if (double[].class.isAssignableFrom(type)) {
        List<Double> doubles = Lists.newArrayList();
        for (DataKey sub : root.getRelative(field.key).getIntegerSubKeys()) {
            doubles.add(sub.getDouble(""));
        }
        value = new double[doubles.size()];
        for (int i = 0; i < doubles.size(); i++) {
            ((double[]) value)[i] = doubles.get(i);
        }
    } else if (int[].class.isAssignableFrom(type)) {
        List<Integer> ints = Lists.newArrayList();
        for (DataKey sub : root.getRelative(field.key).getIntegerSubKeys()) {
            ints.add(sub.getInt(""));
        }
        value = new int[ints.size()];
        for (int i = 0; i < ints.size(); i++) {
            ((int[]) value)[i] = ints.get(i);
        }
    } else
        value = deserialiseValue(field, root.getRelative(field.key));
    if (value == null && field.isRequired())
        throw loadException;
    if (type.isPrimitive() || Primitives.isWrapperType(type)) {
        if (value == null)
            return;
        if (!Primitives.isWrapperType(type)) {
            type = Primitives.wrap(type);
        }
        Class<?> clazz = value.getClass();
        if (clazz == Integer.class && type != Integer.class && type != Double.class && type != Long.class && type != Float.class) {
            return;
        }
        if (clazz == Float.class && type != Double.class && type != Float.class) {
            return;
        }
        if (clazz == Double.class && type != Double.class) {
            if (type == Float.class) {
                value = ((Double) value).floatValue();
            } else {
                return;
            }
        }
        if (clazz == Byte.class && type != Short.class && type != Byte.class && type != Integer.class && type != Double.class && type != Long.class && type != Float.class) {
            return;
        }
        if (clazz == Short.class && type != Short.class && type != Integer.class && type != Double.class && type != Long.class && type != Float.class) {
            return;
        }
        if (clazz == Character.class && type != Character.class && type != Short.class && type != Integer.class && type != Double.class && type != Long.class && type != Float.class) {
            return;
        }
        field.set(instance, value);
    } else {
        if (value != null && !type.isAssignableFrom(value.getClass()))
            return;
        field.set(instance, value);
    }
}
Also used : Set(java.util.Set) EnumSet(java.util.EnumSet) DataKey(net.citizensnpcs.api.util.DataKey) List(java.util.List)

Example 5 with DataKey

use of net.citizensnpcs.api.util.DataKey in project CitizensAPI by CitizensDev.

the class PersistenceLoader method deserialiseMap.

private static void deserialiseMap(Map<String, Object> map, DataKey root, PersistField field) {
    for (DataKey subKey : root.getRelative(field.key).getSubKeys()) {
        Object loaded = deserialiseCollectionValue(field, subKey, field.persistAnnotation.valueType());
        if (loaded == null)
            continue;
        map.put(subKey.name(), loaded);
    }
}
Also used : DataKey(net.citizensnpcs.api.util.DataKey)

Aggregations

DataKey (net.citizensnpcs.api.util.DataKey)17 MemoryDataKey (net.citizensnpcs.api.util.MemoryDataKey)5 Trait (net.citizensnpcs.api.trait.Trait)4 GameProfile (com.mojang.authlib.GameProfile)2 Property (com.mojang.authlib.properties.Property)2 Anchor (net.citizensnpcs.util.Anchor)2 EventHandler (org.bukkit.event.EventHandler)2 SkullMeta (org.bukkit.inventory.meta.SkullMeta)2 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)1 FlaggableObject (com.denizenscript.denizencore.flags.FlaggableObject)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 Function (com.google.common.base.Function)1 Collection (java.util.Collection)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Set (java.util.Set)1 UUID (java.util.UUID)1 NPCCloneEvent (net.citizensnpcs.api.event.NPCCloneEvent)1