nbt

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 14 Imported by: 1

README

Go NBT

The library for Minecraft NBT

Getting Started

Use go get to install the library

go get github.com/Aton-Kish/gonbt

Import in your application

import (
	nbt "github.com/Aton-Kish/gonbt"
)

Usage

package main

import (
	"compress/gzip"
	"fmt"
	"log"
	"os"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	// Load NBT Data
	f, err := os.Open("level.dat")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	zr, err := gzip.NewReader(f)
	if err != nil {
		log.Fatal(err)
	}
	defer zr.Close()

	// Decode
	dat, err := nbt.Decode(zr)
	if err != nil {
		log.Fatal(err)
	}

	// "Reversible" Stringify
	snbt := nbt.Stringify(dat)
	// snbt := nbt.CompactStringify(dat)
	// snbt := nbt.PrettyStringify(dat, "  ")
	fmt.Println(snbt)

	// Parse
	dat2, err := nbt.Parse(snbt)
	if err != nil {
		log.Fatal(err)
	}

	// "Irreversible" Stringify
	json := nbt.Json(dat2)
	// json := nbt.CompactJson(dat2)
	// json := nbt.PrettyJson(dat2, "  ")
	fmt.Println(json)
}

License

This library is licensed under the MIT License, see LICENSE.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidTagType    = errors.New("invalid tag type")
	ErrInvalidSnbtFormat = errors.New("invalid snbt format")
	ErrDecode            = errors.New("failed to decode")
)

Functions

func CompactJson

func CompactJson(tag Tag) string
Example
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.CompactJson(dat))
}
Output:

{"Data":{"GameType":0,"LevelName":"Go NBT","Version":{"Id":3120,"Name":"1.19.2","Series":"main","Snapshot":0}}}

func CompactStringify

func CompactStringify(tag Tag) string
Example
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.CompactStringify(dat))
}
Output:

{Data:{GameType:0,LevelName:"Go NBT",Version:{Id:3120,Name:"1.19.2",Series:"main",Snapshot:0b}}}

func Encode

func Encode(w io.Writer, tag Tag) error
Example
package main

import (
	"bytes"
	"log"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	buf := new(bytes.Buffer)
	if err := nbt.Encode(buf, dat); err != nil {
		log.Fatal(err)
	}

	// f, err := os.Create("level.dat")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// defer f.Close()

	// zw := gzip.NewWriter(f)
	// defer zw.Close()

	// if _, err := zw.Write(buf.Bytes()); err != nil {
	// 	log.Fatal(err)
	// }
}
Output:

func Json

func Json(tag Tag) string
Example
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.Json(dat))
}
Output:

{"Data": {"GameType": 0, "LevelName": "Go NBT", "Version": {"Id": 3120, "Name": "1.19.2", "Series": "main", "Snapshot": 0}}}

func PrettyJson

func PrettyJson(tag Tag, indent string) string
Example (Space2)
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.PrettyJson(dat, "  "))
}
Output:

{
  "Data": {
    "GameType": 0,
    "LevelName": "Go NBT",
    "Version": {
      "Id": 3120,
      "Name": "1.19.2",
      "Series": "main",
      "Snapshot": 0
    }
  }
}
Example (Space4)
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.PrettyJson(dat, "    "))
}
Output:

{
    "Data": {
        "GameType": 0,
        "LevelName": "Go NBT",
        "Version": {
            "Id": 3120,
            "Name": "1.19.2",
            "Series": "main",
            "Snapshot": 0
        }
    }
}
Example (Tab)
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.PrettyJson(dat, "\t"))
}
Output:

{
	"Data": {
		"GameType": 0,
		"LevelName": "Go NBT",
		"Version": {
			"Id": 3120,
			"Name": "1.19.2",
			"Series": "main",
			"Snapshot": 0
		}
	}
}

func PrettyStringify

func PrettyStringify(tag Tag, indent string) string
Example (Space2)
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.PrettyStringify(dat, "  "))
}
Output:

{
  Data: {
    GameType: 0,
    LevelName: "Go NBT",
    Version: {
      Id: 3120,
      Name: "1.19.2",
      Series: "main",
      Snapshot: 0b
    }
  }
}
Example (Space4)
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.PrettyStringify(dat, "    "))
}
Output:

