chat

package
v0.0.0-...-18e7427 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 12 Imported by: 0

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/Tnze/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

Examples

Constants

View Source
const (
	Chat = iota
	System
	GameInfo
	SayCommand
	MsgCommand
	TeamMsgCommand
	EmoteCommand
	TellrawCommand
)
View Source
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

func SetLanguage(trans map[string]string)

SetLanguage set the default language used by String() and ClearString().

func TransCtrlSeq

func TransCtrlSeq(str string, ansi bool) (dst string, change bool)

TransCtrlSeq will transform control sequences into ANSI code or simply filter them. Depends on the second argument. if the str contains control sequences, returned change=true.

Types

type ClickEvent

type ClickEvent struct {
	Action string `json:"action"`
	Value  string `json:"value"`
}

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.

func ShowText

func ShowText(text Message) *HoverEvent

ShowText show the text to display.

type HoverSub

type HoverSub struct {
	Color string `json:"color"`
	Text  string `json:"text"`
}

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 Text

func Text(str string) Message

func TextPtr

func TextPtr(str string) *Message

func TranslateMsg

func TranslateMsg(key string, with ...Message) (m Message)
Example
package main

import (
	"fmt"

	"github.com/Edouard127/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

func (m Message) Append(extraMsg ...Message) Message

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/Edouard127/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

func (m Message) ClearString() string

ClearString return the message String without escape sequence for ansi color.

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

func (*Message) ReadFrom

func (m *Message) ReadFrom(r io.Reader) (int64, error)

ReadFrom decode Message in a ChatMsg packet

func (Message) SetColor

func (m Message) SetColor(color string) Message

func (Message) String

func (m Message) String() 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/matte/go-colorable.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(raw []byte) (err error)

UnmarshalJSON decode json to Message

func (Message) WriteTo

func (m Message) WriteTo(w io.Writer) (int64, error)

WriteTo encode Message into a ChatMsg packet

type SignedMessage

type SignedMessage struct {
	Message      string
	Timestamp    int64
	Salt         int64
	HasSignature bool
	Signature    []byte
	Count        int
	Ack          pk.FixedBitSet
}

func SignMessage

func SignMessage(msg Message, signature []byte) *SignedMessage

func (SignedMessage) WriteTo

func (s SignedMessage) WriteTo(w io.Writer) (int64, error)

type Type

type Type struct {
	ID         int32
	SenderName Message
	TargetName *Message
}

func (*Type) Decorate

func (t *Type) Decorate(content Message, d *Decoration) (msg Message)

func (*Type) ReadFrom

func (t *Type) ReadFrom(r io.Reader) (n int64, err error)

func (*Type) WriteTo

func (t *Type) WriteTo(w io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL