Posted June 19, 201411 yr I'm not sure whether it's not saving properly or not loading properly. I can put items into the slots, but when I close and reopen, it isn't there. Inventory Class: package ttm.inventory; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; public class InventoryPlayerExtended implements IInventory { public EntityPlayer player; public ItemStack[] inventory = new ItemStack[9]; public InventoryPlayerExtended(EntityPlayer player) { this.player = player; } @Override public ItemStack decrStackSize(int par1, int par2) { ItemStack[] aitemstack = this.inventory; if(aitemstack[par1] != null) { ItemStack itemstack; if(aitemstack[par1].stackSize <= par2) { itemstack = aitemstack[par1]; aitemstack[par1] = null; return itemstack; } else { itemstack = aitemstack[par1].splitStack(par2); if(aitemstack[par1].stackSize == 0) { aitemstack[par1] = null; } return itemstack; } } else { return null; } } @Override public ItemStack getStackInSlotOnClosing(int par1) { ItemStack[] aitemstack = this.inventory; if(aitemstack[par1] != null) { ItemStack itemstack = aitemstack[par1]; aitemstack[par1] = null; return itemstack; } else { return null; } } @Override public void setInventorySlotContents(int par1, ItemStack par2ItemStack) { inventory[par1] = par2ItemStack; } @Override public int getSizeInventory() { return this.inventory.length; } @Override public ItemStack getStackInSlot(int par1) { return inventory[par1]; } public void dropAllItems() { int i; for(i = 0; i < this.inventory.length; ++i) { if(this.inventory[i] != null) { this.player.func_146097_a(this.inventory[i], true, false); this.inventory[i] = null; } } } public void copyInventory(InventoryPlayerExtended inventory) { int i; for(i = 0; i < this.inventory.length; ++i) { this.inventory[i] = ItemStack.copyItemStack(inventory.inventory[i]); } } public NBTTagList writeToNBT(NBTTagList par1NBTTagList) { System.err.println("WRITE"); int i; NBTTagCompound nbttagcompound; for(i = 0; i < this.inventory.length; ++i) { if(this.inventory[i] != null) { nbttagcompound = new NBTTagCompound(); nbttagcompound.setByte("Slot", (byte) (i)); this.inventory[i].writeToNBT(nbttagcompound); par1NBTTagList.appendTag(nbttagcompound); } } return par1NBTTagList; } public void readFromNBT(NBTTagList par1NBTTagList) { System.err.println("READ"); this.inventory = new ItemStack[9]; for(int i = 0; i < par1NBTTagList.tagCount(); ++i) { NBTTagCompound nbttagcompound = par1NBTTagList.getCompoundTagAt(i); int j = nbttagcompound.getByte("Slot") & 255; ItemStack itemstack = ItemStack.loadItemStackFromNBT(nbttagcompound); if(itemstack != null) { if(j >= 0 && j < this.inventory.length) { this.inventory[j] = itemstack; } } } } @Override public String getInventoryName() { return "container.inventory.extended"; } @Override public boolean hasCustomInventoryName() { return false; } @Override public int getInventoryStackLimit() { return 64; } @Override public void markDirty() { } @Override public boolean isUseableByPlayer(EntityPlayer var1) { return true; } @Override public void openInventory() { if(!player.worldObj.isRemote) { NBTTagList nbttaglist = player.getEntityData().getTagList("InventoryExtended", 10); this.readFromNBT(nbttaglist); } } @Override public void closeInventory() { if(!player.worldObj.isRemote) { NBTTagList nbttaglist = player.getEntityData().getTagList("InventoryExtended", 10); this.writeToNBT(nbttaglist); } } @Override public boolean isItemValidForSlot(int var1, ItemStack var2) { return true; } public static InventoryPlayerExtended forPlayer(EntityPlayer player) { InventoryPlayerExtended res = new InventoryPlayerExtended(player); res.openInventory(); return res; } } The prints in the read and write methods are being called correctly. Kain
June 19, 201411 yr This code does not do what you think it does: NBTTagCompound nbttagcompound; for(i = 0; i < this.inventory.length; ++i) { if(this.inventory[i] != null) { nbttagcompound = new NBTTagCompound(); nbttagcompound.setByte("Slot", (byte) (i)); this.inventory[i].writeToNBT(nbttagcompound); par1NBTTagList.appendTag(nbttagcompound); } } Your problem is that nbttagcompound is a refrence to a NBtTag, so when you set it to a new NBTCompountTag, you are overwriting the previous one. Also, there light already be Nbt tags in the TagList, so you need to save a starting point to read from. Also, I think the method you want to override is writeToNBT(NBTTagCompound), not writeToNBT(NBTTagList), though it might have changed (I'm in 1.6.4). Here is what I use (It works): @Override public void writeToNBT(NBTTagCompound compound) { super.writeToNBT(compound); NBTTagList items = new NBTTagList(); for (int i = 0; i < getSizeInventory(); i++) { ItemStack stack = getStackInSlot(i); if (stack != null) { NBTTagCompound item = new NBTTagCompound(); item.setByte("Slot", (byte)i); stack.writeToNBT(item); items.appendTag(item); } } compound.setTag("Items", items); } @Override public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound); NBTTagList items = compound.getTagList("Items"); for (int i = 0; i < items.tagCount(); i++) { NBTTagCompound item = (NBTTagCompound)items.tagAt(i); int slot = item.getByte("Slot"); if (slot >= 0 && slot < getSizeInventory()) { setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item)); } } }
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.