Search in sources :

Example 1 with JsonObject

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

the class AdapterBannerPatterns method deserialize.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
// Default serialization is fine.
// We do however provide a smarter deserialiser.
// This is for the sole purpose of upgrading from the old deprecated database format.
// 
// In the old version (around version 2.8.10) we made use of a list with primitives.
// id, color, id, color...
// They were just coming in that order and were not wrapped in some kind of entry.
// 
// The data types are the same in the old and new version.
// String, Number, String, Number...
// 
// In the new version we do however wrap them in the DataBannerPattern.
@Override
public MassiveListDef<DataBannerPattern> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    // Create
    MassiveListDef<DataBannerPattern> ret = new MassiveListDef<>();
    // Null (Def implementation is never null)
    if (json == null)
        return ret;
    if (json.equals(JsonNull.INSTANCE))
        return ret;
    // It is an array in both old and new version
    JsonArray array = json.getAsJsonArray();
    // Empty?
    if (array.size() == 0)
        return ret;
    // First element indicates version
    JsonElement first = array.get(0);
    if (first instanceof JsonObject) {
        // New
        for (JsonElement element : array) {
            DataBannerPattern dataBannerPattern = context.deserialize(element, DataBannerPattern.class);
            ret.add(dataBannerPattern);
        }
    } else {
        // Old aka Upgrade Mode
        Iterator<JsonElement> iterator = array.iterator();
        while (iterator.hasNext()) {
            DataBannerPattern dataBannerPattern = new DataBannerPattern();
            JsonElement idElement = iterator.next();
            String id = idElement.getAsString();
            dataBannerPattern.setId(id);
            JsonElement colorElement = iterator.next();
            Integer color = colorElement.getAsInt();
            dataBannerPattern.setColor(color);
            ret.add(dataBannerPattern);
        }
    }
    return ret;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) DataBannerPattern(com.massivecraft.massivecore.item.DataBannerPattern) MassiveListDef(com.massivecraft.massivecore.collections.MassiveListDef) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 2 with JsonObject

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

the class AdapterInventory method toJson.

// -------------------------------------------- //
// IMPLEMENTATION
// -------------------------------------------- //
public static JsonElement toJson(Inventory src) {
    // The return value is this object:
    JsonObject jsonInventory = new JsonObject();
    // These variables are used in loops and repetitive logic.
    ItemStack itemStack = null;
    JsonElement jsonItemStack = null;
    String index = null;
    // Every inventory has a content part.
    ItemStack[] itemStacks = src.getContents();
    if (src instanceof PlayerInventory) {
        // Add the size "player"
        jsonInventory.addProperty(SIZE, PLAYER);
        // Cast to PlayerInventory
        PlayerInventory psrc = (PlayerInventory) src;
        // Helmet
        itemStack = psrc.getHelmet();
        if (itemStack != null) {
            jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
            jsonInventory.add(HELMET, jsonItemStack);
        }
        // Chestplate
        itemStack = psrc.getChestplate();
        if (itemStack != null) {
            jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
            jsonInventory.add(CHESTPLATE, jsonItemStack);
        }
        // Leggings
        itemStack = psrc.getLeggings();
        if (itemStack != null) {
            jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
            jsonInventory.add(LEGGINGS, jsonItemStack);
        }
        // Boots
        itemStack = psrc.getBoots();
        if (itemStack != null) {
            jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
            jsonInventory.add(BOOTS, jsonItemStack);
        }
        // Shield (Minecraft 1.9)
        itemStack = null;
        if (INDEX_PLAYER_SHIELD < itemStacks.length)
            itemStack = itemStacks[INDEX_PLAYER_SHIELD];
        if (itemStack != null) {
            jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
            jsonInventory.add(SHIELD, jsonItemStack);
        }
        // Storage Range (Minecraft 1.9)
        itemStacks = range(itemStacks, INDEX_PLAYER_STORAGE_FROM, INDEX_PLAYER_STORAGE_TO);
    } else {
        // Add the size *length*
        jsonInventory.addProperty(SIZE, itemStacks.length);
    }
    // Add the content at the end since we like to have it at the bottom of return json.
    for (Entry<Integer, DataItemStack> entry : DataItemStack.fromBukkitContents(itemStacks).entrySet()) {
        index = String.valueOf(entry.getKey());
        jsonItemStack = MassiveCore.gson.toJsonTree(entry.getValue());
        jsonInventory.add(index, jsonItemStack);
    }
    // Add version
    int version = MigratorUtil.getTargetVersion(DataItemStack.class);
    jsonInventory.addProperty(MigratorUtil.VERSION_FIELD_NAME, version);
    return jsonInventory;
}
Also used : DataItemStack(com.massivecraft.massivecore.item.DataItemStack) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) PlayerInventory(org.bukkit.inventory.PlayerInventory) DataItemStack(com.massivecraft.massivecore.item.DataItemStack) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with JsonObject

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

the class AdapterInventory method fromJson.

