Struct valence_protocol::Text
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> 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> 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
].