Skip to content

Commit 539b4a3

Browse files
authored
Add 1.21.1 chat support (#284)
Merge this without verification. Otherwise can't be reviewed in the further updates.
1 parent 9a1f543 commit 539b4a3

File tree

5 files changed

+115
-15
lines changed

5 files changed

+115
-15
lines changed

chat/decoration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Type struct {
2929
}
3030

3131
func (t *Type) Decorate(content Message, d *Decoration) (msg Message) {
32-
with := make([]Message, len(d.Parameters))
32+
with := make([]any, len(d.Parameters))
3333
for i, para := range d.Parameters {
3434
switch para {
3535
case "sender":

chat/jsonmessage.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,16 @@ func (m *Message) UnmarshalJSON(raw []byte) (err error) {
5858
return errors.New("unknown chat message type: '" + string(raw[0]) + "'")
5959
}
6060
}
61+
62+
func (t *TranslateArgs) UnmarshalJSON(raw []byte) error {
63+
var v []Message
64+
err := json.Unmarshal(raw, &v)
65+
if err != nil {
66+
return err
67+
}
68+
69+
for _, v := range v {
70+
*t = append(*t, v)
71+
}
72+
return nil
73+
}

chat/message.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ type Message struct {
7070
ClickEvent *ClickEvent `json:"clickEvent,omitempty" nbt:"clickEvent,omitempty"`
7171
HoverEvent *HoverEvent `json:"hoverEvent,omitempty" nbt:"hoverEvent,omitempty"`
7272

73-
Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
74-
With []Message `json:"with,omitempty" nbt:"with,omitempty"`
75-
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
73+
Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
74+
With TranslateArgs `json:"with,omitempty" nbt:"with,omitempty"`
75+
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
7676
}
7777

78+
type TranslateArgs []any
79+
7880
// Same as Message, but "Text" is omitempty
7981
type translateMsg struct {
8082
Text string `json:"text,omitempty" nbt:"text,omitempty"`
@@ -92,9 +94,9 @@ type translateMsg struct {
9294
ClickEvent *ClickEvent `json:"clickEvent,omitempty" nbt:"clickEvent,omitempty"`
9395
HoverEvent *HoverEvent `json:"hoverEvent,omitempty" nbt:"hoverEvent,omitempty"`
9496

95-
Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
96-
With []Message `json:"with,omitempty" nbt:"with,omitempty"`
97-
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
97+
Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
98+
With TranslateArgs `json:"with,omitempty" nbt:"with,omitempty"`
99+
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
98100
}
99101

100102
type rawMsgStruct Message
@@ -127,7 +129,9 @@ func Text(str string) Message {
127129

128130
func TranslateMsg(key string, with ...Message) (m Message) {
129131
m.Translate = key
130-
m.With = with
132+
for _, v := range with {
133+
m.With = append(m.With, v)
134+
}
131135
return
132136
}
133137

@@ -195,7 +199,12 @@ func (m Message) ClearString() string {
195199
if m.Translate != "" {
196200
args := make([]any, len(m.With))
197201
for i, v := range m.With {
198-
args[i] = v.ClearString()
202+
switch v := v.(type) {
203+
case Message:
204+
args[i] = v.ClearString()
205+
default:
206+
args[i] = v
207+
}
199208
}
200209

201210
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
@@ -238,12 +247,7 @@ func (m Message) String() string {
238247

239248
// handle translate
240249
if m.Translate != "" {
241-
args := make([]any, len(m.With))
242-
for i, v := range m.With {
243-
args[i] = v
244-
}
245-
246-
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
250+
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], m.With...)
247251
}
248252

249253
if m.Extra != nil {

chat/nbtmessage.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,50 @@ func (m *Message) UnmarshalNBT(tagType byte, r nbt.DecoderReader) error {
5252
return errors.New("unknown chat message type: '" + strconv.FormatUint(uint64(tagType), 16) + "'")
5353
}
5454
}
55+
56+
func (t *TranslateArgs) UnmarshalNBT(tagType byte, r nbt.DecoderReader) error {
57+
tagReader := bytes.NewReader([]byte{tagType})
58+
decoder := nbt.NewDecoder(io.MultiReader(tagReader, r))
59+
decoder.NetworkFormat(true) // TagType directlly followed the body
60+
61+
switch tagType {
62+
case nbt.TagList:
63+
var value []Message
64+
if _, err := decoder.Decode(&value); err != nil {
65+
return err
66+
}
67+
for _, v := range value {
68+
*t = append(*t, v)
69+
}
70+
return nil
71+
case nbt.TagByteArray:
72+
var value []int8
73+
if _, err := decoder.Decode(&value); err != nil {
74+
return err
75+
}
76+
for _, v := range value {
77+
*t = append(*t, strconv.FormatInt(int64(v), 10))
78+
}
79+
return nil
80+
case nbt.TagIntArray:
81+
var value []int32
82+
if _, err := decoder.Decode(&value); err != nil {
83+
return err
84+
}
85+
for _, v := range value {
86+
*t = append(*t, strconv.FormatInt(int64(v), 10))
87+
}
88+
return nil
89+
case nbt.TagLongArray:
90+
var value []int64
91+
if _, err := decoder.Decode(&value); err != nil {
92+
return err
93+
}
94+
for _, v := range value {
95+
*t = append(*t, strconv.FormatInt(int64(v), 10))
96+
}
97+
return nil
98+
default:
99+
return errors.New("unknown translation args type: '" + strconv.FormatUint(uint64(tagType), 16) + "'")
100+
}
101+
}

chat/nbtmessage_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package chat_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Tnze/go-mc/chat"
7+
en_us "github.com/Tnze/go-mc/data/lang/en-us"
8+
"github.com/Tnze/go-mc/nbt"
9+
)
10+
11+
func TestMessage_UnmarshalJSON_string(t *testing.T) {
12+
snbts := []string{
13+
"{translate: sleep.players_sleeping, with: [I; 1, 37]}",
14+
}
15+
16+
texts := []string{
17+
"1/37 players sleeping",
18+
}
19+
20+
chat.SetLanguage(en_us.Map)
21+
for i, v := range snbts {
22+
bytes, err := nbt.Marshal(nbt.StringifiedMessage(v))
23+
if err != nil {
24+
t.Errorf("Invalid SNBT: %v", err)
25+
continue
26+
}
27+
28+
var cm chat.Message
29+
if err := nbt.Unmarshal(bytes, &cm); err != nil {
30+
t.Error(err)
31+
}
32+
if str := cm.String(); str != texts[i] {
33+
t.Errorf("gets %q, wants %q", str, texts[i])
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)