/** * Standard method to write the inventory data of this TE to the NBTCompound that stores this TE's * Data. * * @return A NBTTagList with all stacks in the inventory. */ protected NBTBase writeInventoryToCompound() { IItemStorage inventory = (IItemStorage) this; NBTTagList inventoryList = new NBTTagList(); for (int i = 0; i < inventory.getSizeInventory(); i++) { ItemStack stack = inventory.getStackInSlot(i); if (stack == null) continue; NBTTagCompound slotCompound = new NBTTagCompound(); slotCompound.setInteger(CoreReferences.NBT.InventoryData.SLOTINDEX, i); slotCompound.setTag( CoreReferences.NBT.InventoryData.STACKDATA, stack.writeToNBT(new NBTTagCompound())); inventoryList.appendTag(slotCompound); } return inventoryList; }
/** * Standard method to read the inventory data of this TE from the NBTCompound that stores this * TE's Data. * * @param inventoryCompound A NBTBase instance in the form of a TagList containing all the Data of * the Slots in this inventory. */ protected void readInventoryFromCompound(NBTBase inventoryCompound) { if (inventoryCompound == null) return; if (!(inventoryCompound instanceof NBTTagList)) throw new IllegalArgumentException("The given store type is not compatible with this TE!"); IItemStorage inventory = (IItemStorage) this; NBTTagList inventoryList = (NBTTagList) inventoryCompound; ((IItemStorage) this).clearInventory(); for (int i = 0; i < inventoryList.tagCount(); i++) { NBTTagCompound slotCompound = (NBTTagCompound) inventoryList.get(i); ItemStack newStack = ItemStack.loadItemStackFromNBT( slotCompound.getCompoundTag(CoreReferences.NBT.InventoryData.STACKDATA)); inventory.setInventorySlotContents( slotCompound.getInteger(CoreReferences.NBT.InventoryData.SLOTINDEX), newStack); } }