Tutorial 14. NBT Tags

Before you read, make sure you have seen the previous tutorial

Code is on github

So, what is a NBT-Tag?
A NBT-Tag is a little bit of information, which is stored.
You can store NBT’s for almost everything. Think of for example: ItemStacks in a container, Health on a player, Any kind of world parameter, etc etc etc.


In this tutorial, i will store the itemstacks, placed in our container. So make sure you watched my previous tutorial

So, in TileEntity (the minecraft class) there are 2 methods, we need to worry about. readFromNBT and writeToNBT. Quite obviously, one reads the NBT, and one writes the NBT.

Lets worry about writing first.

 

So. The most efficient way to store, is to make a list of NBTTags.
In this list, we will store NBTTagCompounds. (A NBTTag which can contain anything. Not specially an int, byte etc).
So, for all the slots, check if they’re not null, aka have an itemstack stored.
If it contains an itemstack, create an NBTTag with the slot id, and the name “Slot”. This way, we can later read what Slot had the itemstack.
After that, we add the itemstack to the NBTTagCompound. FInally, we add the NBTTagCompound to the list. You do this, by .appendTag.

Finally, we set the tag of our original NBTTagCompount to Items, and add our NBTTagList.
Confusing, but see this. (Click to enlarge)
9f8382aae7587ea2e5e45cf777408b70
So, in the NBTTag we got from the method, we store our NBTList.
That list, contains a few NBTTagCompounds. Those compounds, contain a slotID with as name “Slot”. And they contain the ItemStack. Makes sense now right?

Cool, now lets worry about reading.

So, we get the NBTTagCompound. We KNOW it contains a list right?
Get the list, containing NBTTagCompounds.
We re-initialize the inventory, setting the size.
Then, for all the NBTTag compounds, we read the Slot Tag, and store that in the int j.
If the j is within our slotrange, add the inventory to the j-index slot.
Not too hard i’d say.

 

More about NBT’s


Thanks to my friend hellgast23 and PaleoCrafter

Back to List

Next tutorial


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">