{
    Data: {
        GameType: 0,
        LevelName: "Go NBT",
        Version: {
            Id: 3120,
            Name: "1.19.2",
            Series: "main",
            Snapshot: 0b
        }
    }
}
Example (Tab)
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.PrettyStringify(dat, "\t"))
}
Output:

{
	Data: {
		GameType: 0,
		LevelName: "Go NBT",
		Version: {
			Id: 3120,
			Name: "1.19.2",
			Series: "main",
			Snapshot: 0b
		}
	}
}

func SetLogger added in v0.2.0

func SetLogger(l liblog.Logger)

func Stringify

func Stringify(tag Tag) string
Example
package main

import (
	"fmt"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	dat := nbt.NewCompoundTag(nbt.NewTagName("Data"), nbt.NewCompoundPayload(
		nbt.NewIntTag(nbt.NewTagName("GameType"), nbt.NewIntPayload(0)),
		nbt.NewStringTag(nbt.NewTagName("LevelName"), nbt.NewStringPayload("Go NBT")),
		nbt.NewCompoundTag(nbt.NewTagName("Version"), nbt.NewCompoundPayload(
			nbt.NewIntTag(nbt.NewTagName("Id"), nbt.NewIntPayload(3120)),
			nbt.NewStringTag(nbt.NewTagName("Name"), nbt.NewStringPayload("1.19.2")),
			nbt.NewStringTag(nbt.NewTagName("Series"), nbt.NewStringPayload("main")),
			nbt.NewByteTag(nbt.NewTagName("Snapshot"), nbt.NewBytePayload(0)),
			nbt.NewEndTag(),
		)),
		nbt.NewEndTag(),
	))

	fmt.Println(nbt.Stringify(dat))
}
Output:

{Data: {GameType: 0, LevelName: "Go NBT", Version: {Id: 3120, Name: "1.19.2", Series: "main", Snapshot: 0b}}}

Types

type ByteArrayPayload

type ByteArrayPayload []int8

func NewByteArrayPayload added in v0.1.0

func NewByteArrayPayload(values ...int8) *ByteArrayPayload

func (ByteArrayPayload) String added in v0.2.0

func (p ByteArrayPayload) String() string

func (*ByteArrayPayload) TypeId

func (p *ByteArrayPayload) TypeId() TagType

type ByteArrayTag

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

func NewByteArrayTag added in v0.1.0

func NewByteArrayTag(tagName *TagName, payload *ByteArrayPayload) *ByteArrayTag

func (*ByteArrayTag) Payload added in v0.1.0

func (t *ByteArrayTag) Payload() Payload

func (ByteArrayTag) String added in v0.2.0

func (t ByteArrayTag) String() string

func (*ByteArrayTag) TagName added in v0.1.0

func (t *ByteArrayTag) TagName() *TagName

func (*ByteArrayTag) TypeId

func (t *ByteArrayTag) TypeId() TagType

type BytePayload

type BytePayload int8

func NewBytePayload added in v0.1.0

func NewBytePayload(value int8) *BytePayload

func (BytePayload) String added in v0.2.0

func (p BytePayload) String() string

func (*BytePayload) TypeId

func (p *BytePayload) TypeId() TagType

type ByteTag

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

func NewByteTag added in v0.1.0

func NewByteTag(tagName *TagName, payload *BytePayload) *ByteTag

func (*ByteTag) Payload added in v0.1.0

func (t *ByteTag) Payload() Payload

func (ByteTag) String added in v0.2.0

func (t ByteTag) String() string

func (*ByteTag) TagName added in v0.1.0

func (t *ByteTag) TagName() *TagName

func (*ByteTag) TypeId

func (t *ByteTag) TypeId() TagType

type CompoundPayload

type CompoundPayload []Tag

func NewCompoundPayload added in v0.1.0

func NewCompoundPayload(values ...Tag) *CompoundPayload

func (CompoundPayload) String added in v0.2.0

func (p CompoundPayload) String() string

func (*CompoundPayload) TypeId

func (p *CompoundPayload) TypeId() TagType

type CompoundTag

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

func NewCompoundTag added in v0.1.0

func NewCompoundTag(tagName *TagName, payload *CompoundPayload) *CompoundTag

