NBT
NBT (Named Binary Tags) is a binary format used by Minecraft to store data. It is commonly used for transferring chunks data, items data, and other game-related information over the network. In this section, we will explore how to work with NBT in your plugins.
Example Compound Tag
Section titled “Example Compound Tag”var tag = new NbtCompound{ ["hello"] = new NbtString("world"), ["number"] = new NbtInt(69), ["list"] = new NbtList([new NbtInt(420)], NbtTagType.Int), ["child"] = new NbtCompound { ["key"] = new NbtString("value"), }};
Tag Types
Section titled “Tag Types”NbtByte
: Represents a byte value.NbtByteArray
: Represents an array of byte values.NbtCompound
: Represents a compound tag, which can contain other tags.NbtDouble
: Represents a double value.NbtEnd
: Represents the end of a compound tag.NbtFloat
: Represents a float value.NbtIntArray
: Represents an array of integer values.NbtInt
: Represents an integer value.NbtList
: Represents a list of tags.NbtLongArray
: Represents an array of long values.NbtLong
: Represents a long value.NbtShort
: Represents a short value.NbtString
: Represents a string value.
Reading NBT Tag
Section titled “Reading NBT Tag”To read NBT data, you can use the NbtTag
class.
var bytes = new byte[] { ... }; // Your NBT data as byte array
// length is the number of bytes readvar length = NbtTag.Parse(bytes, out var tag);
logger.LogInformation("Tag type: {Type}", tag.Type);
if (tag is NbtCompound compound){ if (compound["key"] is NbtString key) { logger.LogInformation("{Value}", key.Value); }}
Writing NBT Tag
Section titled “Writing NBT Tag”Use AsStream()
method on your tag instance to access NBT as a byte stream.
var tag = new NbtCompound{ ["key"] = new NbtString("value"),};
var stream = tag.AsStream();var bytes = stream.ToArray();
Snbt is a human-readable format for NBT data. Read more about Snbt.
Converting Nbt to Snbt
Section titled “Converting Nbt to Snbt”var tag = new NbtCompound { ["key"] = new NbtString("value") };Console.WriteLine(tag.ToString()); // Output: {key:"value"}
Converting Snbt to Nbt
Section titled “Converting Snbt to Nbt”var snbt = "{key:\"value\"}";var tag = NbtTag.Parse(snbt);