The Named Binary Tag format is used by Minecraft for the various files in which it saves data. The format is designed to store data in a tree structure made up of various tags, each one with an ID and a name.
More on NBT can be found on Minecraft Wiki.
All 12 tags are provided in the module
sel.nbt.tags and publicly imported in the module
Every tag is a class that extends the class
Tag and may contain extra methods for working with the type, as documented below.
Tag is the base abstract class for every tag and provides the basic properties and methods:
- The property
typecan be used to retrieve the type of the tag. It returns a type of the enum
- The property
namecan be used to retrieve the name of the tag, if there is one.
- The method
rename(string)can be used to rename the tag. A new instance of the tag is created by this method.
decode(Stream)are used to encode and decode the tag from a stream of bytes. See the Encoding and decoding section for more informations about the tag's serialisation.
toJSON()converts the tag to a
toString()converts the tag to a human-readable string.
Named is a templated that can be used to create named tags, adding a string before the other constructor arguments of the tag.
Tag a = new Byte(12); Tag b = new Named!Byte("name", 12); assert(a.name == ""); assert(b.name == "name"); assert(a == b);
Simple tags are
String, they can be compared between each others and with their basic type. Operations are also supported.
Tag a = new Short(44); assert(a == 44); assert(a == new Short(44)); assert(a > 40); a /= 2; a += 5; assert(++a == 28);
Array tags are
LongArray, they can be compared with their respective tag and with their basic array type. All array operations are supported and concatenation to create a new tag can also be done.
Tag a = new IntArray(1, 2, 3); assert(a == [1, 2, 3]); assert(a == 1); assert(a.length == 3); a ~= [4, 5]; assert(a == [1, 2, 3, 4, 5]);
Encoding and Decoding
Every tag can be encoded and decoded (serialised and deserialised) using the one of the derivate of the
Stream abstract class, located in module
sel.nbt.stream and publicly imported in
The sub-classes of
ClassicStream(Endian), where numbers are encoded as either big-endian or little-endian, and
NetworkStream(Endian), where some numbers such as lengths and integers are encoded as google varint.
ClassicStream is usually used by the Java Edition of Minecraft to both save world data and send data through the netowrk, while little-endian
ClassicStream is used by Minecraft (Bedrock Engine) to save world data and little-endian
NetworkStream to send data through the network.
The methods provided by the
Stream to read and write tags are
Stream stream = new ClassicStream!(Endian.bigEndian)(); stream.writeTag(new Byte(12)); assert(stream.data == [1, 0, 0, 12]);
Every tag can be converted to JSON and every JSON value can be converted to a NBT tag.
toNBT functions are located in the module
sel.nbt.json and publicly imported in