func (*CompoundTag) Payload added in v0.1.0

func (t *CompoundTag) Payload() Payload

func (CompoundTag) String added in v0.2.0

func (t CompoundTag) String() string

func (*CompoundTag) TagName added in v0.1.0

func (t *CompoundTag) TagName() *TagName

func (*CompoundTag) TypeId

func (t *CompoundTag) TypeId() TagType

type DoublePayload

type DoublePayload float64

func NewDoublePayload added in v0.1.0

func NewDoublePayload(value float64) *DoublePayload

func (DoublePayload) String added in v0.2.0

func (p DoublePayload) String() string

func (*DoublePayload) TypeId

func (p *DoublePayload) TypeId() TagType

type DoubleTag

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

func NewDoubleTag added in v0.1.0

func NewDoubleTag(tagName *TagName, payload *DoublePayload) *DoubleTag

func (*DoubleTag) Payload added in v0.1.0

func (t *DoubleTag) Payload() Payload

func (DoubleTag) String added in v0.2.0

func (t DoubleTag) String() string

func (*DoubleTag) TagName added in v0.1.0

func (t *DoubleTag) TagName() *TagName

func (*DoubleTag) TypeId

func (t *DoubleTag) TypeId() TagType

type EndTag

type EndTag struct {
}

func NewEndTag added in v0.1.0

func NewEndTag() *EndTag

func (*EndTag) Payload added in v0.1.0

func (t *EndTag) Payload() Payload

func (EndTag) String added in v0.2.0

func (t EndTag) String() string

func (*EndTag) TagName added in v0.1.0

func (t *EndTag) TagName() *TagName

func (*EndTag) TypeId

func (t *EndTag) TypeId() TagType

type FloatPayload

type FloatPayload float32

func NewFloatPayload added in v0.1.0

func NewFloatPayload(value float32) *FloatPayload

func (FloatPayload) String added in v0.2.0

func (p FloatPayload) String() string

func (*FloatPayload) TypeId

func (p *FloatPayload) TypeId() TagType

type FloatTag

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

func NewFloatTag added in v0.1.0

func NewFloatTag(tagName *TagName, payload *FloatPayload) *FloatTag

func (*FloatTag) Payload added in v0.1.0

func (t *FloatTag) Payload() Payload

func (FloatTag) String added in v0.2.0

func (t FloatTag) String() string

func (*FloatTag) TagName added in v0.1.0

func (t *FloatTag) TagName() *TagName

func (*FloatTag) TypeId

func (t *FloatTag) TypeId() TagType

type IntArrayPayload

type IntArrayPayload []int32

func NewIntArrayPayload added in v0.1.0

func NewIntArrayPayload(values ...int32) *IntArrayPayload

func (IntArrayPayload) String added in v0.2.0

func (p IntArrayPayload) String() string

func (*IntArrayPayload) TypeId

func (p *IntArrayPayload) TypeId() TagType

type IntArrayTag

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

func NewIntArrayTag added in v0.1.0

func NewIntArrayTag(tagName *TagName, payload *IntArrayPayload) *IntArrayTag

func (*IntArrayTag) Payload added in v0.1.0

func (t *IntArrayTag) Payload() Payload

func (IntArrayTag) String added in v0.2.0

func (t IntArrayTag) String() string

func (*IntArrayTag) TagName added in v0.1.0

func (t *IntArrayTag) TagName() *TagName

func (*IntArrayTag) TypeId

func (t *IntArrayTag) TypeId() TagType

type IntPayload

type IntPayload int32

func NewIntPayload added in v0.1.0

func NewIntPayload(value int32) *IntPayload

func (IntPayload) String added in v0.2.0

func (p IntPayload) String() string

func (*IntPayload) TypeId

func (p *IntPayload) TypeId() TagType

type IntTag

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

func NewIntTag added in v0.1.0

func NewIntTag(tagName *TagName, payload *IntPayload) *IntTag

func (*IntTag) Payload added in v0.1.0

func (t *IntTag) Payload() Payload

func (IntTag) String added in v0.2.0

func (t IntTag) String() string

func (*IntTag) TagName added in v0.1.0

func (t *IntTag) TagName() *TagName

func (*IntTag) TypeId

func (t *IntTag) TypeId() TagType

type ListPayload

