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

Add 1.21.1 chat support #284

Open
wants to merge 1 commit 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/decoration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Type struct {
}

func (t *Type) Decorate(content Message, d *Decoration) (msg Message) {
with := make([]Message, len(d.Parameters))
with := make([]any, len(d.Parameters))
for i, para := range d.Parameters {
switch para {
case "sender":
Expand Down
13 changes: 13 additions & 0 deletions chat/jsonmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ func (m *Message) UnmarshalJSON(raw []byte) (err error) {
return errors.New("unknown chat message type: '" + string(raw[0]) + "'")
}
}

func (t *TranslateArgs) UnmarshalJSON(raw []byte) error {
var v []Message
err := json.Unmarshal(raw, &v)
if err != nil {
return err
}

for _, v := range v {
*t = append(*t, v)
}
return nil
}
32 changes: 18 additions & 14 deletions chat/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ type Message struct {
ClickEvent *ClickEvent `json:"clickEvent,omitempty" nbt:"clickEvent,omitempty"`
HoverEvent *HoverEvent `json:"hoverEvent,omitempty" nbt:"hoverEvent,omitempty"`

Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
With []Message `json:"with,omitempty" nbt:"with,omitempty"`
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
With TranslateArgs `json:"with,omitempty" nbt:"with,omitempty"`
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
}

type TranslateArgs []any

// Same as Message, but "Text" is omitempty
type translateMsg struct {
Text string `json:"text,omitempty" nbt:"text,omitempty"`
Expand All @@ -92,9 +94,9 @@ type translateMsg struct {
ClickEvent *ClickEvent `json:"clickEvent,omitempty" nbt:"clickEvent,omitempty"`
HoverEvent *HoverEvent `json:"hoverEvent,omitempty" nbt:"hoverEvent,omitempty"`

Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
With []Message `json:"with,omitempty" nbt:"with,omitempty"`
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
Translate string `json:"translate,omitempty" nbt:"translate,omitempty"`
With TranslateArgs `json:"with,omitempty" nbt:"with,omitempty"`
Extra []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
}

type rawMsgStruct Message
Expand Down Expand Up @@ -127,7 +129,9 @@ func Text(str string) Message {

func TranslateMsg(key string, with ...Message) (m Message) {
m.Translate = key
m.With = with
for _, v := range with {
m.With = append(m.With, v)
}
return
}

Expand Down Expand Up @@ -195,7 +199,12 @@ func (m Message) ClearString() string {
if m.Translate != "" {
args := make([]any, len(m.With))
for i, v := range m.With {
args[i] = v.ClearString()
switch v := v.(type) {
case Message:
args[i] = v.ClearString()
default:
args[i] = v
}
}

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

// handle translate
if m.Translate != "" {
args := make([]any, len(m.With))
for i, v := range m.With {
args[i] = v
}

_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], m.With...)
}

if m.Extra != nil {
Expand Down
47 changes: 47 additions & 0 deletions chat/nbtmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,50 @@ func (m *Message) UnmarshalNBT(tagType byte, r nbt.DecoderReader) error {
return errors.New("unknown chat message type: '" + strconv.FormatUint(uint64(tagType), 16) + "'")
}
}

func (t *TranslateArgs) UnmarshalNBT(tagType byte, r nbt.DecoderReader) error {
tagReader := bytes.NewReader([]byte{tagType})
decoder := nbt.NewDecoder(io.MultiReader(tagReader, r))
decoder.NetworkFormat(true) // TagType directlly followed the body

switch tagType {
case nbt.TagList:
var value []Message
if _, err := decoder.Decode(&value); err != nil {
return err
}
for _, v := range value {
*t = append(*t, v)
}
return nil
case nbt.TagByteArray:
var value []int8
if _, err := decoder.Decode(&value); err != nil {
return err
}
for _, v := range value {
*t = append(*t, strconv.FormatInt(int64(v), 10))
}
return nil
case nbt.TagIntArray:
var value []int32
if _, err := decoder.Decode(&value); err != nil {
return err
}
for _, v := range value {
*t = append(*t, strconv.FormatInt(int64(v), 10))
}
return nil
case nbt.TagLongArray:
var value []int64
if _, err := decoder.Decode(&value); err != nil {
return err
}
for _, v := range value {
*t = append(*t, strconv.FormatInt(int64(v), 10))
}
return nil
default:
return errors.New("unknown translation args type: '" + strconv.FormatUint(uint64(tagType), 16) + "'")
}
}
36 changes: 36 additions & 0 deletions chat/nbtmessage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package chat_test

import (
"testing"

"github.com/Tnze/go-mc/chat"
en_us "github.com/Tnze/go-mc/data/lang/en-us"
"github.com/Tnze/go-mc/nbt"
)

func TestMessage_UnmarshalJSON_string(t *testing.T) {
snbts := []string{
"{translate: sleep.players_sleeping, with: [I; 1, 37]}",
}

texts := []string{
"1/37 players sleeping",
}

chat.SetLanguage(en_us.Map)
for i, v := range snbts {
bytes, err := nbt.Marshal(nbt.StringifiedMessage(v))
if err != nil {
t.Errorf("Invalid SNBT: %v", err)
continue
}

var cm chat.Message
if err := nbt.Unmarshal(bytes, &cm); err != nil {
t.Error(err)
}
if str := cm.String(); str != texts[i] {
t.Errorf("gets %q, wants %q", str, texts[i])
}
}
}
Loading