From f8d29d98aa6d6de6ac5948aec18952ca7816cbe9 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 23 Mar 2013 17:24:38 +0100 Subject: [PATCH] More tweaks to the inventory stuff --- .../mcore/adapter/InventoryAdapter.java | 31 +++++++--------- .../mcore/util/InventoryUtil.java | 36 +++++++++++++++++-- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/com/massivecraft/mcore/adapter/InventoryAdapter.java b/src/com/massivecraft/mcore/adapter/InventoryAdapter.java index f5867abe..1a9f661e 100644 --- a/src/com/massivecraft/mcore/adapter/InventoryAdapter.java +++ b/src/com/massivecraft/mcore/adapter/InventoryAdapter.java @@ -68,15 +68,8 @@ public class InventoryAdapter implements JsonDeserializer, 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, 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, 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, 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, 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]; diff --git a/src/com/massivecraft/mcore/util/InventoryUtil.java b/src/com/massivecraft/mcore/util/InventoryUtil.java index 8a6475b1..de01f523 100644 --- a/src/com/massivecraft/mcore/util/InventoryUtil.java +++ b/src/com/massivecraft/mcore/util/InventoryUtil.java @@ -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? // -------------------------------------------- //