public static Inventory fromJson(JsonElement json) {
    // If must be an object!
    if (!json.isJsonObject())
        return null;
    JsonObject jsonInventory = json.getAsJsonObject();
    // The return value
    Inventory ret = null;
    // These variables are used in loops and repetitive logic.
    ItemStack itemStack = null;
    JsonElement jsonItemStack = null;
    // There must be a size entry!
    if (!jsonInventory.has(SIZE))
        return null;
    JsonPrimitive jsonSize = jsonInventory.get(SIZE).getAsJsonPrimitive();
    int size = 0;
    // What size/type is it?
    if (jsonSize.isString() && jsonSize.getAsString().equals(PLAYER)) {
        // We use 36 here since it's the size of the player inventory (without armor)
        size = SIZE_PLAYER_STORAGE;
        // This is a PlayerInventory
        ret = MixinInventory.get().createPlayerInventory();
        PlayerInventory pret = (PlayerInventory) ret;
        // Helmet
        if (jsonInventory.has(HELMET)) {
            itemStack = getItemStack(jsonInventory, HELMET);
            pret.setHelmet(itemStack);
        }
        // Chestplate
        if (jsonInventory.has(CHESTPLATE)) {
            itemStack = getItemStack(jsonInventory, CHESTPLATE);
            pret.setChestplate(itemStack);
        }
        // Leggings
        if (jsonInventory.has(LEGGINGS)) {
            jsonItemStack = jsonInventory.get(LEGGINGS);
            itemStack = MassiveCore.gson.fromJson(jsonItemStack, ItemStack.class);
            pret.setLeggings(itemStack);
        }
        // Boots
        if (jsonInventory.has(BOOTS)) {
            itemStack = getItemStack(jsonInventory, BOOTS);
            pret.setBoots(itemStack);
        }
        // Shield (Minecraft 1.9)
        if (jsonInventory.has(SHIELD) && INDEX_PLAYER_SHIELD < pret.getSize()) {
            itemStack = getItemStack(jsonInventory, SHIELD);
            pret.setItem(INDEX_PLAYER_SHIELD, itemStack);
        }
    } else {
        // A custom size were specified
        size = jsonSize.getAsInt();
        // This is a "Custom" Inventory (content only).
        ret = MixinInventory.get().createInventory(null, size, "");
    }
    // Now process content
    for (int i = 0; i < size; i++) {
        String stackIdx = String.valueOf(i);
        itemStack = getItemStack(jsonInventory, stackIdx);
        if (itemStack == null)
            continue;
        ret.setItem(i, itemStack);
    }
    return ret;
}
Also used : JsonPrimitive(com.massivecraft.massivecore.xlib.gson.JsonPrimitive) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) PlayerInventory(org.bukkit.inventory.PlayerInventory) DataItemStack(com.massivecraft.massivecore.item.DataItemStack) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) MixinInventory(com.massivecraft.massivecore.mixin.MixinInventory) PlayerInventory(org.bukkit.inventory.PlayerInventory)

Example 4 with JsonObject

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

the class AdapterPolymorphic method serialize.

@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null) {
        return JsonNull.INSTANCE;
    }
    JsonObject ret = new JsonObject();
    String type = src.getClass().getCanonicalName();
    ret.addProperty(TYPE, type);
    JsonElement value = context.serialize(src);
    ret.add(VALUE, value);
    return ret;
}
Also used : JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement) JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject)

Example 5 with JsonObject

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

the class CmdMassiveCoreStoreCopydb method perform.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void perform() throws MassiveException {
    // Args
    final String fromAlias = this.readArg();
    final Db fromDb = MStore.getDb(fromAlias);
    if (fromDb == null) {
        msg("<b>could not get the from-database.");
        return;
    }
    final String toAlias = this.readArg();
    final Db toDb = MStore.getDb(toAlias);
    if (toDb == null) {
        msg("<b>could not get the to-database.");
        return;
    }
    // Prepare
    Set<String> collnames = fromDb.getCollnames();
    // Statistics
    int countCollCurrent = 0;
    int countCollTotal = collnames.size();
    // Do it!
    long before = System.currentTimeMillis();
    msg("<i>Now copying database with <h>%d <i>collections.", countCollTotal);
    for (String collname : fromDb.getCollnames()) {
        countCollCurrent++;
        final Coll fromColl = new Coll(collname, Entity.class, fromDb, MassiveCore.get());
        final Coll toColl = new Coll(collname, Entity.class, toDb, MassiveCore.get());
        Collection<String> ids = fromDb.getIds(fromColl);
        msg("<i>Now copying collection <h>%d/%d %s <i>with <h>%d <i>documents.", countCollCurrent, countCollTotal, collname, ids.size());
        // Do a load check to verify we have access to this folder.
        if (ids.size() > 0 && fromDb.load(fromColl, ids.iterator().next()) == null) {
            msg("<b>Skipping <h>%s <b>since could not load data.", collname);
            continue;
        }
        for (String id : ids) {
            Entry<JsonObject, Long> data = fromDb.load(fromColl, id);
            toDb.save(toColl, id, data.getKey());
        }
    }
    long after = System.currentTimeMillis();
    long duration = after - before;
    msg("<g>The copy is now complete. <i>It took <h>%dms<i>.", duration);
}
Also used : JsonObject(com.massivecraft.massivecore.xlib.gson.JsonObject) TypeString(com.massivecraft.massivecore.command.type.primitive.TypeString) Coll(com.massivecraft.massivecore.store.Coll) Db(com.massivecraft.massivecore.store.Db)

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