Apply "network nbt" format for net/packet.

examples and tests are updated
This commit is contained in:
Tnze
2023-11-19 02:01:17 +08:00
parent 7d6e65f554
commit 61916db07a
4 changed files with 24 additions and 22 deletions

View File

@ -47,9 +47,14 @@ func (e *Encoder) NetworkFormat(enable bool) {
// expect `[]int8`, `[]int32`, `[]int64`, `[]uint8`, `[]uint32` and `[]uint64`,
// which TagByteArray, TagIntArray and TagLongArray.
// To force encode them as TagList, add a struct field tag.
func (e *Encoder) Encode(v any, tagName string) error {
func (e *Encoder) Encode(v any, tagName string) (err error) {
t, val := getTagType(reflect.ValueOf(v))
if err := writeTag(e.w, t, tagName); err != nil {
if e.networkFormat {
_, err = e.w.Write([]byte{t})
} else {
err = writeTag(e.w, t, tagName)
}
if err != nil {
return err
}
return e.marshal(val, t)