This commit is contained in:
JunDao
2019-05-01 15:22:42 +08:00
parent 2b58eb07b5
commit 0d436609ce
21 changed files with 91491 additions and 0 deletions

105
chat/chatMsg.go Normal file
View File

@ -0,0 +1,105 @@
package chat
import (
"encoding/json"
"fmt"
"strings"
"github.com/Tnze/go-mc/data"
)
//Message is a message sent by other
type Message jsonChat
type jsonChat struct {
Text string `json:"text"`
Bold bool `json:"bold"` //粗体
Italic bool `json:"Italic"` //斜体
UnderLined bool `json:"underlined"` //下划线
StrikeThrough bool `json:"strikethrough"` //删除线
Obfuscated bool `json:"obfuscated"` //随机
Color string `json:"color"`
Translate string `json:"translate"`
With []json.RawMessage `json:"with"` // How can go handle an JSON array with Object and String?
Extra []jsonChat `json:"extra"`
}
//UnmarshalJSON decode json to Message
func (m *Message) UnmarshalJSON(jsonMsg []byte) (err error) {
if jsonMsg[0] == '"' {
err = json.Unmarshal(jsonMsg, &m.Text)
} else {
err = json.Unmarshal(jsonMsg, (*jsonChat)(m))
}
return
}
var colors = map[string]int{
"black": 30,
"dark_blue": 34,
"dark_green": 32,
"dark_aqua": 36,
"dark_red": 31,
"dark_purple": 35,
"gold": 33,
"gray": 37,
"dark_gray": 90,
"blue": 94,
"green": 92,
"aqua": 96,
"red": 91,
"light_purple": 95,
"yellow": 93,
"white": 97,
}
// String return the message with escape sequence for ansi color.
// On windows, you may want print this string using
// github.com/mattn/go-colorable.
func (m Message) String() string {
var msg, format strings.Builder
if m.Bold {
format.WriteString("1;")
}
if m.Italic {
format.WriteString("3;")
}
if m.UnderLined {
format.WriteString("4;")
}
if m.StrikeThrough {
format.WriteString("9;")
}
if m.Color != "" {
fmt.Fprintf(&format, "%d;", colors[m.Color])
}
if format.Len() > 0 {
msg.WriteString("\033[" + format.String()[:format.Len()-1] + "m")
}
msg.WriteString(m.Text)
if format.Len() > 0 {
msg.WriteString("\033[0m")
}
//handle translate
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
}
fmt.Fprintf(&msg, data.EnUs[m.Translate], args...)
}
if m.Extra != nil {
for i := range m.Extra {
msg.WriteString(Message(m.Extra[i]).String())
}
}
return msg.String()
}

53
chat/chatMsg_test.go Normal file
View File

@ -0,0 +1,53 @@
package chat
import (
// "fmt"
//"github.com/mattn/go-colorable"//On Windows need
"testing"
)
/*
"translation.test.none": "Hello, world!",
"translation.test.complex": "Prefix, %s %[2]s again %s and %[1]s lastly %s and also %[1]s again!"
"Prefix, str1str2 again str3 and str1 lastly str2 and also str1 again!"
"Prefix, str1str2 again str2 and str1 lastly str3 and also str1 again!"
"translation.test.escape": "%%s %%%s %%%%s %%%%%s",
"translation.test.invalid": "hi %",
"translation.test.invalid2": "hi % s",
"translation.test.args": "%s %s",
"translation.test.world":
*/
var jsons = []string{
`{"extra":[{"color":"green","text":"故我依然"},{"color":"white","text":"™ "},{"color":"gray","text":"Kun_QwQ"},{"color":"white","text":": 为什么想要用炼药锅灭火时总是跳不进去"}],"text":""}`,
`{"translate":"chat.type.text","with":[{"insertion":"Xi_Xi_Mi","clickEvent":{"action":"suggest_command","value":"/tell Xi_Xi_Mi "},"hoverEvent":{"action":"show_entity","value":{"text":"{name:\"{\\\"text\\\":\\\"Xi_Xi_Mi\\\"}\",id:\"c1445a67-7551-4d7e-813d-65ef170ae51f\",type:\"minecraft:player\"}"}},"text":"Xi_Xi_Mi"},"好像是这个id。。"]}`,
`{"translate":"translation.test.none"}`,
//`{"translate":"translation.test.complex","with":["str1","str2","str3"]}`,
`{"translate":"translation.test.escape","with":["str1","str2"]}`,
`{"translate":"translation.test.args","with":["str1","str2"]}`,
`{"translate":"translation.test.world"}`,
}
var texts = []string{
"\033[92m故我依然\033[0m\033[97m™ \033[0m\033[37mKun_QwQ\033[0m\033[97m: 为什么想要用炼药锅灭火时总是跳不进去\033[0m",
"<Xi_Xi_Mi> 好像是这个id。。",
"Hello, world!",
//"Prefix, str1str2 again str2 and str1 lastly str3 and also str1 again!",
"%s %str1 %%s %%str2",
"str1 str2",
"world",
}
func TestChatMsgFormatString(t *testing.T) {
for i, v := range jsons {
var cm Message
err := cm.UnmarshalJSON([]byte(v))
if err != nil {
t.Error(err)
}
if cm.String() != texts[i] {
t.Errorf("gets %q, wants %q", cm, texts[i])
}
}
}