Skip to content

NBT

NBT (Named Binary Tags) is a binary format used by Minecraft to store data. It is commonly used for transferring chunk data, item data, and other game-related information over the network. In this section, we will explore how to work with NBT in your plugins.

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"),
}
};

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 read
var 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);
}
}

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.

var tag = new NbtCompound { ["key"] = new NbtString("value") };
Console.WriteLine(tag.ToString()); // Output: {key:"value"}
var snbt = "{key:\"value\"}";
var tag = NbtTag.Parse(snbt);