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§
§impl Text
impl Text
pub fn translate<K, W>(key: K, with: W) -> Text
pub fn translate<K, W>(key: K, with: W) -> Text
Create translated text based on the given translation key, with extra text components to be inserted into the slots of the translation text.
pub fn score<N, O>(
name: N,
objective: O,
value: Option<Cow<'static, str>>,
) -> Text
pub fn score<N, O>( name: N, objective: O, value: Option<Cow<'static, str>>, ) -> Text
Create a score from the scoreboard with an optional custom value.
pub fn selector<S>(selector: S, separator: Option<Text>) -> Text
pub fn selector<S>(selector: S, separator: Option<Text>) -> Text
Creates a text component for selecting entity names with an optional custom separator.
pub fn keybind<K>(keybind: K) -> Text
pub fn keybind<K>(keybind: K) -> Text
Creates a text component for a keybind. The keybind should be a valid
keybind identifier
.
pub fn block_nbt<B, N>(
block: B,
nbt: N,
interpret: Option<bool>,
separator: Option<Text>,
) -> Text
pub fn block_nbt<B, N>( block: B, nbt: N, interpret: Option<bool>, separator: Option<Text>, ) -> Text
Creates a text component for a block NBT tag.
pub fn entity_nbt<E, N>(
entity: E,
nbt: N,
interpret: Option<bool>,
separator: Option<Text>,
) -> Text
pub fn entity_nbt<E, N>( entity: E, nbt: N, interpret: Option<bool>, separator: Option<Text>, ) -> Text
Creates a text component for an entity NBT tag.
pub fn storage_nbt<S, N>(
storage: S,
nbt: N,
interpret: Option<bool>,
separator: Option<Text>,
) -> Text
pub fn storage_nbt<S, N>( storage: S, nbt: N, interpret: Option<bool>, separator: Option<Text>, ) -> Text
Creates a text component for a command storage NBT tag.
pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the text contains no characters. Returns false
otherwise.
pub 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§
§impl<T> AddAssign<T> for Textwhere
T: IntoText<'static>,
impl<T> AddAssign<T> for Textwhere
T: IntoText<'static>,
§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+=
operation. Read more§impl<'de> Deserialize<'de> for Text
impl<'de> Deserialize<'de> for Text
§fn deserialize<D>(
deserializer: D,
) -> Result<Text, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Text, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<'a> IntoText<'a> for &'a Text
impl<'a> IntoText<'a> for &'a Text
§fn into_cow_text(self) -> Cow<'a, Text>
fn into_cow_text(self) -> Cow<'a, Text>
Text
object, either owned or borrowed.§fn clear_color(self) -> Text
fn clear_color(self) -> Text
Text
object will be
used.§fn clear_font(self) -> Text
fn clear_font(self) -> Text
Text
object will be
used.§fn clear_bold(self) -> Text
fn clear_bold(self) -> Text
bold
property of the text. Property of the parent Text
object will be used.§fn not_italic(self) -> Text
fn not_italic(self) -> Text
§fn clear_italic(self) -> Text
fn clear_italic(self) -> Text
italic
property of the text. Property of the parent
Text
object will be used.§fn underlined(self) -> Text
fn underlined(self) -> Text
§fn not_underlined(self) -> Text
fn not_underlined(self) -> Text
§fn clear_underlined(self) -> Text
fn clear_underlined(self) -> Text
underlined
property of the text. Property of the parent
Text
object will be used.§fn strikethrough(self) -> Text
fn strikethrough(self) -> Text
§fn not_strikethrough(self) -> Text
fn not_strikethrough(self) -> Text
§fn clear_strikethrough(self) -> Text
fn clear_strikethrough(self) -> Text
strikethrough
property of the text. Property of the parent
Text
object will be used.§fn obfuscated(self) -> Text
fn obfuscated(self) -> Text
§fn not_obfuscated(self) -> Text
fn not_obfuscated(self) -> Text
§fn clear_obfuscated(self) -> Text
fn clear_obfuscated(self) -> Text
obfuscated
property of the text. Property of the parent
Text
object will be used.§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.§fn clear_insertion(self) -> Text
fn clear_insertion(self) -> Text
insertion
property of the text. Property of the parent
Text
object will be used.§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.§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
§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
§fn on_click_change_page(self, page: impl Into<i32>) -> Text
fn on_click_change_page(self, page: impl Into<i32>) -> Text
1
.§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
§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.§fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
§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<'a> IntoText<'a> for Text
impl<'a> IntoText<'a> for Text
§fn into_cow_text(self) -> Cow<'a, Text>
fn into_cow_text(self) -> Cow<'a, Text>
Text
object, either owned or borrowed.§fn clear_color(self) -> Text
fn clear_color(self) -> Text
Text
object will be
used.§fn clear_font(self) -> Text
fn clear_font(self) -> Text
Text
object will be
used.§fn clear_bold(self) -> Text
fn clear_bold(self) -> Text
bold
property of the text. Property of the parent Text
object will be used.§fn not_italic(self) -> Text
fn not_italic(self) -> Text
§fn clear_italic(self) -> Text
fn clear_italic(self) -> Text
italic
property of the text. Property of the parent
Text
object will be used.§fn underlined(self) -> Text
fn underlined(self) -> Text
§fn not_underlined(self) -> Text
fn not_underlined(self) -> Text
§fn clear_underlined(self) -> Text
fn clear_underlined(self) -> Text
underlined
property of the text. Property of the parent
Text
object will be used.§fn strikethrough(self) -> Text
fn strikethrough(self) -> Text
§fn not_strikethrough(self) -> Text
fn not_strikethrough(self) -> Text
§fn clear_strikethrough(self) -> Text
fn clear_strikethrough(self) -> Text
strikethrough
property of the text. Property of the parent
Text
object will be used.§fn obfuscated(self) -> Text
fn obfuscated(self) -> Text
§fn not_obfuscated(self) -> Text
fn not_obfuscated(self) -> Text
§fn clear_obfuscated(self) -> Text
fn clear_obfuscated(self) -> Text
obfuscated
property of the text. Property of the parent
Text
object will be used.§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.§fn clear_insertion(self) -> Text
fn clear_insertion(self) -> Text
insertion
property of the text. Property of the parent
Text
object will be used.§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.§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
§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
§fn on_click_change_page(self, page: impl Into<i32>) -> Text
fn on_click_change_page(self, page: impl Into<i32>) -> Text
1
.§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
§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.§fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
fn on_hover_show_text(self, text: impl IntoText<'static>) -> Text
§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 Serialize for Text
impl Serialize for Text
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
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
)§impl<T> Conv for T
impl<T> Conv for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.