Search in sources :

Example 6 with JsonElement

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

the class Coll method logModification.

protected void logModification(E entity, Modification modification) {
    JsonObject lastRaw = entity.getLastRaw();
    if (lastRaw == null) {
        List<String> messages = new MassiveList<>();
        messages.add(Txt.parse("<pink>%s", this.getDebugName()));
        messages.add(Txt.parse("<aqua>%s", entity.getId()));
        messages.add(Txt.parse("<blue>%s", modification));
        String message = Txt.implode(messages, Txt.parse("<silver> | "));
        message = Txt.parse("<b>[lastRaw null] %s", message);
        MassiveCore.get().log(message);
        return;
    }
    JsonObject currentRaw = this.getGson().toJsonTree(entity).getAsJsonObject();
    List<String> changes = new MassiveList<>();
    // Check removal and modification.
    for (Entry<String, JsonElement> entry : lastRaw.entrySet()) {
        String name = entry.getKey();
        JsonElement currentValue = currentRaw.get(name);
        if (currentValue == null) {
            changes.add(Txt.parse("<b>%s", name));
            continue;
        }
        JsonElement lastValue = entry.getValue();
        if (MStore.equal(currentValue, lastValue))
            continue;
        changes.add(Txt.parse("<i>%s", name));
    }
    // Check for addition
    for (Entry<String, JsonElement> entry : currentRaw.entrySet()) {
        String name = entry.getKey();
        if (lastRaw.has(name))
            continue;
        changes.add(Txt.parse("<g>%s", name));
    }
    // Log
    if (changes.isEmpty())
        return;
    changes.add(0, Txt.parse("<pink>%s", this.getDebugName()));
    changes.add(1, Txt.parse("<aqua>%s", entity.getId()));
    String change = Txt.implode(changes, Txt.parse("<silver> | "));
    String message = Txt.parse("<b>[Unreported Modification] %s", change);
    MassiveCore.get().log(message);
}
Also used : MassiveList(com.massivecraft.massivecore.collections.MassiveList) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 7 with JsonElement

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

the class MigratorDataItemStack001StringId method migrateInner.

// -------------------------------------------- //
// CONVERSION
// -------------------------------------------- //
@Override
public void migrateInner(JsonObject json) {
    // Get the id
    JsonElement id = json.get("id");
    if (id == null || id.isJsonNull())
        return;
    // The id is either a number or string, always a JsonPrimitive
    JsonPrimitive primitive = id.getAsJsonPrimitive();
    // Only convert if number
    if (!primitive.isNumber())
        return;
    int typeId = primitive.getAsInt();
    String name = id2name.get(typeId);
    if (name == null)
        throw new RuntimeException(String.valueOf(typeId));
    JsonElement newValue = new JsonPrimitive(name);
    json.add("id", newValue);
}
Also used : JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Example 8 with JsonElement

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

the class MigratorFieldConvert method migrate.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void migrate(JsonObject entity) {
    JsonElement value = entity.get(this.getFieldName());
    if (value == null)
        value = JsonNull.INSTANCE;
    value = getElement(migrateInner(value));
    entity.add(this.getFieldName(), value);
}
Also used : JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Example 9 with JsonElement

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

the class MigratorFieldRename method migrate.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public void migrate(JsonObject entity) {
    String from = this.getFrom();
    String to = this.getTo();
    JsonElement element = entity.remove(from);
    if (element != null)
        entity.add(to, element);
}
Also used : JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Example 10 with JsonElement

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

the class MigratorUtil method migrate.

// -------------------------------------------- //
// MIGRATE
// -------------------------------------------- //
public static boolean migrate(Type realType, JsonElement jsonElement) {
    if (jsonElement == null)
        throw new NullPointerException("element");
    if (jsonElement.isJsonNull())
        return false;
    if (jsonElement.isJsonPrimitive())
        return false;
    Type jsonType = getJsonRepresentation(realType);
    if (jsonElement.isJsonObject()) {
        if (jsonType != null && Map.class.isAssignableFrom(getClassType(jsonType))) {
            ParameterizedType parameterizedType = (ParameterizedType) jsonType;
            Type keyType = parameterizedType.getActualTypeArguments()[0];
            Type valueType = parameterizedType.getActualTypeArguments()[1];
            JsonObject object = jsonElement.getAsJsonObject();
            boolean migrated = false;
            for (Entry<String, JsonElement> entry : object.entrySet()) {
                migrated = migrate(valueType, entry.getValue()) | migrated;
            }
            return migrated;
        }
        boolean migrated = false;
        JsonObject object = jsonElement.getAsJsonObject();
        Type classType = jsonType != null ? jsonType : realType;
        migrated = migrateClass(classType, object) | migrated;
        if (jsonType != null)
            migrated = migrateFields(jsonType, object) | migrated;
        return migrated;
    }
    if (jsonElement.isJsonArray()) {
        Class<?> clazz = getClassType(jsonType);
        Type elementType = null;
        if (clazz.isArray()) {
            elementType = clazz.getComponentType();
        } else if (Collection.class.isAssignableFrom(clazz)) {
            ParameterizedType parameterizedType = (ParameterizedType) jsonType;
            elementType = parameterizedType.getActualTypeArguments()[0];
        }
        boolean migrated = false;
        for (JsonElement element1 : jsonElement.getAsJsonArray()) {
            migrated = migrate(elementType, element1) | migrated;
        }
        return migrated;
    }
    throw new RuntimeException();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) MassiveMap(com.massivecraft.massivecore.collections.MassiveMap)

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