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(
key: impl Into<Cow<'static, str>>,
with: impl Into<Vec<Text, Global>>
) -> Text
pub fn translate( key: impl Into<Cow<'static, str>>, with: impl Into<Vec<Text, Global>> ) -> Text
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(
name: impl Into<Cow<'static, str>>,
objective: impl Into<Cow<'static, str>>,
value: Option<Cow<'static, str>>
) -> Text
pub fn score( name: impl Into<Cow<'static, str>>, objective: impl Into<Cow<'static, str>>, value: Option<Cow<'static, str>> ) -> Text
Create a score from the scoreboard with an optional custom value.
sourcepub fn selector(
selector: impl Into<Cow<'static, str>>,
separator: Option<Text>
) -> Text
pub fn selector( selector: impl Into<Cow<'static, str>>, separator: Option<Text> ) -> Text
Creates a text component for selecting entity names with an optional custom separator.
sourcepub fn keybind(keybind: impl Into<Cow<'static, str>>) -> Text
pub fn keybind(keybind: impl Into<Cow<'static, str>>) -> Text
Creates a text component for a keybind. The keybind should be a valid
keybind identifier
.
sourcepub fn block_nbt(
block: impl Into<Cow<'static, str>>,
nbt: impl Into<Cow<'static, str>>,
interpret: Option<bool>,
separator: Option<Text>
) -> Text
pub fn block_nbt( block: impl Into<Cow<'static, str>>, nbt: impl Into<Cow<'static, str>>, interpret: Option<bool>, separator: Option<Text> ) -> Text
Creates a text component for a block NBT tag.
sourcepub fn entity_nbt(
entity: impl Into<Cow<'static, str>>,
nbt: impl Into<Cow<'static, str>>,
interpret: Option<bool>,
separator: Option<Text>
) -> Text
pub fn entity_nbt( entity: impl Into<Cow<'static, str>>, nbt: impl Into<Cow<'static, str>>, interpret: Option<bool>, separator: Option<Text> ) -> Text
Creates a text component for an entity NBT tag.
sourcepub fn storage_nbt(
storage: impl Into<Ident<Cow<'static, str>>>,
nbt: impl Into<Cow<'static, str>>,
interpret: Option<bool>,
separator: Option<Text>
) -> Text
pub fn storage_nbt( storage: impl Into<Ident<Cow<'static, str>>>, nbt: impl Into<Cow<'static, str>>, interpret: Option<bool>, separator: Option<Text> ) -> Text
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> AddAssign<T> for Textwhere
T: IntoText<'static>,
impl<T> AddAssign<T> for Textwhere T: IntoText<'static>,
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: 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>,
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.source§impl PartialEq<Text> for Text
impl PartialEq<Text> for Text
source§impl Serialize for Text
impl Serialize for Text
source§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 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
§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, Global>) -> Box<dyn Any, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any, Global>
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, Global>) -> Rc<dyn Any, Global>
fn into_any_rc(self: Rc<T, Global>) -> Rc<dyn Any, Global>
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> 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) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
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) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.source§impl<T> Serialize for Twhere
T: Serialize + ?Sized,
impl<T> Serialize for Twhere T: Serialize + ?Sized,
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
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)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.