Apply the waring in nbt/README.md

This commit is contained in:
Tnze
2021-06-22 11:18:18 +08:00
parent 4e6349dfda
commit 4f9779df88
4 changed files with 74 additions and 49 deletions

View File

@ -1,11 +1,11 @@
# NBT [![Go Reference](https://pkg.go.dev/badge/github.com/Tnze/go-mc/nbt.svg)](https://pkg.go.dev/github.com/Tnze/go-mc/nbt)
This package implement the [Named Binary Tag](https://wiki.vg/NBT) format of Minecraft.
The API is very similar to the standard library `encoding/json`. If you (high probability) have used that, it is easy to use this.
This package implement the [Named Binary Tag](https://wiki.vg/NBT) format of Minecraft.
# Basic Usage
> I don't know why `Marshal` looks like that, and **I will change it** to `func Marshal(v interface{}) ([]byte, error)`.
> **Use `Encoder` is recommended now.**
The API is very similar to the standard library `encoding/json`. If you (high probability) have used that, it is easy to
use this.
## Basic Usage
For the following NBT tag:
@ -20,7 +20,6 @@ To read and write would look like:
```go
package main
import "bytes"
import "github.com/Tnze/go-mc/nbt"
type Compound struct {
@ -28,27 +27,26 @@ type Compound struct {
}
func main() {
var out bytes.Buffer
banana := Compound{Name: "Bananrama"}
_ = nbt.Marshal(&out, banana)
data, _ := nbt.Marshal(banana)
var rama Compound
_ = nbt.Unmarshal(out.Bytes(), &rama)
_ = nbt.Unmarshal(data, &rama)
}
```
# Struct field tags
## 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.
tag is used to enforce a certain NBT Tag type when it is ambiguous.
For example:
```go
type Compound struct {
LongArray []int64
@ -56,5 +54,25 @@ type Compound struct {
}
```
## Stringified NBT
You can transform SNBT string into binary NBT by using go-mc, which could be useful when parsing command, but the
reverse conversion is not supported at the moment.
```go
package main
import (
"bytes"
"github.com/Tnze/go-mc/nbt"
)
func main() {
var buf bytes.Buffer
e := nbt.NewEncoder(&buf)
err := e.WriteSNBT(`{Tnze: 1, 'sp ace': [I;1,2,3]}`)
if err != nil {
panic(err)
}
}
```