Files
go-mc/nbt/README.md
2023-04-26 23:17:49 +08:00

1.7 KiB

NBT Go Reference

This package implements the Named Binary Tag format of Minecraft.

The API is very similar to the standard library encoding/json. (But fix some its problem) If you (high probability) have used that, it is easy to use this.

Supported Struct Tags and Options

  • nbt - The primary tag name. See below.
  • nbtkey - The key name of the field (Used to support commas , in tag names)

The nbt tag

In most cases, you only need this one to specify the name of the tag.

The format of nbt struct tag: <nbt tag>[,opt].

It's a comma-separated list of options. The first item is the name of the tag, and the rest are options.

Like this:

type MyStruct struct {
    Name string `nbt:"name"`
}

To tell the encoder not to encode a field, use -:

type MyStruct struct {
    Internal string `nbt:"-"`
}

To tell the encoder to skip the field if it is zero value, use omitempty:

type MyStruct struct {
    Name string `nbt:"name,omitempty"`
}

Fields typed []byte, []int32 and []int64 will be encoded as TagByteArray, TagIntArray and TagLongArray respectively by default. You can override this behavior by specifying encode them asTagList by using list:

type MyStruct struct {
    Data []byte `nbt:"data,list"`
}

The nbtkey tag

Common issue with JSON standard libraries: inability to specify keys containing commas for structures. (e.g {"a,b" : "c"})

So this is a workaround for that:

type MyStruct struct {
    AB string `nbt:",omitempty" nbtkey:"a,b"`
}