Skip to content

Component

Namespace Mc.Scripting.Text · Kind class (sealed)

public sealed class Component

An immutable rich text component (vanilla “text component”): text, translatable text, keybind, score or selector content with a style and children. Every method that “changes” a component returns a new one.

Strings convert implicitly to a literal text component (no formatting codes are interpreted, so user input is safe); use Component.Legacy for & codes and Component.Parse for MiniMessage-style tags.

Member Description
IReadOnlyList<Component> Children { get; } The children appended after this component’s own content.
string Content { get; } The literal text of a text component (empty for other kinds).
bool IsEmpty { get; } Whether this is the empty literal component without style or children.
TextColor TextColour { get; } The colour, TextColor.None if inherited.
static Component Empty { get; } The empty component.
public IReadOnlyList<Component> Children { get; }

The children appended after this component’s own content.

public string Content { get; }

The literal text of a text component (empty for other kinds).

public bool IsEmpty { get; }

Whether this is the empty literal component without style or children.

public TextColor TextColour { get; }

The colour, TextColor.None if inherited.

public static Component Empty { get; }

The empty component.

Member Description
Component Append(Component child) Appends a child component.
Component Append(string text) Appends literal text.
Component Bold(bool value = true) Sets bold.
Component Color(TextColor color) Sets the colour.
Component Font(string font) Sets the font resource key.
Component Insertion(string text) Text inserted into the chat box when the component is shift-clicked.
Component Italic(bool value = true) Sets italic.
Component Obfuscated(bool value = true) Sets obfuscated (random characters).
Component OnClick(ClickEvent click) Sets the click event.
Component OnHover(HoverEvent hover) Sets the hover event.
Component Strikethrough(bool value = true) Sets strikethrough.
string ToJson() Serialises to the vanilla JSON text component form (what the native layer parses).
string ToPlainText() The text of this component and its children without formatting (translatable components show their key).
override string ToString()
Component Tooltip(Component text) Shortcut for a text tooltip.
Component Underlined(bool value = true) Sets underlined.
static Component FromJson(string json) Parses the vanilla JSON form (also a bare JSON string).
static Component FromJson(Utf8String json) Parses the vanilla JSON form from engine memory.
static Component Join(params Component[] parts) Concatenates components into one (an empty parent with the parts as children).
static Component Join(Component separator, IEnumerable<Component> parts) Joins with a separator.
static Component Keybind(string key) A component showing the client’s key binding (e.g.
static Component Legacy(string text, char prefix = '&') Converts text with & colour/format codes (“&aGreen &lbold”) to a component.
static Component Parse(string markup) Parses MiniMessage-style tags: <red>, <#ff8800>, <bold>, <italic>, <underlined>, <strikethrough>, <obfuscated>, <reset>, <click:run_command:/cmd>, <hover:show_text:'text'>, and closing tags </red>.
static Component Score(string name, string objective) A scoreboard score.
static Component Selector(string selector) An entity selector (resolved when sent).
static Component Text(string text) A literal text component.
static Component Translatable(string key, params Component[] args) A translatable component (resolved by the client’s language).
public Component Append(Component child)

Appends a child component.

Parameters

Name Type Description
child Component

Returns Component

public Component Append(string text)

Appends literal text.

Parameters

Name Type Description
text string

Returns Component

public Component Bold(bool value = true)

Sets bold.

Parameters

Name Type Description
value bool = true

Returns Component

public Component Color(TextColor color)

Sets the colour.

Parameters

Name Type Description
color TextColor

Returns Component

public Component Font(string font)

Sets the font resource key.

Parameters

Name Type Description
font string

Returns Component

public Component Insertion(string text)

Text inserted into the chat box when the component is shift-clicked.

Parameters

Name Type Description
text string

Returns Component

public Component Italic(bool value = true)

Sets italic.

Parameters

Name Type Description
value bool = true

Returns Component

public Component Obfuscated(bool value = true)

Sets obfuscated (random characters).

Parameters

Name Type Description
value bool = true

Returns Component

public Component OnClick(ClickEvent click)

Sets the click event.

Parameters

Name Type Description
click ClickEvent

Returns Component

public Component OnHover(HoverEvent hover)

Sets the hover event.

Parameters

Name Type Description
hover HoverEvent

Returns Component

public Component Strikethrough(bool value = true)

Sets strikethrough.

Parameters

Name Type Description
value bool = true

Returns Component

public string ToJson()

Serialises to the vanilla JSON text component form (what the native layer parses).

Returns string

public string ToPlainText()

The text of this component and its children without formatting (translatable components show their key).

Returns string

public override string ToString()

Returns string

public Component Tooltip(Component text)

Shortcut for a text tooltip.

Parameters

Name Type Description
text Component

Returns Component

public Component Underlined(bool value = true)

Sets underlined.

Parameters

Name Type Description
value bool = true

Returns Component

public static Component FromJson(string json)

Parses the vanilla JSON form (also a bare JSON string).

Parameters

Name Type Description
json string

Returns Component

Throws

  • System.FormatException: The text is not a valid component.
public static Component FromJson(Utf8String json)

Parses the vanilla JSON form from engine memory.

Parameters

Name Type Description
json Utf8String

Returns Component

public static Component Join(params Component[] parts)

Concatenates components into one (an empty parent with the parts as children).

Parameters

Name Type Description
parts params Component[]

Returns Component

public static Component Join(Component separator, IEnumerable<Component> parts)

Joins with a separator.

Parameters

Name Type Description
separator Component
parts IEnumerable<Component>

Returns Component

public static Component Keybind(string key)

A component showing the client’s key binding (e.g. “key.jump”).

Parameters

Name Type Description
key string

Returns Component

public static Component Legacy(string text, char prefix = '&')

Converts text with & colour/format codes (“&aGreen &lbold”) to a component. && is a literal ampersand, &#RRGGBB a custom colour, &r resets.

Parameters

Name Type Description
text string
prefix char = '&'

Returns Component

public static Component Parse(string markup)

Parses MiniMessage-style tags: <red>, <#ff8800>, <bold>, <italic>, <underlined>, <strikethrough>, <obfuscated>, <reset>, <click:run_command:/cmd>, <hover:show_text:'text'>, and closing tags </red>. Unknown tags are kept as text. Escape a literal < as \<.

Parameters

Name Type Description
markup string

Returns Component

public static Component Score(string name, string objective)

A scoreboard score.

Parameters

Name Type Description
name string Score holder name or selector.
objective string Objective name.

Returns Component

public static Component Selector(string selector)

An entity selector (resolved when sent).

Parameters

Name Type Description
selector string

Returns Component

public static Component Text(string text)

A literal text component.

Parameters

Name Type Description
text string The text, shown verbatim.

Returns Component

public static Component Translatable(string key, params Component[] args)

A translatable component (resolved by the client’s language).

Parameters

Name Type Description
key string Translation key, e.g. “chat.type.admin”.
args params Component[] Arguments substituted for %s / %1$s.

Returns Component

Member Description
static implicit operator Component(string text) Literal text converts implicitly.
public static implicit operator Component(string text)

Literal text converts implicitly.

Parameters

Name Type Description
text string

Returns Component

ClickEvent · Content · HoverEvent · TextColor · Utf8String