More tweaks to the inventory stuff

This commit is contained in:
Olof Larsson 2013-03-23 17:24:38 +01:00
parent 9cc687dab0
commit f8d29d98aa
2 changed files with 47 additions and 20 deletions

View File

@ -68,15 +68,8 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
ItemStack itemStack = null;
JsonElement jsonItemStack = null;
// Every inventory has a content part. Lets handle it at once:
// Every inventory has a content part.
ItemStack[] itemStacks = src.getContents();
for (int i = 0; i < itemStacks.length; i++)
{
itemStack = itemStacks[i];
jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
if (jsonItemStack == null) continue;
jsonInventory.add(String.valueOf(i), jsonItemStack);
}
if (src instanceof PlayerInventory)
{
@ -124,6 +117,15 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
jsonInventory.addProperty(SIZE, itemStacks.length);
}
// Add the content at the end since we like to have it at the bottom of return json.
for (int i = 0; i < itemStacks.length; i++)
{
itemStack = itemStacks[i];
jsonItemStack = MCore.gson.toJsonTree(itemStack, ItemStack.class);
if (jsonItemStack == null) continue;
jsonInventory.add(String.valueOf(i), jsonItemStack);
}
return jsonInventory;
}
@ -142,15 +144,13 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
// 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())
if (jsonSize.isString() && jsonSize.getAsString().equals(PLAYER))
{
// Only makes sense if stating "player".
if (!jsonSize.getAsString().equals(PLAYER)) return null;
// We use 36 here since it's the size of the player inventory (without armor)
size = 36;
@ -190,7 +190,7 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
pret.setBoots(itemStack);
}
}
else if (jsonSize.isNumber())
else
{
// A custom size were specified
size = jsonSize.getAsInt();
@ -198,11 +198,6 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
// This is a "Custom" Inventory (content only).
ret = new CraftInventoryCustom(null, size);
}
else
{
// It must be either string or number
return null;
}
// Now process content
ItemStack[] itemStacks = new ItemStack[size];

View File

@ -112,6 +112,15 @@ public class InventoryUtil
}
}
// -------------------------------------------- //
// CREATE PLAYER INVENTORY
// -------------------------------------------- //
public static PlayerInventory createPlayerInventory()
{
return new CraftInventoryPlayer(new MCorePlayerInventory());
}
// -------------------------------------------- //
// IS EMPTY?
// -------------------------------------------- //
@ -179,8 +188,7 @@ public class InventoryUtil
if (inventory instanceof PlayerInventory)
{
MCorePlayerInventory nmsret = new MCorePlayerInventory();
CraftInventoryPlayer pret = new CraftInventoryPlayer(nmsret);
PlayerInventory pret = createPlayerInventory();
ret = pret;
PlayerInventory pinventory = (PlayerInventory)inventory;
@ -201,6 +209,30 @@ public class InventoryUtil
return ret;
}
// -------------------------------------------- //
// SET CONTENT
// -------------------------------------------- //
// This one simply moves the content pointers from on inventory to another.
// You may want to clone the from inventory first.
public static void setAllContents(Inventory from, Inventory to)
{
to.setContents(from.getContents());
if (from instanceof PlayerInventory)
{
PlayerInventory pfrom = (PlayerInventory)from;
if (to instanceof PlayerInventory)
{
PlayerInventory pto = (PlayerInventory)to;
pto.setHelmet(pfrom.getHelmet());
pto.setChestplate(pfrom.getChestplate());
pto.setLeggings(pfrom.getLeggings());
pto.setBoots(pfrom.getBoots());
}
}
}
// -------------------------------------------- //
// CAN I ADD MANY?
// -------------------------------------------- //