chat

package
v0.0.0-...-71b1cdd Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: MIT Imports: 11 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

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" nbt:"action"`
	Value  string `json:"value" nbt:"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" nbt:"action"`
	Contents any     `json:"contents" nbt:"contents"` // Didn't handled yet
	Value    Message `json:"value" nbt:"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 JsonMessage

type JsonMessage Message

JsonMessage is Message, unless when it is going to be Json instead of NBT

func (*JsonMessage) ReadFrom

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

ReadFrom decode Message in a JSON Text component

func (JsonMessage) WriteTo

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

WriteTo encode Message into a JSON Text component

type Message

type Message struct {
	Text string `json:"text" nbt:"text"`

	Bold          bool `json:"bold,omitempty" nbt:"bold,omitempty"`                   // 粗体
	Italic        bool `json:"italic,omitempty" nbt:"italic,omitempty"`               // 斜体
	UnderLined    bool `json:"underlined,omitempty" nbt:"underlined,omitempty"`       // 下划线
	StrikeThrough bool `json:"strikethrough,omitempty" nbt:"strikethrough,omitempty"` // 删除线
	Obfuscated    bool `json:"obfuscated,omitempty" nbt:"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" nbt:"font,omitempty"`   // 字体
	Color string `json:"color,omitempty" nbt:"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" nbt:"insertion,omitempty"`
	ClickEvent *ClickEvent `json:"clickEvent,omitempty" nbt:"clickEvent,omitempty"`
	HoverEvent *HoverEvent `json:"hoverEvent,omitempty" nbt:"hoverEvent,omitempty"`

	Translate string    `json:"translate,omitempty" nbt:"translate,omitempty"`
	With      []Message `json:"with,omitempty" nbt:"with,omitempty"`
	Extra     []Message `json:"extra,omitempty" nbt:"extra,omitempty"`
}

Message is a message sent by other

func Text

func Text(str string) Message

func TranslateMsg

func TranslateMsg(key string, with ...Message) (m Message)

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.

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) MarshalNBT

func (m Message) MarshalNBT(w io.Writer) error

func (*Message) ReadFrom

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

ReadFrom decode Message in a Text component

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/mattn/go-colorable.

func (Message) TagType

func (m Message) TagType() byte

func (*Message) UnmarshalJSON

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

func (*Message) UnmarshalNBT

func (m *Message) UnmarshalNBT(tagType byte, r nbt.DecoderReader) error

func (Message) WriteTo

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

WriteTo encode Message into a Text component

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)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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