Documentation ¶
Overview ¶
Package chat implements Minecraft's chat message encoding system.
The type Message is the Minecraft chat message. Can be encoded as JSON or net/packet.Field .
It's very recommended that use SetLanguage before using Message.String or Message.ClearString, or the `github.com/SkyVillageMC/go-mc/data/en-us` will be used. Note: The package of data/lang/... will SetLanguage on theirs init() so you don't need to call by your self.
Some of these docs is copied from https://wiki.vg/Chat.
Index ¶
- Constants
- func SetLanguage(trans map[string]string)
- func TransCtrlSeq(str string, ansi bool) (dst string, change bool)
- type ClickEvent
- type Decoration
- type HoverEvent
- type HoverSub
- type Message
- func (m Message) Append(extraMsg ...Message) Message
- func (m Message) ClearString() string
- func (m Message) MarshalJSON() ([]byte, error)
- func (m *Message) ReadFrom(r io.Reader) (int64, error)
- func (m Message) SetColor(color string) Message
- func (m Message) String() string
- func (m *Message) UnmarshalJSON(raw []byte) (err error)
- func (m Message) WriteTo(w io.Writer) (int64, error)
- type Type
Examples ¶
Constants ¶
const ( Chat = iota System GameInfo SayCommand MsgCommand TeamMsgCommand EmoteCommand TellrawCommand )
const ( Black = "black" DarkBlue = "dark_blue" DarkGreen = "dark_green" DarkAqua = "dark_aqua" DarkRed = "dark_red" DarkPurple = "dark_purple" Gold = "gold" Gray = "gray" DarkGray = "dark_gray" Blue = "blue" Green = "green" Aqua = "aqua" Red = "red" LightPurple = "light_purple" Yellow = "yellow" White = "white" )
Colors
Variables ¶
This section is empty.
Functions ¶
func SetLanguage ¶
SetLanguage set the default language used by String() and ClearString().
Types ¶
type ClickEvent ¶
ClickEvent defines an event that occurs when this component is clicked.
func ChangePage ¶
func ChangePage(page int) *ClickEvent
ChangePage create a ClickEvent usable within written books. Changes the page of the book to the given page, starting at 1. For instance, "value":1 switches the book to the first page. If the page is less than one or beyond the number of pages in the book, the event is ignored.
func CopyToClipboard ¶
func CopyToClipboard(text string) *ClickEvent
CopyToClipboard create a ClickEvent copies the given text to the client's clipboard when clicked.
func OpenURL ¶
func OpenURL(url string) *ClickEvent
OpenURL create a ClickEvent opens the given URL in the default web browser. Ignored if the player has opted to disable links in chat; may open a GUI prompting the user if the setting for that is enabled. The link's protocol must be set and must be http or https, for security reasons.
func RunCommand ¶
func RunCommand(cmd string) *ClickEvent
RunCommand create a ClickEvent runs the given command. Not required to be a command - clicking this only causes the client to send the given content as a chat message, so if not prefixed with /, they will say the given text instead. If used in a book GUI, the GUI is closed after clicking.
func SuggestCommand ¶
func SuggestCommand(cmd string) *ClickEvent
SuggestCommand create a ClickEvent replaces the content of the chat box with the given text - usually a command, but it is not required to be a command (commands should be prefixed with /). This is only usable for messages in chat.
type Decoration ¶
type Decoration struct { TranslationKey string `nbt:"translation_key"` Parameters []string `nbt:"parameters"` Style struct { Bold bool `nbt:"bold,omitempty"` Italic bool `nbt:"italic,omitempty"` UnderLined bool `nbt:"underlined,omitempty"` StrikeThrough bool `nbt:"strikethrough,omitempty"` Obfuscated bool `nbt:"obfuscated,omitempty"` Color string `nbt:"color,omitempty"` Insertion string `nbt:"insertion,omitempty"` Font string `nbt:"font,omitempty"` } `nbt:"style,omitempty"` }
type HoverEvent ¶
type HoverEvent struct { Action string `json:"action"` Contents json.RawMessage `json:"contents"` // Didn't handled yet Value Message `json:"value"` // Legacy }
HoverEvent defines an event that occurs when this component hovered over.
func ShowEntity ¶
func ShowEntity(entity string) *HoverEvent
ShowEntity show an entity describing by the S-NBT, nbt.StringifiedMessage could help. See: https://wiki.vg/Chat#:~:text=show_entity,given%20entity%20loaded.
func ShowItem ¶
func ShowItem(item string) *HoverEvent
ShowItem show the item to display. Item is encoded as the S-NBT format, nbt.StringifiedMessage could help. See: https://wiki.vg/Chat#:~:text=show_item,in%20red%20instead.
type Message ¶
type Message struct { Text string `json:"text"` Bold bool `json:"bold,omitempty"` // 粗体 Italic bool `json:"italic,omitempty"` // 斜体 UnderLined bool `json:"underlined,omitempty"` // 下划线 StrikeThrough bool `json:"strikethrough,omitempty"` // 删除线 Obfuscated bool `json:"obfuscated,omitempty"` // 随机 // Font of the message, could be one of minecraft:uniform, minecraft:alt or minecraft:default // This option is only valid on 1.16+, otherwise the property is ignored. Font string `json:"font,omitempty"` // 字体 Color string `json:"color,omitempty"` // 颜色 // Insertion contains text to insert. Only used for messages in chat. // When shift is held, clicking the component inserts the given text // into the chat box at the cursor (potentially replacing selected text). Insertion string `json:"insertion,omitempty"` ClickEvent *ClickEvent `json:"clickEvent,omitempty"` HoverEvent *HoverEvent `json:"hoverEvent,omitempty"` Translate string `json:"translate,omitempty"` With []Message `json:"with,omitempty"` Extra []Message `json:"extra,omitempty"` }
Message is a message sent by other
func TranslateMsg ¶
Example ¶
package main import ( "fmt" "github.com/SkyVillageMC/go-mc/chat" ) func main() { fmt.Println(chat.TranslateMsg("translation.test.none")) fmt.Println(chat.TranslateMsg( // translation.test.complex == "Prefix, %s%[2]s again %s and %[1]s lastly %s and also %[1]s again!" "translation.test.complex", chat.Text("1111"), chat.Text("2222"), chat.Text("3333"), ).String()) }
Output: Hello, world! Prefix, 11112222 again 3333 and 1111 lastly 2222 and also 1111 again!
func (Message) Append ¶
Append extra message to the end of the message and return the new one. The source message remains unchanged.
Example ¶
package main import ( "fmt" "github.com/SkyVillageMC/go-mc/chat" ) func main() { msg := chat.Message{Text: "1111"}. Append(chat.Message{Text: "22222"}). Append(chat.Message{Text: "333333"}). Append(chat.Message{Text: "4444444"}) fmt.Print(msg) }
Output: 1111222223333334444444
func (Message) ClearString ¶
ClearString return the message String without escape sequence for ansi color.
func (Message) MarshalJSON ¶
func (Message) String ¶
String return the message string with escape sequence for ansi color. To convert Translated Message to string, you must set On Windows, you may want print this string using github.com/mattn/go-colorable.
func (*Message) UnmarshalJSON ¶
UnmarshalJSON decode json to Message