chat

package
v0.0.0-...-9debb76 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package chat is used to construct messages that can be converted to Minecraft's JSON chat format. For more information about this format, visit https:wiki.vg/index.php?title=Chat&oldid=5640. Please note that this package does not implement new functionality from versions newer than 1.8.X, such as web colors.

A simple message can be constructed like this:

msg := Msg{
	Text: "Hello",
	Bold: StyleOn,
	Color: ColorGreen,
}

You can use json.Encoder to convert msg to JSON.

For more complex messages, you might want to use a Builder:

msg := NewBuilder("Hello, ").Bold().Color(ColorRed).
			Append("world").NotBold().Color(ColorYellow).
			Build()

If you change a style or color setting in a Builder, that change applies to the current text but also to all text that is appended later. If you want to prevent this, you must explicitly disable the style or change the color back after calling Append.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a utility for constructing more complex Msg objects. An instance should be obtained using NewBuilder.

func NewBuilder

func NewBuilder(text string) *Builder

NewBuilder constructs a new Builder instance with initial text.

func (*Builder) Append

func (b *Builder) Append(text string) *Builder

Append adds a new child message with the specified text.

func (*Builder) Bold

func (b *Builder) Bold() *Builder

Bold enables the bold style for the current text.

func (*Builder) Build

func (b *Builder) Build() *Msg

Build constructs a Msg using all the previously applied options. You should not use and/or keep a reference to the Builder anymore after calling this function.

func (*Builder) Color

func (b *Builder) Color(c Color) *Builder

Color changes the color for the current text.

func (*Builder) Italic

func (b *Builder) Italic() *Builder

Italic enables the italic style for the current text.

func (*Builder) NotBold

func (b *Builder) NotBold() *Builder

NotBold disables the bold style for the current text.

func (*Builder) NotItalic

func (b *Builder) NotItalic() *Builder

NotItalic disables the italic style for the current text.

func (*Builder) NotObfuscated

func (b *Builder) NotObfuscated() *Builder

NotObfuscated disables the obfuscated style for the current text.

func (*Builder) NotStrikethrough

func (b *Builder) NotStrikethrough() *Builder

NotStrikethrough disables the strikethrough style for the current text.

func (*Builder) NotUnderlined

func (b *Builder) NotUnderlined() *Builder

NotUnderlined disables the underlined style for the current text.

func (*Builder) Obfuscated

func (b *Builder) Obfuscated() *Builder

Obfuscated enables the obfuscated style for the current text.

func (*Builder) Strikethrough

func (b *Builder) Strikethrough() *Builder

Strikethrough enables the strikethrough style for the current text.

func (*Builder) Underlined

func (b *Builder) Underlined() *Builder

Underlined enables the underlined style for the current text.

type Color

type Color uint8

Color represents a color used in a Msg.

const (
	// ColorReset resets the color to the default. Which value that is depends on the context of the Msg.
	ColorReset Color = iota
	ColorBlack
	ColorDarkBlue
	ColorDarkGreen
	ColorDarkCyan
	ColorDarkRed
	ColorPurple
	ColorGold
	ColorGray
	ColorDarkGray
	ColorBlue
	ColorGreen
	ColorCyan
	ColorRed
	ColorPink
	ColorYellow
	ColorWhite
)

func (Color) MarshalText

func (c Color) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

type Msg

type Msg struct {
	Text          string `json:"text"`
	Bold          Style  `json:"bold,omitempty"`
	Italic        Style  `json:"italic,omitempty"`
	Underlined    Style  `json:"underlined,omitempty"`
	Strikethrough Style  `json:"strikethrough,omitempty"`
	Obfuscated    Style  `json:"obfuscated,omitempty"`
	Color         Color  `json:"color,omitempty"`
	Extra         []Msg  `json:"extra,omitempty"`
}

Msg represents a chat message. For complex messages, it might be easier to use Builder to construct them.

func (Msg) String

func (m Msg) String() string

String returns the text from this Msg (and optionally extra ones from Msg.Extra) without any additional formatting.

type Style

type Style uint8
const (
	// StyleInherit indicates that a style option should be inherited from the parent. This is the default behavior.
	StyleInherit Style = iota
	// StyleOn explicitly enables a style option.
	StyleOn
	// StyleOff explicitly disables a style option.
	StyleOff
)

func (Style) MarshalText

func (s Style) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

Jump to

Keyboard shortcuts

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