Skip to main content

TextComponent

Struct TextComponent 

Source
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: TextContent

The content of this text component (text, translation, keybind, etc.).

§style: TextStyle

Styling 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

Source

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.

Source

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.

Source

pub fn color(self, color: TextColor) -> Self

Sets the text color. Returns self for builder-style chaining.

Source

pub fn bold(self, bold: bool) -> Self

Sets bold formatting. Returns self for builder-style chaining.

Source

pub fn italic(self, italic: bool) -> Self

Sets italic formatting. Returns self for builder-style chaining.

Source

pub fn underlined(self, underlined: bool) -> Self

Sets underlined formatting. Returns self for builder-style chaining.

Source

pub fn strikethrough(self, strikethrough: bool) -> Self

Sets strikethrough formatting. Returns self for builder-style chaining.

Source

pub fn obfuscated(self, obfuscated: bool) -> Self

Sets obfuscated formatting. Returns self for builder-style chaining.

Source

pub fn click_event(self, event: ClickEvent) -> Self

Sets the click event. Returns self for builder-style chaining.

Source

pub fn hover_event(self, event: HoverEvent) -> Self

Sets the hover event. Returns self for builder-style chaining.

Source

pub fn append(self, child: TextComponent) -> Self

Appends a child component. Returns self for builder-style chaining.

Source

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

Source§

fn clone(&self) -> TextComponent

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextComponent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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§

fn decode(buf: &mut &[u8]) -> Result<Self>

Deserializes a TextComponent from network NBT format.

Fails with Error::Nbt if required fields are missing or have unexpected types.

Source§

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§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Serializes the component to network NBT format.

Source§

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

Returns the byte count of the network NBT encoding.

Source§

impl PartialEq for TextComponent

Source§

fn eq(&self, other: &TextComponent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for TextComponent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.