Documentation ¶
Overview ¶
Package nbt implements the NBT formats used by Minecraft Bedrock Edition and Minecraft Java Edition. These formats are a little endian format, a big endian format and a little endian format using varints (typically used over network in Bedrock Edition).
The package exposes serialisation and deserialisation roughly the same way as the JSON standard library does, using nbt.Marshal() and nbt.Unmarshal when working with byte slices, and nbt.NewEncoder() and nbt.NewDecoder() when working with readers or writers.
The package encodes and decodes the following Go types with the following NBT tags.
byte/uint8: TAG_Byte bool: TAG_Byte int16: TAG_Short int32: TAG_Int int64: TAG_Long float32: TAG_Float float64: TAG_Double [...]byte: TAG_ByteArray [...]int32: TAG_IntArray [...]int64: TAG_LongArray string: TAG_String []<type>: TAG_List struct{...}: TAG_Compound map[string]<type/interface{}>: TAG_Compound
Structures decoded or encoded may have struct field tags in a comparable way to the JSON standard library. The 'nbt' struct tag may be filled out the following ways:
'-': Ignores the field completely when encoding and decoding. ',omitempty': Doesn't encode the field if its value is the same as the default value. 'name(,omitempty)': Encodes/decodes the field with a different name than its usual name.
If no 'nbt' struct tag is present for a field, the name of the field will be used to encode/decode the struct. Note that this package, unlike the JSON standard library package, is case sensitive when decoding.
Index ¶
- Variables
- func Dump(data []byte, encoding Encoding) (string, error)
- func Marshal(v interface{}) ([]byte, error)
- func MarshalEncoding(v interface{}, encoding Encoding) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalEncoding(data []byte, v interface{}, encoding Encoding) error
- type BufferOverrunError
- type Decoder
- type Encoder
- type Encoding
- type FailedWriteError
- type IncompatibleTypeError
- type InvalidArraySizeError
- type InvalidStringError
- type InvalidTypeError
- type MaximumBytesReadError
- type MaximumDepthReachedError
- type NonPointerTypeError
- type UnexpectedNamedTagError
- type UnexpectedTagError
- type UnknownTagError
Constants ¶
This section is empty.
Variables ¶
var BigEndian bigEndian
BigEndian is the fixed size big endian implementation of NBT. It is the original implementation, and is used only on Minecraft Java Edition.
var LittleEndian littleEndian
LittleEndian is the fixed size little endian implementation of NBT. It is the format typically used for writing Minecraft (Bedrock Edition) world saves.
var NetworkLittleEndian networkLittleEndian
NetworkLittleEndian is the variable sized integer implementation of NBT. It is otherwise the same as the normal little endian NBT. The NetworkLittleEndian format limits the total bytes of NBT that may be read. If the limit is hit, the reading operation will fail immediately.
Functions ¶
func Dump ¶
Dump returns a human readable decoded version of a serialised slice of NBT encoded using the encoding that is passed. Types are printed using the names present in the doc.go file and nested tags are indented using a single tab. Due to the nature of NBT, TAG_Compounds will not be printed in the same order. A different result is to be expected every time Dump is called, due to the random ordering.
If the serialised NBT data passed is not parsable using the encoding that was passed, an error is returned and the resulting string will always be empty.
func Marshal ¶
Marshal encodes an object to its NBT representation and returns it as a byte slice. It uses the NetworkLittleEndian NBT encoding. To use a specific encoding, use MarshalEncoding.
The Go value passed must be one of the values below, and structs, maps and slices may only have types that are found in the list below.
The following Go types are converted to tags as such:
byte/uint8: TAG_Byte bool: TAG_Byte int16: TAG_Short int32: TAG_Int int64: TAG_Long float32: TAG_Float float64: TAG_Double [...]byte: TAG_ByteArray [...]int32: TAG_IntArray [...]int64: TAG_LongArray string: TAG_String []<type>: TAG_List struct{...}: TAG_Compound map[string]<type/interface{}>: TAG_Compound
Marshal accepts struct fields with the 'nbt' struct tag. The 'nbt' struct tag allows setting the name of a field that some tag should be decoded in. Setting the struct tag to '-' means that field will never be filled by the decoding of the data passed. Suffixing the 'nbt' struct tag with ',omitempty' will prevent the field from being encoded if it is equal to its default value.
func MarshalEncoding ¶
MarshalEncoding encodes an object to its NBT representation and returns it as a byte slice. Its functionality is identical to that of Marshal, except it accepts any NBT encoding.
func Unmarshal ¶
Unmarshal decodes a slice of NBT data into a pointer to a Go values passed. Marshal will use the NetworkLittleEndian encoding by default. To use a specific encoding, use UnmarshalEncoding.
The Go value passed must be a pointer to a value. Anything else will return an error before decoding. The following NBT tags are decoded in the Go value passed as such:
TAG_Byte: byte/uint8(/interface{}) or bool TAG_Short: int16(/interface{}) TAG_Int: int32(/interface{}) TAG_Long: int64(/interface{}) TAG_Float: float32(/interface{}) TAG_Double: float64(/interface{}) TAG_ByteArray: [...]byte(/interface{}) (The value must be a byte array, not a slice) TAG_String: string(/interface{}) TAG_List: []interface{}(/interface{}) (The value type of the slice may vary. Depending on the type of values in the List tag, it might be of the type of any of the other tags, such as []int64.
TAG_Compound: struct{...}/map[string]interface{}(/interface{}) TAG_IntArray: [...]int32(/interface{}) (The value must be an int32 array, not a slice) TAG_LongArray: [...]int64(/interface{}) (The value must be an int64 array, not a slice)
Unmarshal returns an error if the data is decoded into a struct and the struct does not have all fields that the matching TAG_Compound in the NBT has, in order to prevent the loss of data. For varying data, the data should be decoded into a map. Nil maps and slices are initialised and filled out automatically by Unmarshal.
Unmarshal accepts struct fields with the 'nbt' struct tag. The 'nbt' struct tag allows setting the name of a field that some tag should be decoded in. Setting the struct tag to '-' means that field will never be filled by the decoding of the data passed.
func UnmarshalEncoding ¶
UnmarshalEncoding decodes a slice of NBT data into a pointer to a Go values passed using the NBT encoding passed. Its functionality is identical to that of Unmarshal, except that it allows a specific encoding.
Types ¶
type BufferOverrunError ¶
type BufferOverrunError struct {
Op string
}
BufferOverrunError is returned when the data buffer passed in when reading is overrun, meaning one of the reading operations extended beyond the end of the slice.
type Decoder ¶
type Decoder struct { // Encoding is the variant to use for decoding the NBT passed. By default, the variant is set to // NetworkLittleEndian, which is the variant used for network NBT. Encoding Encoding // contains filtered or unexported fields }
Decoder reads NBT objects from an NBT input stream.
func NewDecoder ¶
NewDecoder returns a new Decoder for the input stream reader passed.
func NewDecoderWithEncoding ¶
NewDecoderWithEncoding returns a new Decoder for the input stream reader passed with a specific encoding.
type Encoder ¶
type Encoder struct { // Encoding is the variant to use for encoding the objects passed. By default, the variant is set to // NetworkLittleEndian, which is the variant used for network NBT. Encoding Encoding // contains filtered or unexported fields }
Encoder writes NBT objects to an NBT output stream.
func NewEncoder ¶
NewEncoder returns a new encoder for the output stream writer passed.
func NewEncoderWithEncoding ¶
NewEncoderWithEncoding returns a new encoder for the output stream writer passed using a specific encoding. It is identical to calling NewEncoder and setting the Encoding field manually.
type Encoding ¶
type Encoding interface { Int16(r *offsetReader) (int16, error) Int32(r *offsetReader) (int32, error) Int64(r *offsetReader) (int64, error) Float32(r *offsetReader) (float32, error) Float64(r *offsetReader) (float64, error) String(r *offsetReader) (string, error) WriteInt16(w *offsetWriter, x int16) error WriteInt32(w *offsetWriter, x int32) error WriteInt64(w *offsetWriter, x int64) error WriteFloat32(w *offsetWriter, x float32) error WriteFloat64(w *offsetWriter, x float64) error WriteString(w *offsetWriter, x string) error }
Encoding is an encoding variant of NBT. In general, there are three different encodings of NBT, which are all the same except for the way basic types are written.
type FailedWriteError ¶
FailedWriteError is returned if a Write operation failed on an offsetWriter, meaning some of the data could not be written to the io.Writer.
type IncompatibleTypeError ¶
IncompatibleTypeError is returned if a value is attempted to be written to an io.Writer, but its type can- not be translated to an NBT tag.
type InvalidArraySizeError ¶
InvalidArraySizeError is returned when an array read from the NBT (that includes byte arrays, int32 arrays and int64 arrays) does not have the same size as the Go representation.
type InvalidStringError ¶
InvalidStringError is returned if a string read is not valid, meaning it does not exist exclusively out of utf8 characters, or if it is longer than the length prefix can carry.
type InvalidTypeError ¶
InvalidTypeError is returned when the type of a tag read is not equal to the struct field with the name of that tag.
type MaximumBytesReadError ¶
type MaximumBytesReadError struct { }
MaximumBytesReadError is returned if the maximum amount of bytes has been read for NetworkLittleEndian format. It is returned if the offset hits maximumNetworkOffset.
type MaximumDepthReachedError ¶
type MaximumDepthReachedError struct { }
MaximumDepthReachedError is returned if the maximum depth of 512 compound/list tags has been reached while reading or writing NBT.
func (MaximumDepthReachedError) Error ¶
func (err MaximumDepthReachedError) Error() string
Error ...
type NonPointerTypeError ¶
NonPointerTypeError is returned when the type of a value passed in Decoder.Decode or Unmarshal is not a pointer.
type UnexpectedNamedTagError ¶
UnexpectedNamedTagError is returned when a named tag is read from a compound which is not present in the struct it is decoded into.
type UnexpectedTagError ¶
UnexpectedTagError is returned when a tag type encountered was not expected, and thus valid in its context.
type UnknownTagError ¶
UnknownTagError is returned when the type of a tag read is not known, meaning it is not found in the tag.go file.