type ListPayload []Payload

func NewListPayload added in v0.1.0

func NewListPayload(values ...Payload) *ListPayload

func (ListPayload) String added in v0.2.0

func (p ListPayload) String() string

func (*ListPayload) TypeId

func (p *ListPayload) TypeId() TagType

type ListTag

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

func NewListTag added in v0.1.0

func NewListTag(tagName *TagName, payload *ListPayload) *ListTag

func (*ListTag) Payload added in v0.1.0

func (t *ListTag) Payload() Payload

func (ListTag) String added in v0.2.0

func (t ListTag) String() string

func (*ListTag) TagName added in v0.1.0

func (t *ListTag) TagName() *TagName

func (*ListTag) TypeId

func (t *ListTag) TypeId() TagType

type LongArrayPayload

type LongArrayPayload []int64

func NewLongArrayPayload added in v0.1.0

func NewLongArrayPayload(values ...int64) *LongArrayPayload

func (LongArrayPayload) String added in v0.2.0

func (p LongArrayPayload) String() string

func (*LongArrayPayload) TypeId

func (p *LongArrayPayload) TypeId() TagType

type LongArrayTag

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

func NewLongArrayTag added in v0.1.0

func NewLongArrayTag(tagName *TagName, payload *LongArrayPayload) *LongArrayTag

func (*LongArrayTag) Payload added in v0.1.0

func (t *LongArrayTag) Payload() Payload

func (LongArrayTag) String added in v0.2.0

func (t LongArrayTag) String() string

func (*LongArrayTag) TagName added in v0.1.0

func (t *LongArrayTag) TagName() *TagName

func (*LongArrayTag) TypeId

func (t *LongArrayTag) TypeId() TagType

type LongPayload

type LongPayload int64

func NewLongPayload added in v0.1.0

func NewLongPayload(value int64) *LongPayload

func (LongPayload) String added in v0.2.0

func (p LongPayload) String() string

func (*LongPayload) TypeId

func (p *LongPayload) TypeId() TagType

type LongTag

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

func NewLongTag added in v0.1.0

func NewLongTag(tagName *TagName, payload *LongPayload) *LongTag

func (*LongTag) Payload added in v0.1.0

func (t *LongTag) Payload() Payload

func (LongTag) String added in v0.2.0

func (t LongTag) String() string

func (*LongTag) TagName added in v0.1.0

func (t *LongTag) TagName() *TagName

func (*LongTag) TypeId

func (t *LongTag) TypeId() TagType

type NbtError added in v0.2.0

type NbtError struct {
	Op  string
	Err error
}

func (*NbtError) Error added in v0.2.0

func (e *NbtError) Error() string

func (*NbtError) Unwrap added in v0.2.0

func (e *NbtError) Unwrap() error

type Payload

type Payload interface {
	String() string
	TypeId() TagType
	// contains filtered or unexported methods
}

func NewPayload

func NewPayload(typ TagType) (Payload, error)

type ShortPayload

type ShortPayload int16

func NewShortPayload added in v0.1.0

func NewShortPayload(value int16) *ShortPayload

func (ShortPayload) String added in v0.2.0

func (p ShortPayload) String() string

func (*ShortPayload) TypeId

func (p *ShortPayload) TypeId() TagType

type ShortTag

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

func NewShortTag added in v0.1.0

func NewShortTag(tagName *TagName, payload *ShortPayload) *ShortTag

func (*ShortTag) Payload added in v0.1.0

func (t *ShortTag) Payload() Payload

func (ShortTag) String added in v0.2.0

func (t ShortTag) String() string

func (*ShortTag) TagName added in v0.1.0

func (t *ShortTag) TagName() *TagName

func (*ShortTag) TypeId

func (t *ShortTag) TypeId() TagType

type StringPayload

type StringPayload string

func NewStringPayload added in v0.1.0

func NewStringPayload(value string) *StringPayload

func (StringPayload) String added in v0.2.0

func (p StringPayload) String() string

func (*StringPayload) TypeId

func (p *StringPayload) TypeId() TagType

type StringTag

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

func NewStringTag added in v0.1.0

func NewStringTag(tagName *TagName, payload *StringPayload) *StringTag

func (*StringTag) Payload added in v0.1.0

