Jump to content

[1.7.2] Saving Inventory - NBT


SwordsMiner

Recommended Posts

I cant seem to get my TileEntity to save its NBT, ive looked at updated 1.7.2 tutorials on saving NBT and it just wont work.

 

Here is my code:

 

public Packet getDescriptionPacket() {
	NBTTagCompound nbtTag = new NBTTagCompound();
	this.writeToNBT(nbtTag);
	return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
}

public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
	readFromNBT(packet.func_148857_g());
}

public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
	super.readFromNBT(par1NBTTagCompound);
        NBTTagList tagList = par1NBTTagCompound.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
        for (int i = 0; i < tagList.tagCount(); i++) 
          {
                NBTTagCompound tag = (NBTTagCompound) tagList.getCompoundTagAt(i);
                byte slot = tag.getByte("Slot");                                                 
                if (slot >= 0 && slot < slots.length)   
                  {
                	slots[slot] = ItemStack.loadItemStackFromNBT(tag); 
                  }                                                                                                
          }
}

public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
	super.writeToNBT(par1NBTTagCompound);

	NBTTagList itemList = new NBTTagList();
        for (int i = 0; i < slots.length; i++)                             
          {
                ItemStack stack = slots[i];                                               
                if (stack != null)                                                                      
                  {
                        NBTTagCompound tag = new NBTTagCompound();  
                        tag.setByte("Slot", (byte) i);                   
                        stack.writeToNBT(tag);  
                        itemList.appendTag(tag);
                  }
          }
        par1NBTTagCompound.setTag("Inventory", itemList);     
}

 

Slots is indeed the itemstack array used by the container/gui.

 

I'll give any more code if needed. ;)

Link to comment
Share on other sites

I don't see any constructors, but...

When the game reloads it will call a constructor with no arguments and then call "readFromNBT(...)". If there is no empty constructor available it will give up.

When this is the problem Eclipse throws a bunch of red text at me upon attempts to load the world. Pay attention to the red text.

Link to comment
Share on other sites

When this is the problem Eclipse throws a bunch of red text at me upon attempts to load the world. Pay attention to the red text.

 

Thanks, Ive been doing this for a long time so I am looking over exceptions since vanilla minecraft makes so many. Thanks again!

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.