Skip to content

NBT Compound Tags

Shane Bee edited this page Apr 29, 2024 · 7 revisions

Tags

As you may have learned before, NBT works with a key/value system. We refer to these keys as "tags".
Tags allow us to get/set/delete different parts of the NBT compound.

Get

If we need to get the specific element of an NBT compound, we simply grab its tag.
Here are the syntaxes: Syntax:

%nbttype% %string% of %nbtcompound%"
%string% %nbttype% of %nbtcompound%"

You can find all available nbt type's on SkriptHub Docs
You can also read more about NBT data types on McWiki

For the sake of this tutorial, we're going to use a made up NBT compound:

{name:"ShaneBee",health:10.0,pos:[1,64,1]}

So let's say you wanna get the name from the compound:

set {_name} to string tag "name" of {_nbt}

This will return the string "ShaneBee".
This getter will return the correct type, if its a list, use a list variable.

set {_pos::*} to int list tag "pos" of {_nbt}

This will return the pos tag as a list 1, 64, 1

Set

Need to change a tag? No problem. ex:

set string tag "name" of {_nbt} to "Bob"

If the NBT compound is from an entity/block, it will update it directly.

Items on the other hand work a little different.
As we learned in the NBT COMPOUND wiki, there are two different ways to get NBT of an item, see HERE
Let's look at both ways.

NBT Compound

This method will return to us all the data within the "tag" compound.
Since we are already within the "tag" compound, we don't need to type that again.
When manipulating NBT in this compound, it will directly be applied to the item.
EX:

set int tag "PlayerPoints" of nbt compound of player's tool to 10
# OR
set {_n} to nbt compound of player's tool
set int tag "PlayerPoints" of {_n} to 10
# OR
set {_i} to diamond sword named "BOB" with lore "OOO LOOK... LORE"
set {_n} to nbt compound of {_i}
set int tag "PlayerPoints" of {_n} to 10
give player 1 of {_i}

Full NBT compound

When using this method, the original item will not be changed. This can have some benefits, for example if you want to save the item for use later, such as in a file/yaml/variable/etc. It's a great way for serialization.
When setting custom data in items, everything needs to be within the "tag" tag.
This is just how Minecraft stores data on items (see example).
Once we do this, we will use another expression to get the Item from the NBT compound:

item[s] (from|of) nbt[s] %nbtcompounds%
nbt item[s] (from|of) %nbtcompounds%

ex:

set {_nbt} to full nbt compound of player's tool
set int tag "tag;SomeTag" of {_nbt} to 10
set player's tool to item from nbt {_nbt}

Easy peasy right? RIGHT? RIGHT?

Delete

Need to delete a tag? Yeah, we can do that too. ex:

delete tag "SomeTag" of {_nbt}

Be careful when deleting tags from entities/blocks as it may screw them up. In most cases, Minecraft should default the value, but you never know. (Don't say I didn't warn you).

Nested Tags

NBT compounds may contain compounds within compounds.
If you want to retrieve an inner compound you can use a nested tag.
Let's use this compound as an example:
{a:{b:{c:1}}}
As you can see, we have the "c" compound within the "b" compound within the "a" compound.

If we want to get the value of "c" we simple use a nested tag, which is just each tag separated by the delimiter ;:

set {_c} to int tag "a;b;c" of {_nbt}

We can do the same with setting:

set int tag "custom;points" of nbt of player to 10
set float tag "abilities;walkSpeed" of nbt of player to 0.2

Entity/Block Custom Tags

Vanilla Minecraft does not natively support custom NBT on blocks and entities.
With some hacky methods I was able to make this work.
That said, this system is a little more confusing that other methods.
You will not be able to put your custom tags wherever you want all willy nilly, they will need to hang out in the "custom" tag of the compound.
The "custom" tag is specific to SkBee.

Get

Like the rest of NBT you can simply get the custom NBT compound, which will store all of your custom tags.

set {_nbt} to nbt compound of target entity
set {_custom} to compound tag "custom" of {_nbt}

This will retrieve the custom NBT compound.

You can also get nested tags:

set {_p} to int tag "custom;points" of {_nbt}

Set

To set custom NBT in a block/entity, use the "custom" tag. Everything you add needs to go in this tag.

set {_custom} to compound tag "custom" of nbt compound of player
set int tag "points" of {_custom} to 10
set string tag "game" of {_custom} to "MyGame"

Delete

You can also delete the "custom" tag.
NOTE: This will not actually delete the compound itself, but the values inside the compound.

delete tag "custom" of {_nbt}

You can also delete nested tags

delete tag "custom;points" of {_nbt}