func (t *StringTag) Payload() Payload

func (StringTag) String added in v0.2.0

func (t StringTag) String() string

func (*StringTag) TagName added in v0.1.0

func (t *StringTag) TagName() *TagName

func (*StringTag) TypeId

func (t *StringTag) TypeId() TagType

type Tag

type Tag interface {
	String() string
	TypeId() TagType
	TagName() *TagName
	Payload() Payload
	// contains filtered or unexported methods
}

func Decode

func Decode(r io.Reader) (Tag, error)
Example
package main

import (
	"bytes"
	"fmt"
	"log"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	// f, err := os.Open("level.dat")
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// defer f.Close()

	// zr, err := gzip.NewReader(f)
	// if err != nil {
	// 	log.Fatal(err)
	// }
	// defer zr.Close()

	// fake NBT
	zr := bytes.NewBuffer([]byte{
		// CompoundTag(Data):
		0x0A,
		0x00, 0x04,
		0x44, 0x61, 0x74, 0x61,
		//   - IntTag(GameType): 0
		0x03,
		0x00, 0x08,
		0x47, 0x61, 0x6D, 0x65, 0x54, 0x79, 0x70, 0x65,
		0x00, 0x00, 0x00, 0x00,
		//   - StringTag(LevelName): "Go NBT"
		0x08,
		0x00, 0x09,
		0x4C, 0x65, 0x76, 0x65, 0x6C, 0x4E, 0x61, 0x6D, 0x65,
		0x00, 0x06,
		0x47, 0x6F, 0x20, 0x4E, 0x42, 0x54,
		//   - CompoundTag(Version):
		0x0A,
		0x00, 0x07,
		0x56, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E,
		//     - IntTag(Id): 3120
		0x03,
		0x00, 0x02,
		0x49, 0x64,
		0x00, 0x00, 0x0C, 0x30,
		//     - StringTag(Name): "1.19.2"
		0x08,
		0x00, 0x04,
		0x4E, 0x61, 0x6D, 0x65,
		0x00, 0x06,
		0x31, 0x2E, 0x31, 0x39, 0x2E, 0x32,
		//     - StringTag(Series): "main"
		0x08,
		0x00, 0x06,
		0x53, 0x65, 0x72, 0x69, 0x65, 0x73,
		0x00, 0x04,
		0x6D, 0x61, 0x69, 0x6E,
		//     - ByteTag(Snapshot): 0b
		0x01,
		0x00, 0x08,
		0x53, 0x6E, 0x61, 0x70, 0x73, 0x68, 0x6F, 0x74,
		0x00,
		//     - EndTag
		0x00,
		//   - EndTag
		0x00,
	})

	dat, err := nbt.Decode(zr)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(nbt.PrettyStringify(dat, "  "))
}
Output:

{
  Data: {
    GameType: 0,
    LevelName: "Go NBT",
    Version: {
      Id: 3120,
      Name: "1.19.2",
      Series: "main",
      Snapshot: 0b
    }
  }
}

func NewTag

func NewTag(typ TagType) (Tag, error)

func Parse

func Parse(stringified string) (Tag, error)
Example
package main

import (
	"fmt"
	"log"

	nbt "github.com/Aton-Kish/gonbt"
)

func main() {
	str := `{Data: {GameType: 0, LevelName: "Go NBT", Version: {Id: 3120, Name: "1.19.2", Series: "main", Snapshot: 0b}}}`

	dat, err := nbt.Parse(str)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(nbt.Stringify(dat))
}
Output:

{Data: {GameType: 0, LevelName: "Go NBT", Version: {Id: 3120, Name: "1.19.2", Series: "main", Snapshot: 0b}}}

type TagName

type TagName string

func NewTagName added in v0.1.0

func NewTagName(value string) *TagName

func (TagName) String added in v0.2.0

func (n TagName) String() string

type TagType

type TagType byte
const (
	TagTypeEnd TagType = iota
	TagTypeByte
	TagTypeShort
	TagTypeInt
	TagTypeLong
	TagTypeFloat
	TagTypeDouble
	TagTypeByteArray
	TagTypeString
	TagTypeList
	TagTypeCompound
	TagTypeIntArray
	TagTypeLongArray
)

func (TagType) String added in v0.2.0

func (t TagType) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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