update 1.19.2 bot, chat support

This commit is contained in:
Tnze
2022-11-26 15:58:31 +08:00
parent 6a3589ad61
commit 7814e7b1ab
20 changed files with 532 additions and 136 deletions

51
chat/decoration.go Normal file
View File

@ -0,0 +1,51 @@
package chat
type Decoration struct {
TranslationKey string `nbt:"translation_key"`
Parameters []string `nbt:"parameters"`
Style struct {
Bold bool `nbt:"bold"`
Italic bool `nbt:"italic"`
UnderLined bool `nbt:"underlined"`
StrikeThrough bool `nbt:"strikethrough"`
Obfuscated bool `nbt:"obfuscated"`
Color string `nbt:"color"`
Insertion string `nbt:"insertion"`
Font string `nbt:"font"`
} `nbt:"style"`
}
type Type struct {
ID int32
SenderName Message
TargetName *Message
}
func (t *Type) Decorate(content Message, d *Decoration) (msg Message) {
with := make([]Message, len(d.Parameters))
for i, para := range d.Parameters {
switch para {
case "sender":
with[i] = t.SenderName
case "target":
with[i] = *t.TargetName
case "content":
with[i] = content
default:
with[i] = Text("<nil>")
}
}
return Message{
Translate: d.TranslationKey,
With: with,
Bold: d.Style.Bold,
Italic: d.Style.Italic,
UnderLined: d.Style.UnderLined,
StrikeThrough: d.Style.StrikeThrough,
Obfuscated: d.Style.Obfuscated,
Font: d.Style.Font,
Color: d.Style.Color,
Insertion: d.Style.Insertion,
}
}

View File

@ -22,10 +22,8 @@ import (
pk "github.com/Tnze/go-mc/net/packet"
)
type Type int32
const (
Chat Type = iota
Chat = iota
System
GameInfo
SayCommand
@ -76,9 +74,9 @@ type Message struct {
ClickEvent *ClickEvent `json:"clickEvent,omitempty"`
HoverEvent *HoverEvent `json:"hoverEvent,omitempty"`
Translate string `json:"translate,omitempty"`
With []json.RawMessage `json:"with,omitempty"`
Extra []Message `json:"extra,omitempty"`
Translate string `json:"translate,omitempty"`
With []Message `json:"with,omitempty"`
Extra []Message `json:"extra,omitempty"`
}
// Same as Message, but "Text" is omitempty
@ -98,9 +96,9 @@ type translateMsg struct {
ClickEvent *ClickEvent `json:"clickEvent,omitempty"`
HoverEvent *HoverEvent `json:"hoverEvent,omitempty"`
Translate string `json:"translate"`
With []json.RawMessage `json:"with,omitempty"`
Extra []Message `json:"extra,omitempty"`
Translate string `json:"translate"`
With []Message `json:"with,omitempty"`
Extra []Message `json:"extra,omitempty"`
}
type jsonMsg Message
@ -180,10 +178,7 @@ func Text(str string) Message {
func TranslateMsg(key string, with ...Message) (m Message) {
m.Translate = key
m.With = make([]json.RawMessage, len(with))
for i, v := range with {
m.With[i], _ = json.Marshal(v)
}
m.With = with
return
}
@ -250,9 +245,7 @@ func (m Message) ClearString() string {
if m.Translate != "" {
args := make([]interface{}, len(m.With))
for i, v := range m.With {
var arg Message
_ = arg.UnmarshalJSON(v) //ignore error
args[i] = arg.ClearString()
args[i] = v.ClearString()
}
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)
@ -297,9 +290,7 @@ func (m Message) String() string {
if m.Translate != "" {
args := make([]interface{}, len(m.With))
for i, v := range m.With {
var arg Message
_ = arg.UnmarshalJSON(v) //ignore error
args[i] = arg
args[i] = v
}
_, _ = fmt.Fprintf(&msg, translateMap[m.Translate], args...)