Search in sources :

Example 6 with JsonObject

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

the class Coll method loadFromRemoteFixed.

@Override
public synchronized void loadFromRemoteFixed(String id, Entry<JsonObject, Long> remoteEntry) {
    if (id == null)
        throw new NullPointerException("id");
    this.removeIdentifiedModificationFixed(id);
    if (remoteEntry == null) {
        try {
            remoteEntry = this.getDb().load(this, id);
        } catch (Exception e) {
            logLoadError(id, e.getMessage());
            return;
        }
    }
    if (!this.remoteEntryIsOk(id, remoteEntry))
        return;
    JsonObject raw = remoteEntry.getKey();
    Long mtime = remoteEntry.getValue();
    int version = MigratorUtil.getVersion(raw);
    if (version > MigratorUtil.getTargetVersion(this.getEntityClass())) {
        logLoadError(id, String.format("Cannot load entity of entity version %d", version));
        return;
    }
    // Migrate if another version is wanted
    boolean migrated = MigratorUtil.migrate(this.getEntityClass(), raw);
    // Calculate temp but handle raw cases.
    E temp;
    try {
        temp = this.getGson().fromJson(raw, this.getEntityClass());
    } catch (JsonSyntaxException ex) {
        logLoadError(id, ex.getMessage());
        return;
    }
    E entity = this.getFixed(id, false);
    if (entity != null) {
        // It did already exist
        this.copy(temp, entity);
    } else {
        // Create first (or load from registry)
        // The registry is used by MassiveMagic
        Map<String, E> tempRegistry = this.getTempRegistry();
        if (tempRegistry != null)
            entity = tempRegistry.get(id);
        if (entity == null)
            entity = this.createNewInstance();
        if (tempRegistry != null)
            tempRegistry.remove(id);
        // Copy over data first
        this.copy(temp, entity);
        // Then attach!
        this.attach(entity, id, false);
    // On creation it might be modified by addition or removal of new/old fields.
    // So we must do a check for that.
    // this.putIdentifiedModificationFixed(id, Modification.UNKNOWN);
    }
    entity.setLastRaw(raw);
    entity.setLastMtime(mtime);
    entity.setLastDefault(false);
    // Now the loading is done. If it was migrated we will have to save it to remote again.
    if (migrated)
        this.putIdentifiedModificationFixed(id, Modification.LOCAL_ALTER);
}
Also used : JsonSyntaxException(com.massivecraft.massivecore.xlib.gson.JsonSyntaxException) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonSyntaxException(com.massivecraft.massivecore.xlib.gson.JsonSyntaxException)

Example 7 with JsonObject

use of com.massivecraft.massivecore.xlib.gson.JsonObject 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 8 with JsonObject

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

the class Coll method saveToRemoteFixed.

@Override
public synchronized void saveToRemoteFixed(String id) {
    if (id == null)
        throw new NullPointerException("id");
    this.removeIdentifiedModificationFixed(id);
    E entity = this.idToEntity.get(id);
    if (entity == null)
        return;
    entity.clearSyncLogFields();
    JsonObject raw = this.getGson().toJsonTree(entity, this.getEntityClass()).getAsJsonObject();
    entity.setLastRaw(raw);
    if (this.isDefault(entity)) {
        this.getDb().delete(this, id);
        entity.setLastDefault(true);
    } else {
        long mtime = this.getDb().save(this, id, raw);
        // Perhaps we should log it, or simply try again.
        if (mtime == 0)
            return;
        entity.setLastMtime(mtime);
    }
    MixinModification.get().syncModification(entity);
}
Also used : JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 9 with JsonObject

use of com.massivecraft.massivecore.xlib.gson.JsonObject 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)

Example 10 with JsonObject

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

the class DriverMongo method loadRaw.

public Entry<JsonObject, Long> loadRaw(BasicDBObject raw) {
    if (raw == null)
        return new SimpleEntry<>(null, 0L);
    // Throw away the id field
    raw.removeField(ID_FIELD);
    // In case there is no _mtime set we assume 1337.
    // NOTE: We can not use 0 since that one is reserved for errors.
    // Probably a manual database addition by the server owner.
    long mtime = 1337L;
    Object mtimeObject = raw.removeField(MTIME_FIELD);
    if (mtimeObject != null) {
        mtime = ((Number) mtimeObject).longValue();
    }
    // Convert MongoDB --> GSON
    JsonObject element = GsonMongoConverter.mongo2GsonObject(raw);
    return new SimpleEntry<>(element, mtime);
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Aggregations

JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)21 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)9 SimpleEntry (java.util.AbstractMap.SimpleEntry)4 BasicDBObject (com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)3 Entry (java.util.Map.Entry)3 DataItemStack (com.massivecraft.massivecore.item.DataItemStack)2 JsonPrimitive (com.massivecraft.massivecore.xlib.gson.JsonPrimitive)2 JsonSyntaxException (com.massivecraft.massivecore.xlib.gson.JsonSyntaxException)2 ItemStack (org.bukkit.inventory.ItemStack)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 TypeString (com.massivecraft.massivecore.command.type.primitive.TypeString)1 DataBannerPattern (com.massivecraft.massivecore.item.DataBannerPattern)1 MixinInventory (com.massivecraft.massivecore.mixin.MixinInventory)1 Coll (com.massivecraft.massivecore.store.Coll)1 Db (com.massivecraft.massivecore.store.Db)1 SenderColl (com.massivecraft.massivecore.store.SenderColl)1 JsonArray (com.massivecraft.massivecore.xlib.gson.JsonArray)1