From 473669cbcef6dde138bb62ce7a6a024422d62daa Mon Sep 17 00:00:00 2001 From: Mark Asp Date: Mon, 21 Sep 2020 20:58:31 -0500 Subject: [PATCH] rename tag for nbt_type to list instead of noarray and add documentation --- nbt/README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- nbt/marshal.go | 4 ++-- nbt/nbt_test.go | 2 +- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/nbt/README.md b/nbt/README.md index b28c217..cf42c37 100644 --- a/nbt/README.md +++ b/nbt/README.md @@ -1,4 +1,53 @@ # NBT -This package implement the Named Binary Tag format of Minecraft. +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: + +```go +package main + +import "bytes" +import "github.com/Tnze/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: +```go +type Compound struct { + LongArray []int64 + LongList []int64 `nbt_type:"list"` // forces a long list instead of a long array +} +``` + # Docs [![GoDoc](https://godoc.org/github.com/Tnze/go-mc/nbt?status.svg)](https://godoc.org/github.com/Tnze/go-mc/nbt) \ No newline at end of file diff --git a/nbt/marshal.go b/nbt/marshal.go index 1fcb384..34f0c6a 100644 --- a/nbt/marshal.go +++ b/nbt/marshal.go @@ -186,11 +186,11 @@ func parseTag(f reflect.StructField, tagName string) tagProps { nbtType := f.Tag.Get("nbt_type") result.Type = getTagType(f.Type) - if strings.Contains(nbtType, "noarray") { + if strings.Contains(nbtType, "list") { if IsArrayTag(result.Type) { result.Type = TagList // for expanding the array to a standard list } else { - panic("noarray is only supported for array types (byte, int, long)") + panic("list is only supported for array types (byte, int, long)") } } diff --git a/nbt/nbt_test.go b/nbt/nbt_test.go index 5cc4f7e..bbc2e34 100644 --- a/nbt/nbt_test.go +++ b/nbt/nbt_test.go @@ -145,7 +145,7 @@ type BigTestStruct struct { Value float32 `nbt:"value"` } `nbt:"egg"` } `nbt:"nested compound test"` - ListTest []int64 `nbt:"listTest (long)" nbt_type:"noarray"` + ListTest []int64 `nbt:"listTest (long)" nbt_type:"list"` ListTest2 [2]struct { Name string `nbt:"name"` CreatedOn int64 `nbt:"created-on"`