Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports unnamed NBT field #285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chat/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const (

// Message is a message sent by other
type Message struct {
Text string `json:"text" nbt:"text"`
Text string `json:"text" nbt:"text,default"`

Bold bool `json:"bold,omitempty" nbt:"bold,omitempty"` // 粗体
Italic bool `json:"italic,omitempty" nbt:"italic,omitempty"` // 斜体
Expand Down
21 changes: 21 additions & 0 deletions chat/nbtmessage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package chat

import (
"github.com/Tnze/go-mc/nbt"
"os"
"testing"
)

func TestNbtExtraText(t *testing.T) {
//SNBT: {extra: [{extra: [{color: "dark_gray",text: "> "},{: "test"}],text: ""}],text: ""}
f, _ := os.Open("testdata/chat.nbt")
d := nbt.NewDecoder(f)
var m Message
if _, err := d.Decode(&m); err != nil {
t.Fatal(err)
}

if m.ClearString() != "> test" {
t.Fatalf("gets %q, wants %q", m.ClearString(), "> test")
}
}
Binary file added chat/testdata/chat.nbt
Binary file not shown.
2 changes: 1 addition & 1 deletion nbt/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error {
// Fall back to linear search.
for i := range fields.list {
ff := &fields.list[i]
if strings.EqualFold(ff.name, tn) {
if strings.EqualFold(ff.name, tn) || ff.asDefault && tn == "" {
f = ff
break
}
Expand Down
6 changes: 5 additions & 1 deletion nbt/typeinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type field struct {
index []int
typ reflect.Type
omitEmpty bool
asDefault bool
asList bool
}

Expand Down Expand Up @@ -105,7 +106,7 @@ func typeFields(t reflect.Type) (tInfo structFields) {
}

// parse options
var omitEmpty, asList bool
var omitEmpty, asList, asDefault bool
for opts != "" {
var name string
name, opts, _ = strings.Cut(opts, ",")
Expand All @@ -114,6 +115,8 @@ func typeFields(t reflect.Type) (tInfo structFields) {
omitEmpty = true
case "list":
asList = true
case "default":
asDefault = true
}
}
// Deprecated: use `nbt:",list"` instead.
Expand All @@ -133,6 +136,7 @@ func typeFields(t reflect.Type) (tInfo structFields) {
index: index,
typ: ft,
omitEmpty: omitEmpty,
asDefault: asDefault,
asList: asList,
}

Expand Down
Loading