nbt

package
v1.16.2-0...-18405f0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2021 License: MIT Imports: 10 Imported by: 0

README

NBT

This package implement the Named Binary Tag format of Minecraft. It supports all formats of NBT including compact arrays for longs.

Usage

For the following NBT tag:

TAG_Compound("hello world") {
    TAG_String('name'): 'Bananrama'
}   

To read and write would look like:

package main

import "bytes"
import "github.com/ForgottenWorld/go-mc/nbt"

type Compound struct {
    Name string `nbt:"name"` // Note that if name is private (name), the field will not be used
}

func main() {
    var out bytes.Buffer
    banana := Compound{Name: "Bananrama"}
    _ = nbt.Marshal(&out, banana)

    var rama Compound
    _ = nbt.Unmarshal(out.Bytes(), &rama)
}

Struct field tags

There are two tags supported:

  • nbt
  • nbt_type

The nbt tag is used to change the name of the NBT Tag field, whereas the nbt_type tag is used to enforce a certain NBT Tag type when it is ambiguous.

For example:

type Compound struct {
    LongArray []int64
    LongList []int64 `nbt_type:"list"` // forces a long list instead of a long array
}

Docs

GoDoc

Documentation

Overview

Package nbt implement the Named Binary Tag format of Minecraft. It provides api like encoding/xml package.

Index

Examples

Constants

View Source
const (
	TagEnd byte = iota
	TagByte
	TagShort
	TagInt
	TagLong
	TagFloat
	TagDouble
	TagByteArray
	TagString
	TagList
	TagCompound
	TagIntArray
	TagLongArray
	TagNone = 0xFF
)

Tag type IDs

Variables

View Source
var ErrEND = errors.New("unexpected TAG_End")

ErrEND error will be returned when reading a NBT with only Tag_End

View Source
var (
	ErrMustBeStruct = errors.New("a compound can only be a struct")
)

Functions

func IsArrayTag

func IsArrayTag(ty byte) bool

func Marshal

func Marshal(w io.Writer, v interface{}) error
Example
var value = struct {
	Name string `nbt:"name"`
}{"Tnze"}

var buf bytes.Buffer
if err := Marshal(&buf, value); err != nil {
	panic(err)
}

fmt.Printf("% 02x ", buf.Bytes())
Output:

	0a 00 00 08 00 04 6e 61 6d 65 00 04 54 6e 7a 65 00

func MarshalCompound

func MarshalCompound(w io.Writer, v interface{}, rootTagName string) error

func Unmarshal

func Unmarshal(data []byte, v interface{}) error
Example
var data = []byte{
	0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x09,
	0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
}

var Name string

if err := Unmarshal(data, &Name); err != nil {
	panic(err)
}

fmt.Println(Name)
Output:

Bananrama

Types

type Decoder

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

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

type DecoderReader

type DecoderReader = interface {
	io.ByteScanner
	io.Reader
}

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

type Unmarshaler

type Unmarshaler interface {
	Unmarshal(tagType byte, tagName string, r DecoderReader) error
}

Jump to

Keyboard shortcuts

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