From bd1350ae7f294f0f78861efa5d6ab7b2edb85504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=9F=E8=92=BB?= Date: Sun, 25 Aug 2024 22:30:32 +0800 Subject: [PATCH 1/2] nbt: "default" for unnamed field tag --- nbt/decode.go | 2 +- nbt/typeinfo.go | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/nbt/decode.go b/nbt/decode.go index e5589ddc8..4ddefe255 100644 --- a/nbt/decode.go +++ b/nbt/decode.go @@ -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 } diff --git a/nbt/typeinfo.go b/nbt/typeinfo.go index 5cc963838..bac6293ec 100644 --- a/nbt/typeinfo.go +++ b/nbt/typeinfo.go @@ -19,6 +19,7 @@ type field struct { index []int typ reflect.Type omitEmpty bool + asDefault bool asList bool } @@ -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, ",") @@ -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. @@ -133,6 +136,7 @@ func typeFields(t reflect.Type) (tInfo structFields) { index: index, typ: ft, omitEmpty: omitEmpty, + asDefault: asDefault, asList: asList, } From e614a236f134f3b4a3466891277fe3b93675bf6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=9F=E8=92=BB?= Date: Sun, 25 Aug 2024 22:31:39 +0800 Subject: [PATCH 2/2] chat: text is default field --- chat/message.go | 2 +- chat/nbtmessage_test.go | 21 +++++++++++++++++++++ chat/testdata/chat.nbt | Bin 0 -> 90 bytes 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 chat/nbtmessage_test.go create mode 100644 chat/testdata/chat.nbt diff --git a/chat/message.go b/chat/message.go index 85f0253fd..183a0d7f1 100644 --- a/chat/message.go +++ b/chat/message.go @@ -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"` // 斜体 diff --git a/chat/nbtmessage_test.go b/chat/nbtmessage_test.go new file mode 100644 index 000000000..d90b2774a --- /dev/null +++ b/chat/nbtmessage_test.go @@ -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") + } +} diff --git a/chat/testdata/chat.nbt b/chat/testdata/chat.nbt new file mode 100644 index 0000000000000000000000000000000000000000..19f100d4bf9f1e630dda8678d90405673c311f1a GIT binary patch literal 90 zcmd;LVBln6O|2*?O5|c-U|>Y%FmW)jCg