Search in sources :

Example 16 with JsonObject

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

the class DriverFlatfile method loadFile.

public Entry<JsonObject, Long> loadFile(File file) {
    long mtime = file.lastModified();
    JsonObject raw = loadFileJson(file);
    return new SimpleEntry<>(raw, mtime);
}
Also used : SimpleEntry(java.util.AbstractMap.SimpleEntry) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 17 with JsonObject

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

the class DriverMongo method loadAll.

@Override
public Map<String, Entry<JsonObject, Long>> loadAll(Coll<?> coll) {
    // Declare Ret
    Map<String, Entry<JsonObject, Long>> ret = null;
    // Fix Coll
    DBCollection dbcoll = fixColl(coll);
    // Find All
    DBCursor cursor = dbcoll.find();
    try {
        // Create Ret
        ret = new MassiveMap<>(cursor.count());
        // For Each Found
        while (cursor.hasNext()) {
            BasicDBObject raw = (BasicDBObject) cursor.next();
            // Get ID
            Object rawId = raw.removeField(ID_FIELD);
            if (rawId == null)
                continue;
            String id = rawId.toString();
            // Get Entry
            Entry<JsonObject, Long> entry = loadRaw(raw);
            // NOTE: The entry can be a failed one with null and 0.
            // NOTE: We add it anyways since it's an informative failure.
            // NOTE: This is supported by our defined specification.
            // Add
            ret.put(id, entry);
        }
    } finally {
        cursor.close();
    }
    // Return Ret
    return ret;
}
Also used : DBCollection(com.massivecraft.massivecore.xlib.mongodb.DBCollection) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) DBCursor(com.massivecraft.massivecore.xlib.mongodb.DBCursor) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) BasicDBObject(com.massivecraft.massivecore.xlib.mongodb.BasicDBObject)

Example 18 with JsonObject

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

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

the class Coll method examineHasLocalAlterFixed.

protected boolean examineHasLocalAlterFixed(String id, E entity) {
    JsonObject lastRaw = entity.getLastRaw();
    JsonObject currentRaw = null;
    try {
        currentRaw = this.getGson().toJsonTree(entity, this.getEntityClass()).getAsJsonObject();
    } catch (Exception e) {
        MassiveCore.get().log(Txt.parse("<b>Database examineHasLocalAlter failed convert current entity to JSON tree."));
        MassiveCore.get().log(Txt.parse("<k>Error: <v>%s", e.getMessage()));
        MassiveCore.get().log(Txt.parse("<k>Entity: <v>%s", id));
        MassiveCore.get().log(Txt.parse("<k>Collection: <v>%s", this.getName()));
        throw new RuntimeException(e);
    }
    return !MStore.equal(lastRaw, currentRaw);
}
Also used : JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) JsonSyntaxException(com.massivecraft.massivecore.xlib.gson.JsonSyntaxException)

Example 20 with JsonObject

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

the class Coll method remoteEntryIsOk.

public boolean remoteEntryIsOk(String id, Entry<JsonObject, Long> remoteEntry) {
    Long mtime = remoteEntry.getValue();
    if (mtime == null) {
        this.logLoadError(id, "Last modification time (mtime) was null. The file might not be readable or simply not exist.");
        return false;
    }
    if (mtime == 0) {
        this.logLoadError(id, "Last modification time (mtime) was 0. The file might not be readable or simply not exist.");
        return false;
    }
    JsonObject raw = remoteEntry.getKey();
    if (raw == null) {
        this.logLoadError(id, "Raw data was null. Is the file completely empty?");
        return false;
    }
    if (raw.isJsonNull()) {
        this.logLoadError(id, "Raw data was JSON null. It seems you have a file containing just the word \"null\". Why would you do this?");
        return false;
    }
    return true;
}
Also used : JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

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