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