NBTCompound - ShaneBeee/SkBee GitHub Wiki

NBTCompounds are an advanced NBT object which offers more functionality than the normal NBT strings.

Get

So how do we get an NBT compound? Well its simple.
We can get the compound from a block (tile/block entity, such as a furnace, hopper, campfire, etc), entity, item or a new compound from a string.
The optional copy will return a copy of the NBT compound. This is useful when you don't want to manipulate the actual NBT compound of the object.
Do note that NBT compounds of items and files will always return a copy.
Syntaxes:

nbt compound [copy] (of|from) %blocks/entities/itemtypes/itemstacks/slots/strings%
nbt compound (of|from) file[s] %strings%

Examples:

set {_n} to nbt compound of target block of player
set {_n} to nbt compound of event-entity
set {_n} to nbt compound of player's tool
set {_n} to nbt compound of (diamond sword of unbreaking 2 named "Mr Strong Stuff")
set {_n} to nbt compound from "{}" #creates a blank compound
set {_n} to nbt compound from "{name:""blah"",age:1b}" #creates a compound from a string
set {_n} to nbt compound from {some::old::nbt::string::variable}
set {_n} to nbt compound of file "world/playerdata/some-uuid.dat"

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.
Syntax:

tag %string% of %nbtcompound%

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 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 tag "pos" of {_nbt}

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

Set

Need to change a tag? No problem. ex:

set 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 will not update automatically (silly NBT), you will need to replace the item.
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).
ex:

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

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.

Tag Types

Each part of an NBT compound will have a different type. When getting a tag, this will automatically return the correct type. When setting a tag that is already set previously, Minecraft will keep the same tag type. When setting a new tag SkBee will try its best to match the correct type. Sometimes, it won't set exactly how you want it to set. For example, you may be trying to set an integer tag, but SkBee will try to set it as a byte tag (which will have a max of 127). In these cases, you can specially pick which tag type you want.
The expression is as follows:

tag %string% of %string/nbtcompound% 
%string% tag of %string/nbtcompound% 
%nbttype% %string% of %nbtcompound% 
%string% %nbttype% of %nbtcompound% 

The tag types are:

byte array tag, byte tag, compound list tag, compound tag, double list tag, double tag, 
float list tag, float tag, int array tag, int list tag, int tag, long list tag, long tag, 
short tag, string list tag, string tag

Here is an example of setting a tag type:

set {_n} to nbt compound of player's tool
set int tag of {_n} to 10
set {_i} to nbt item from {_n}

You can also find out the type of tag:

[nbt[ ]]tag[ ]type of tag %string% of %nbtcompound%

This will tell you exactly what the tag type is if it's already set.

Entity/Block Custom Tags

NOTE: This is only supported on Minecraft 1.14+ for entities and tile entities (blocks such as chests and furnaces), and Minecraft 1.16.4+ for every other block.

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. This 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 tag "custom" of {_nbt}

This will retrieve the custom NBT compound.

You can also get nested tags:

set {_p} to 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 tag "custom" of nbt compound of player
set tag "points" of {_custom} to 10
set tag "game" of {_custom} to "MyGame"

Add

You can add to the custom tag if you'd like

add "{custom:{points:10,game:""MyGame""}}" to nbt of target entity

Delete

If you delete the custom tag:

delete tag "custom" of {_nbt}

You can also delete nested tags

delete tag "custom;points" of {_nbt}

File

When getting the NBT compound of a file, if the file isn't already created, SkBee will create it.
Any changes made to the compound will need to be saved using the save NBT file effect.

set {_n} to nbt compound from file "plugins/myplugin/test.nbt"
set tag "my-tag" of {_n} to "my value"

save nbt file of {_n} #this will save all changes to file