pub struct TextComponent {
pub content: TextContent,
pub style: TextStyle,
pub extra: Vec<TextComponent>,
}Expand description
A rich text component used for chat messages, disconnect reasons, action bars, titles, and all UI text in the Minecraft protocol.
TextComponent is a recursive tree structure: each component has content,
optional styling, and an optional list of child components (extra).
Children inherit their parent’s style unless they override specific fields.
Since Minecraft 1.20.3, TextComponent is encoded as NBT on the wire
(previously JSON). The Encode/Decode implementations convert
to/from NbtCompound using the network NBT format.
Fields§
§content: TextContentThe content of this text component (text, translation, keybind, etc.).
style: TextStyleStyling applied to this component. None fields inherit from the parent.
extra: Vec<TextComponent>Child components appended after this one, inheriting its style.
Implementations§
Source§impl TextComponent
impl TextComponent
Sourcepub fn text(text: impl Into<String>) -> Self
pub fn text(text: impl Into<String>) -> Self
Creates a plain text component with no styling.
This is the most common way to create a text component for simple messages. The text is displayed as-is with the default chat style.
Sourcepub fn translate(key: impl Into<String>, with: Vec<TextComponent>) -> Self
pub fn translate(key: impl Into<String>, with: Vec<TextComponent>) -> Self
Creates a translation component with substitution arguments.
The client resolves the key against its language file and inserts
the with components at the template’s substitution points.
Sourcepub fn color(self, color: TextColor) -> Self
pub fn color(self, color: TextColor) -> Self
Sets the text color. Returns self for builder-style chaining.
Sourcepub fn bold(self, bold: bool) -> Self
pub fn bold(self, bold: bool) -> Self
Sets bold formatting. Returns self for builder-style chaining.
Sourcepub fn italic(self, italic: bool) -> Self
pub fn italic(self, italic: bool) -> Self
Sets italic formatting. Returns self for builder-style chaining.
Sourcepub fn underlined(self, underlined: bool) -> Self
pub fn underlined(self, underlined: bool) -> Self
Sets underlined formatting. Returns self for builder-style chaining.
Sourcepub fn strikethrough(self, strikethrough: bool) -> Self
pub fn strikethrough(self, strikethrough: bool) -> Self
Sets strikethrough formatting. Returns self for builder-style chaining.
Sourcepub fn obfuscated(self, obfuscated: bool) -> Self
pub fn obfuscated(self, obfuscated: bool) -> Self
Sets obfuscated formatting. Returns self for builder-style chaining.
Sourcepub fn click_event(self, event: ClickEvent) -> Self
pub fn click_event(self, event: ClickEvent) -> Self
Sets the click event. Returns self for builder-style chaining.
Sourcepub fn hover_event(self, event: HoverEvent) -> Self
pub fn hover_event(self, event: HoverEvent) -> Self
Sets the hover event. Returns self for builder-style chaining.
Sourcepub fn append(self, child: TextComponent) -> Self
pub fn append(self, child: TextComponent) -> Self
Appends a child component. Returns self for builder-style chaining.
Sourcepub fn to_nbt(&self) -> NbtCompound
pub fn to_nbt(&self) -> NbtCompound
Converts this text component into an NbtCompound.
Useful for protocol packets that accept an NbtCompound directly
(e.g., SystemChat, KickDisconnect, title packets) instead of
going through the Encode trait.
Trait Implementations§
Source§impl Clone for TextComponent
impl Clone for TextComponent
Source§fn clone(&self) -> TextComponent
fn clone(&self) -> TextComponent
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TextComponent
impl Debug for TextComponent
Source§impl Decode for TextComponent
Decodes a TextComponent from network NBT (compound tag).
impl Decode for TextComponent
Decodes a TextComponent from network NBT (compound tag).
Reads a network NBT compound, then parses it into a TextComponent tree. Handles all content types (text, translate, keybind, score, selector), all style fields, click/hover events, and recursive extra children.
Source§impl Encode for TextComponent
Encodes a TextComponent as network NBT (compound tag).
impl Encode for TextComponent
Encodes a TextComponent as network NBT (compound tag).
Converts the component tree into an NbtCompound, then encodes it using the network NBT format (1.20.3+). The resulting bytes can be used directly in protocol packets for chat, disconnect, title, etc.
Source§impl EncodedSize for TextComponent
Computes the wire size of a TextComponent as network NBT.
impl EncodedSize for TextComponent
Computes the wire size of a TextComponent as network NBT.
Converts to NbtCompound and delegates to its EncodedSize. This involves building the full NBT tree, so it is not free — use sparingly or cache the result when encoding multiple times.
Source§fn encoded_size(&self) -> usize
fn encoded_size(&self) -> usize
Returns the byte count of the network NBT encoding.