Search in sources :

Example 1 with DataItemStack

use of com.massivecraft.massivecore.item.DataItemStack 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)

Aggregations

DataItemStack (com.massivecraft.massivecore.item.DataItemStack)1 JsonElement (com.massivecraft.massivecore.xlib.gson.JsonElement)1 JsonObject (com.massivecraft.massivecore.xlib.gson.JsonObject)1 ItemStack (org.bukkit.inventory.ItemStack)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1