Search in sources :

Example 1 with JsonElement

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

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

the class AdapterEntry method serialize.

// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(Entry<?, ?> src, Type type, JsonSerializationContext context) {
    // NULL
    if (src == null)
        return JsonNull.INSTANCE;
    // Create Ret
    JsonArray ret = new JsonArray();
    // Fill Ret
    Object key = src.getKey();
    Object value = src.getValue();
    Type keyType = getKeyType(type);
    Type valueType = getValueType(type);
    JsonElement keyJson = context.serialize(key, keyType);
    JsonElement valueJson = context.serialize(value, valueType);
    ret.add(keyJson);
    ret.add(valueJson);
    // Return Ret
    return ret;
}
Also used : JsonArray(com.massivecraft.massivecore.xlib.gson.JsonArray) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonElement(com.massivecraft.massivecore.xlib.gson.JsonElement)

Example 3 with JsonElement

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

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

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

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