/** * Loads items from the tag to the inventory. * * @param inventory * @param tag */ public static void loadInventory(IInventory inventory, NBTTagCompound tag) { /** Loading the items from the new format. */ if (tag.hasKey("Items")) { NBTTagList list = tag.getList("Items", 10); for (int i = 0; i < list.size(); i++) { NBTTagCompound tag2 = (NBTTagCompound) list.get(i); int slot = tag2.getByte("Slot") & 0xFF; if (slot < inventory.getSize()) { inventory.setItem(slot, ItemStack.createStack(tag2)); } } /** Loading the items from the old format. */ } else { for (int slot = 0; slot < inventory.getSize(); slot++) { if (tag.hasKey("Slot" + slot)) { inventory.setItem(slot, ItemStack.createStack(tag.getCompound("Slot" + slot))); } } } }
/** * Saves the inventories items to the tag. * * @param inventory * @param tag */ public static NBTTagCompound saveInventory(IInventory inventory, NBTTagCompound tag) { NBTTagList list = new NBTTagList(); for (int slot = 0; slot < inventory.getSize(); slot++) { ItemStack item = inventory.getItem(slot); if (item != null) { NBTTagCompound tag2 = new NBTTagCompound(); tag2.setByte("Slot", (byte) slot); item.save(tag2); list.add(tag2); } } tag.set("Items", list); return tag; }