Struct valence_text::Text
source · pub struct Text(/* private fields */);
Expand description
Represents formatted text in Minecraft’s JSON text format.
Text is used in various places such as chat, window titles, disconnect messages, written books, signs, and more.
For more information, see the relevant Minecraft Wiki article.
§Examples
With IntoText
in scope, you can write the following:
use valence_text::{Color, IntoText, Text};
let txt = "The text is ".into_text()
+ "Red".color(Color::RED)
+ ", "
+ "Green".color(Color::GREEN)
+ ", and also "
+ "Blue".color(Color::BLUE)
+ "! And maybe even "
+ "Italic".italic()
+ ".";
assert_eq!(
txt.to_string(),
r#"{"text":"The text is ","extra":[{"text":"Red","color":"red"},{"text":", "},{"text":"Green","color":"green"},{"text":", and also "},{"text":"Blue","color":"blue"},{"text":"! And maybe even "},{"text":"Italic","italic":true},{"text":"."}]}"#
);
Implementations§
source§impl Text
impl Text
sourcepub fn translate<K, W>(key: K, with: W) -> Self
pub fn translate<K, W>(key: K, with: W) -> Self
Create translated text based on the given translation key, with extra text components to be inserted into the slots of the translation text.
sourcepub fn score<N, O>(
name: N,
objective: O,
value: Option<Cow<'static, str>>,
) -> Self
pub fn score<N, O>( name: N, objective: O, value: Option<Cow<'static, str>>, ) -> Self
Create a score from the scoreboard with an optional custom value.
sourcepub fn selector<S>(selector: S, separator: Option<Text>) -> Self
pub fn selector<S>(selector: S, separator: Option<Text>) -> Self
Creates a text component for selecting entity names with an optional custom separator.
sourcepub fn keybind<K>(keybind: K) -> Self
pub fn keybind<K>(keybind: K) -> Self
Creates a text component for a keybind. The keybind should be a valid
keybind identifier
.
sourcepub fn block_nbt<B, N>(
block: B,
nbt: N,
interpret: Option<bool>,
separator: Option<Text>,
) -> Self
pub fn block_nbt<B, N>( block: B, nbt: N, interpret: Option<bool>, separator: Option<Text>, ) -> Self
Creates a text component for a block NBT tag.
sourcepub fn entity_nbt<E, N>(
entity: E,
nbt: N,
interpret: Option<bool>,
separator: Option<Text>,
) -> Self
pub fn entity_nbt<E, N>( entity: E, nbt: N, interpret: Option<bool>, separator: Option<Text>, ) -> Self
Creates a text component for an entity NBT tag.
sourcepub fn storage_nbt<S, N>(
storage: S,
nbt: N,
interpret: Option<bool>,
separator: Option<Text>,
) -> Self
pub fn storage_nbt<S, N>( storage: S, nbt: N, interpret: Option<bool>, separator: Option<Text>, ) -> Self
Creates a text component for a command storage NBT tag.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the text contains no characters. Returns false
otherwise.
sourcepub fn to_legacy_lossy(&self) -> String
pub fn to_legacy_lossy(&self) -> String
Converts the Text
object to a plain string with the legacy formatting (§
and format codes)
Removes everything that can’t be represented with a §
and a modifier.
Any colors not on the the legacy color list will be replaced with their closest equivalent.
Trait Implementations§
source§impl<T: IntoText<'static>> AddAssign<T> for Text
impl<T: IntoText<'static>> AddAssign<T> for Text
source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Text
impl<'de> Deserialize<'de> for Text
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
source§impl<'a> IntoText<'a> for &'a Text
impl<'a> IntoText<'a> for &'a Text
source§fn clear_color(self) -> Text
fn clear_color(self) -> Text
Text
object will be
used.source§fn clear_font(self) -> Text
fn clear_font(self) -> Text
Text
object will be
used.source§fn clear_bold(self) -> Text
fn clear_bold(self) -> Text
bold
property of the text. Property of the parent Text
object will be used.source§fn not_italic(self) -> Text
fn not_italic(self) -> Text
source§fn clear_italic(self) -> Text
fn clear_italic(self) -> Text
italic
property of the text. Property of the parent
Text
object will be used.source§fn underlined(self) -> Text
fn underlined(self) -> Text
source§fn not_underlined(self) -> Text
fn not_underlined(self) -> Text
source§fn clear_underlined(self) -> Text
fn clear_underlined(self) -> Text
underlined
property of the text. Property of the parent
Text
object will be used.source§fn strikethrough(self) -> Text
fn strikethrough(self) -> Text
source§fn not_strikethrough(self) -> Text
fn not_strikethrough(self) -> Text
source§fn clear_strikethrough(self) -> Text
fn clear_strikethrough(self) -> Text
strikethrough
property of the text. Property of the parent
Text
object will be used.source§fn obfuscated(self) -> Text
fn obfuscated(self) -> Text
source§fn not_obfuscated(self) -> Text
fn not_obfuscated(self) -> Text
source§fn clear_obfuscated(self) -> Text
fn clear_obfuscated(self) -> Text
obfuscated
property of the text. Property of the parent
Text
object will be used.source§fn insertion(self, insertion: impl Into<Cow<'static, str>>) -> Text
fn insertion(self, insertion: impl Into<Cow<'static, str>>) -> Text
insertion
property to the text. When shift-clicked, the given
text will be inserted into chat box for the client.source§fn clear_insertion(self) -> Text
fn clear_insertion(self) -> Text
insertion
property of the text. Property of the parent
Text
object will be used.source§fn on_click_open_url(self, url: impl Into<Cow<'static, str>>) -> Text
fn on_click_open_url(self, url: impl Into<Cow<'static, str>>) -> Text
http
or https
protocol.source§fn on_click_run_command(self, command: impl Into<Cow<'static, str>>) -> Text
fn on_click_run_command(self, command: impl Into<Cow<'static, str>>) -> Text
source§fn on_click_suggest_command(self, command: impl Into<Cow<'static, str>>) -> Text
fn on_click_suggest_command(self, command: impl Into<Cow<'static, str>>) -> Text
source§fn on_click_change_page(self, page: impl Into<i32>) -> Text
fn on_click_change_page(self, page: impl Into<i32>) -> Text
1
.source§fn on_click_copy_to_clipboard(self, text: impl Into<Cow<'static, str>>) -> Text
fn on_click_copy_to_clipboard(self, text: impl Into<Cow<'static, str>>) -> Text
source§fn clear_click_event(self) -> Text
fn clear_click_event(self) -> Text
click_event
property of the text. Property of the parent
Text
object will be used.source§fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
source§fn clear_hover_event(self) -> Text
fn clear_hover_event(self) -> Text
hover_event
property of the text. Property of the parent
Text
object will be used.source§impl<'a> IntoText<'a> for Text
impl<'a> IntoText<'a> for Text
source§fn clear_color(self) -> Text
fn clear_color(self) -> Text
Text
object will be
used.source§fn clear_font(self) -> Text
fn clear_font(self) -> Text
Text
object will be
used.source§fn clear_bold(self) -> Text
fn clear_bold(self) -> Text
bold
property of the text. Property of the parent Text
object will be used.source§fn not_italic(self) -> Text
fn not_italic(self) -> Text
source§fn clear_italic(self) -> Text
fn clear_italic(self) -> Text
italic
property of the text. Property of the parent
Text
object will be used.source§fn underlined(self) -> Text
fn underlined(self) -> Text
source§fn not_underlined(self) -> Text
fn not_underlined(self) -> Text
source§fn clear_underlined(self) -> Text
fn clear_underlined(self) -> Text
underlined
property of the text. Property of the parent
Text
object will be used.source§fn strikethrough(self) -> Text
fn strikethrough(self) -> Text
source§fn not_strikethrough(self) -> Text
fn not_strikethrough(self) -> Text
source§fn clear_strikethrough(self) -> Text
fn clear_strikethrough(self) -> Text
strikethrough
property of the text. Property of the parent
Text
object will be used.source§fn obfuscated(self) -> Text
fn obfuscated(self) -> Text
source§fn not_obfuscated(self) -> Text
fn not_obfuscated(self) -> Text
source§fn clear_obfuscated(self) -> Text
fn clear_obfuscated(self) -> Text
obfuscated
property of the text. Property of the parent
Text
object will be used.source§fn insertion(self, insertion: impl Into<Cow<'static, str>>) -> Text
fn insertion(self, insertion: impl Into<Cow<'static, str>>) -> Text
insertion
property to the text. When shift-clicked, the given
text will be inserted into chat box for the client.source§fn clear_insertion(self) -> Text
fn clear_insertion(self) -> Text
insertion
property of the text. Property of the parent
Text
object will be used.source§fn on_click_open_url(self, url: impl Into<Cow<'static, str>>) -> Text
fn on_click_open_url(self, url: impl Into<Cow<'static, str>>) -> Text
http
or https
protocol.source§fn on_click_run_command(self, command: impl Into<Cow<'static, str>>) -> Text
fn on_click_run_command(self, command: impl Into<Cow<'static, str>>) -> Text
source§fn on_click_suggest_command(self, command: impl Into<Cow<'static, str>>) -> Text
fn on_click_suggest_command(self, command: impl Into<Cow<'static, str>>) -> Text
source§fn on_click_change_page(self, page: impl Into<i32>) -> Text
fn on_click_change_page(self, page: impl Into<i32>) -> Text
1
.source§fn on_click_copy_to_clipboard(self, text: impl Into<Cow<'static, str>>) -> Text
fn on_click_copy_to_clipboard(self, text: impl Into<Cow<'static, str>>) -> Text
source§fn clear_click_event(self) -> Text
fn clear_click_event(self) -> Text
click_event
property of the text. Property of the parent
Text
object will be used.source§fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
source§fn clear_hover_event(self) -> Text
fn clear_hover_event(self) -> Text
hover_event
property of the text. Property of the parent
Text
object will be used.impl StructuralPartialEq for Text
Auto Trait Implementations§
impl Freeze for Text
impl RefUnwindSafe for Text
impl Send for Text
impl Sync for Text
impl Unpin for Text
impl UnwindSafe for Text
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)