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 = 36;
// This is a PlayerInventory
ret = new CraftInventoryPlayer(new MCorePlayerInventory());
PlayerInventory pret = (PlayerInventory)ret;
// helmet
if (jsonInventory.has(HELMET))
{
jsonItemStack = jsonInventory.get(HELMET);
itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
pret.setHelmet(itemStack);
}
// chestplate
if (jsonInventory.has(CHESTPLATE))
{
jsonItemStack = jsonInventory.get(CHESTPLATE);
itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
pret.setChestplate(itemStack);
}
// leggings
if (jsonInventory.has(LEGGINGS))
{
jsonItemStack = jsonInventory.get(LEGGINGS);
itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
pret.setLeggings(itemStack);
}
// boots
if (jsonInventory.has(BOOTS))
{
jsonItemStack = jsonInventory.get(BOOTS);
itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
pret.setBoots(itemStack);
}
}
else
{
// A custom size were specified
size = jsonSize.getAsInt();
// This is a "Custom" Inventory (content only).
ret = new CraftInventoryCustom(null, size);
}
// Now process content
ItemStack[] itemStacks = new ItemStack[size];
for (int i = 0; i < size; i++)
{
// Fetch the jsonItemStack or mark it as empty and continue
String stackIdx = String.valueOf(i);
jsonItemStack = jsonInventory.get(stackIdx);
itemStack = MCore.gson.fromJson(jsonItemStack, ItemStack.class);
itemStacks[i] = itemStack;
}
ret.setContents(itemStacks);