Search in sources :

Example 16 with JsonElement

use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.

the class PS method valueOf.

public static PS valueOf(final JsonElement jsonElement) {
    if (jsonElement == null)
        return null;
    if (jsonElement.isJsonNull())
        return null;
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    final PSBuilder builder = new PSBuilder();
    if (jsonObject.has("world") && jsonObject.has("yaw")) {
        // Old Faction LazyLocation
        for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            final String key = entry.getKey();
            final JsonElement value = entry.getValue();
            if (key.equals("world")) {
                builder.world(value.getAsString());
            } else if (key.equals("x")) {
                builder.locationX(value.getAsDouble());
            } else if (key.equals("y")) {
                builder.locationY(value.getAsDouble());
            } else if (key.equals("z")) {
                builder.locationZ(value.getAsDouble());
            } else if (key.equals("pitch")) {
                builder.pitch(value.getAsFloat());
            } else if (key.equals("yaw")) {
                builder.yaw(value.getAsFloat());
            }
        }
    } else {
        // The Standard Format
        for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            final String key = entry.getKey();
            final JsonElement value = entry.getValue();
            if (key.equals(NAME_SERIALIZED_WORLD)) {
                builder.world(value.getAsString());
            } else if (key.equals(NAME_SERIALIZED_BLOCKX)) {
                builder.blockX(value.getAsInt());
            } else if (key.equals(NAME_SERIALIZED_BLOCKY)) {
                builder.blockY(value.getAsInt());
            } else if (key.equals(NAME_SERIALIZED_BLOCKZ)) {
                builder.blockZ(value.getAsInt());
            } else if (key.equals(NAME_SERIALIZED_LOCATIONX)) {
                builder.locationX(value.getAsDouble());
            } else if (key.equals(NAME_SERIALIZED_LOCATIONY)) {
                builder.locationY(value.getAsDouble());
            } else if (key.equals(NAME_SERIALIZED_LOCATIONZ)) {
                builder.locationZ(value.getAsDouble());
            } else if (key.equals(NAME_SERIALIZED_CHUNKX)) {
                builder.chunkX(value.getAsInt());
            } else if (key.equals(NAME_SERIALIZED_CHUNKZ)) {
                builder.chunkZ(value.getAsInt());
            } else if (key.equals(NAME_SERIALIZED_PITCH)) {
                builder.pitch(value.getAsFloat());
            } else if (key.equals(NAME_SERIALIZED_YAW)) {
                builder.yaw(value.getAsFloat());
            } else if (key.equals(NAME_SERIALIZED_VELOCITYX)) {
                builder.velocityX(value.getAsDouble());
            } else if (key.equals(NAME_SERIALIZED_VELOCITYY)) {
                builder.velocityY(value.getAsDouble());
            } else if (key.equals(NAME_SERIALIZED_VELOCITYZ)) {
                builder.velocityZ(value.getAsDouble());
            }
        }
    }
    return builder.build();
}
Also used : JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 17 with JsonElement

use of com.massivecraft.massivecore.xlib.gson.JsonElement in project MassiveCore by MassiveCraft.

the class MigratorUtil method migrateFields.

public static boolean migrateFields(Type type, JsonObject object) {
    if (type == null)
        throw new NullPointerException("entityClass");
    if (object == null)
        throw new NullPointerException("object");
    Class<?> entityClass = getClassType(type);
    // because their "fields" are dynamically made and can't be looked up with reflection
    if (Map.class.isAssignableFrom(entityClass))
        return false;
    boolean migrated = false;
    for (Entry<String, JsonElement> entry : object.entrySet()) {
        String name = entry.getKey();
        JsonElement element = entry.getValue();
        // It might be defined in a superclass, so find the defining class
        Class<?> superClass = ReflectionUtil.getSuperclassDeclaringField(entityClass, true, name);
        // Try find field if it has a different serialisation name
        if (superClass == null) {
            Field field = tryFindField(entityClass, name);
            if (field != null) {
                name = field.getName();
                superClass = entityClass;
            }
        }
        Type elementType = ReflectionUtil.getField(superClass, name).getGenericType();
        migrated = migrate(elementType, element) | migrated;
    }
    return migrated;
}
Also used : Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Aggregations

JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)17 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)9 JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 Type (java.lang.reflect.Type)4 DataItemStack (com.massivecraft.massivecore.item.DataItemStack)3 ItemStack (org.bukkit.inventory.ItemStack)3 JsonPrimitive (com.massivecraft.massivecore.xlib.gson.JsonPrimitive)2 PlayerInventory (org.bukkit.inventory.PlayerInventory)2 MassiveList (com.massivecraft.massivecore.collections.MassiveList)1 MassiveListDef (com.massivecraft.massivecore.collections.MassiveListDef)1 MassiveMap (com.massivecraft.massivecore.collections.MassiveMap)1 DataBannerPattern (com.massivecraft.massivecore.item.DataBannerPattern)1 MixinInventory (com.massivecraft.massivecore.mixin.MixinInventory)1 JsonNull (com.massivecraft.massivecore.xlib.gson.JsonNull)1 BasicDBList (com.massivecraft.massivecore.xlib.mongodb.BasicDBList)1 Field (java.lang.reflect.Field)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1