package imdutch21.oilcraft.util; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; /** * Created by Bart on 12/02/2016. */ public class InventoryUtils { public static void saveInventory(NBTTagCompound compound, IInventory inventory) { NBTTagList items = new NBTTagList(); for (int i = 0; i < inventory.getSizeInventory(); i++) { ItemStack stack = inventory.getStackInSlot(i); if (stack != null) { NBTTagCompound item = new NBTTagCompound(); item.setByte("Slot", (byte) i); stack.writeToNBT(item); items.appendTag(item); } } compound.setTag("Items", items); } public static void loadInventory(NBTTagCompound compound, IInventory inventory) { NBTTagList items = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND); for (int i = 0; items.tagCount() > i; i++) { NBTTagCompound item = items.getCompoundTagAt(i); int slot = item.getByte("Slot"); if (slot >= 0 && slot < inventory.getSizeInventory()) { inventory.setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item)); } } } }