nbt

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2022 License: MIT Imports: 9 Imported by: 0

README

NBT

Implementation of Minecraft's NBT file format in Go.

Features

  • Serialize structs to NBT directly
  • Deserialize NBT to structs directly
  • Support for arrays, maps, pointers, byte arrays, long arrays and integer arrays
  • Support encoding/TextMarshaler
  • Support custom encoding and decoding by implementing nbt.Marshaler and nbt.Unmarshaler
  • WIP Read from SNBT.
  • WIP Write to SNBT.

Examples

Writing NBT
type Color struct {
	Red   uint16 `nbt:"red"`
	Green uint16 `nbt:"green"`
	Blue  uint16 `nbt:"blue"`
}

func main() {
	color := Color{
		Red:   120,
		Green: 30,
		Blue:  255,
	}
	file, _ := os.Create("color.dat")
	encoder := nbt.NewEncoder(file)
	err := encoder.Encode(color, "") // empty is root tag
	if err != nil {
		panic(err)
	}
}

Results in (inspected using this tool):

NBT

Reading from a file
func main() {
	var color Color
	file, _ := os.Open("color.dat")
	decoder := nbt.NewDecoder(file)
	_, err := decoder.Decode(&color)
	if err != nil {
		panic(err)
	}
	fmt.Println(color)
}

Outputs:

{120 30 255}

Documentation

Index

Constants

View Source
const (
	TagEnd byte = iota
	TagByte
	TagShort
	TagInt
	TagLong
	TagFloat
	TagDouble
	TagByteArray
	TagString
	TagList
	TagCompound
	TagIntArray
	TagLongArray
	TagInvalid // Not really used in NBT, merely to catch invalid types
)

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

func Unmarshal

func Unmarshal(data []byte, v any) error

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(reader io.Reader) *Decoder

func (Decoder) Decode

func (d Decoder) Decode(v any) (string, error)

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(writer io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(value any, tagName string) error

func (*Encoder) WriteTag

func (e *Encoder) WriteTag(value reflect.Value, tagType byte, tagName string) error

type Marshaler

type Marshaler interface {
	TagType() byte
	MarshalNBT(writer io.Writer) error
}

Marshaler is implemented by structs that wish to specify how they would like to be marshalled.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalNBT(tagType byte, reader io.Reader) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL