Chat Message support Encode now

This commit is contained in:
Tnze
2019-07-24 02:54:36 +08:00
parent 6c4924af96
commit a91caff32f
2 changed files with 27 additions and 9 deletions

View File

@ -15,7 +15,7 @@ import (
type Message jsonChat type Message jsonChat
type jsonChat struct { type jsonChat struct {
Text string `json:"text"` Text string `json:"text,omitempty"`
Bold bool `json:"bold,omitempty"` //粗体 Bold bool `json:"bold,omitempty"` //粗体
Italic bool `json:"Italic,omitempty"` //斜体 Italic bool `json:"Italic,omitempty"` //斜体
@ -39,7 +39,7 @@ func (m *Message) UnmarshalJSON(jsonMsg []byte) (err error) {
return return
} }
//Decode a ChatMsg packet //Decode for a ChatMsg packet
func (m *Message) Decode(r pk.DecodeReader) error { func (m *Message) Decode(r pk.DecodeReader) error {
var Len pk.VarInt var Len pk.VarInt
if err := Len.Decode(r); err != nil { if err := Len.Decode(r); err != nil {
@ -49,6 +49,15 @@ func (m *Message) Decode(r pk.DecodeReader) error {
return json.NewDecoder(io.LimitReader(r, int64(Len))).Decode(m) return json.NewDecoder(io.LimitReader(r, int64(Len))).Decode(m)
} }
//Encode for a ChatMsg packet
func (m Message) Encode() []byte {
code, err := json.Marshal(m)
if err != nil {
panic(err)
}
return code
}
var fmtCode = map[byte]string{ var fmtCode = map[byte]string{
'0': "30", '0': "30",
'1': "34", '1': "34",
@ -96,7 +105,7 @@ var colors = map[string]string{
// ClearString return the message without escape sequence for ansi color. // ClearString return the message without escape sequence for ansi color.
func (m Message) ClearString() string { func (m Message) ClearString() string {
var msg strings.Builder var msg strings.Builder
text, _ := transf(m.Text, false) text, _ := trans(m.Text, false)
msg.WriteString(text) msg.WriteString(text)
//handle translate //handle translate
@ -104,11 +113,11 @@ func (m Message) ClearString() string {
args := make([]interface{}, len(m.With)) args := make([]interface{}, len(m.With))
for i, v := range m.With { for i, v := range m.With {
var arg Message var arg Message
arg.UnmarshalJSON(v) //ignore error _ = arg.UnmarshalJSON(v) //ignore error
args[i] = arg.ClearString() args[i] = arg.ClearString()
} }
fmt.Fprintf(&msg, data.EnUs[m.Translate], args...) _, _ = fmt.Fprintf(&msg, data.EnUs[m.Translate], args...)
} }
if m.Extra != nil { if m.Extra != nil {
@ -143,7 +152,7 @@ func (m Message) String() string {
msg.WriteString("\033[" + format.String()[:format.Len()-1] + "m") msg.WriteString("\033[" + format.String()[:format.Len()-1] + "m")
} }
text, ok := transf(m.Text, true) text, ok := trans(m.Text, true)
msg.WriteString(text) msg.WriteString(text)
//handle translate //handle translate
@ -151,11 +160,11 @@ func (m Message) String() string {
args := make([]interface{}, len(m.With)) args := make([]interface{}, len(m.With))
for i, v := range m.With { for i, v := range m.With {
var arg Message var arg Message
arg.UnmarshalJSON(v) //ignore error _ = arg.UnmarshalJSON(v) //ignore error
args[i] = arg args[i] = arg
} }
fmt.Fprintf(&msg, data.EnUs[m.Translate], args...) _, _ = fmt.Fprintf(&msg, data.EnUs[m.Translate], args...)
} }
if m.Extra != nil { if m.Extra != nil {
@ -172,7 +181,7 @@ func (m Message) String() string {
var fmtPat = regexp.MustCompile("(?i)§[0-9A-FK-OR]") var fmtPat = regexp.MustCompile("(?i)§[0-9A-FK-OR]")
func transf(str string, ansi bool) (dst string, change bool) { func trans(str string, ansi bool) (dst string, change bool) {
dst = fmtPat.ReplaceAllStringFunc( dst = fmtPat.ReplaceAllStringFunc(
str, str,
func(str string) string { func(str string) string {

View File

@ -3,6 +3,7 @@ package chat
import ( import (
// "fmt" // "fmt"
//"github.com/mattn/go-colorable"//On Windows need //"github.com/mattn/go-colorable"//On Windows need
"bytes"
"testing" "testing"
) )
@ -87,3 +88,11 @@ func TestChatMsgClearString(t *testing.T) {
} }
} }
} }
func TestMessage_Encode(t *testing.T) {
codeMsg := Message{Translate: "multiplayer.disconnect.server_full"}.Encode()
wantMsg := []byte(`{"translate":"multiplayer.disconnect.server_full"}`)
if !bytes.Equal(codeMsg, wantMsg) {
t.Error("encode Message error: get", string(codeMsg), ", want", string(wantMsg))
}
}