diff options
| author | mat <git@matdoes.dev> | 2026-01-19 16:48:49 -1100 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2026-01-19 16:48:49 -1100 |
| commit | b7ad0e60f7b0a5f5a1f6a2c80abc865fdb250ee5 (patch) | |
| tree | 683c9ea01e0be320f76cd817fbf9da4f9c5d76d8 /azalea-chat/src | |
| parent | 7c1be0aa47cfee6ffcb114860ccb715dbad109a5 (diff) | |
| download | azalea-drasl-b7ad0e60f7b0a5f5a1f6a2c80abc865fdb250ee5.tar.xz | |
deserialize ClickEvent from nbt
Diffstat (limited to 'azalea-chat/src')
| -rw-r--r-- | azalea-chat/src/click_event.rs | 138 | ||||
| -rw-r--r-- | azalea-chat/src/lib.rs | 12 | ||||
| -rw-r--r-- | azalea-chat/src/style.rs | 24 |
3 files changed, 111 insertions, 63 deletions
diff --git a/azalea-chat/src/click_event.rs b/azalea-chat/src/click_event.rs index a61e2561..01b196ff 100644 --- a/azalea-chat/src/click_event.rs +++ b/azalea-chat/src/click_event.rs @@ -1,78 +1,102 @@ use serde::Serialize; #[cfg(feature = "simdnbt")] -use simdnbt::owned::{Nbt, NbtCompound, NbtTag}; +use simdnbt::{ + DeserializeError, + owned::{Nbt, NbtCompound}, +}; -#[derive(Clone, Debug, PartialEq, Serialize)] -#[serde(rename_all = "snake_case", tag = "action")] -pub enum ClickEvent { - OpenUrl { +#[cfg(feature = "simdnbt")] +use crate::get_in_compound; + +macro_rules! define_click_event_struct { + ( + $( + $action_name:ident : $action_variant:ident { + $( + $(#[$meta:meta])* $field:ident : $type:ty + ),* + $(,)? + } + ),* + $(,)? + ) => { + #[derive(Clone, Debug, PartialEq, Serialize)] + #[serde(rename_all = "snake_case", tag = "action")] + pub enum ClickEvent { + $( + $action_variant { + $( + $(#[$meta])* + $field: $type + ),* + } + ),* + } + + #[cfg(feature = "simdnbt")] + impl simdnbt::Serialize for ClickEvent { + fn to_compound(self) -> NbtCompound { + let mut compound = NbtCompound::new(); + match self { + $( + Self::$action_variant { $($field),* } => { + compound.insert("action", stringify!($action_name)); + $( + compound.insert(stringify!($field), $field); + )* + } + )* + }; + compound + } + } + + #[cfg(feature = "simdnbt")] + impl simdnbt::Deserialize for ClickEvent { + fn from_compound( + compound: simdnbt::borrow::NbtCompound, + ) -> Result<Self, simdnbt::DeserializeError> { + let action = get_in_compound::<String>(&compound, "action")?; + Ok(match action.as_str() { + $( + stringify!($action_name) => Self::$action_variant { + $( + $field: get_in_compound(&compound, stringify!($field))? + ),* + }, + )* + _ => return Err(DeserializeError::MismatchedFieldType(action.to_owned())), + }) + } + } + + } +} + +define_click_event_struct! { + open_url: OpenUrl { url: String, }, - OpenFile { + open_file: OpenFile { path: String, }, - RunCommand { + run_command: RunCommand { command: String, }, - SuggestCommand { + suggest_command: SuggestCommand { command: String, }, // TODO: this uses Dialog.CODEC - ShowDialog, - ChangePage { + show_dialog: ShowDialog {}, + change_page: ChangePage { page: i32, }, - CopyToClipboard { + copy_to_clipboard: CopyToClipboard { value: String, }, - Custom { + custom: Custom { id: String, #[cfg(feature = "simdnbt")] payload: Nbt, }, } - -#[cfg(feature = "simdnbt")] -impl simdnbt::Serialize for ClickEvent { - fn to_compound(self) -> NbtCompound { - let mut compound = NbtCompound::new(); - let mut action = |s: &str| { - compound.insert("action", s); - }; - match self { - ClickEvent::OpenUrl { url } => { - action("open_url"); - compound.insert("url", url); - } - ClickEvent::OpenFile { path } => { - action("open_file"); - compound.insert("path", path); - } - ClickEvent::RunCommand { command } => { - action("run_command"); - compound.insert("command", command); - } - ClickEvent::SuggestCommand { command } => { - action("suggest_command"); - compound.insert("command", command); - } - ClickEvent::ShowDialog => { - action("show_dialog"); - } - ClickEvent::ChangePage { page } => { - action("change_page"); - compound.insert("page", NbtTag::Int(page)); - } - ClickEvent::CopyToClipboard { value } => { - action("copy_to_clipboard"); - compound.insert("value", value); - } - ClickEvent::Custom { id, payload } => { - action("custom"); - compound.insert("id", id); - compound.insert("payload", (**payload).clone()); - } - } - compound - } -} diff --git a/azalea-chat/src/lib.rs b/azalea-chat/src/lib.rs index 4e0c38bf..fef2697e 100644 --- a/azalea-chat/src/lib.rs +++ b/azalea-chat/src/lib.rs @@ -11,3 +11,15 @@ pub mod text_component; pub mod translatable_component; pub use component::{DEFAULT_STYLE, FormattedText}; +#[cfg(feature = "simdnbt")] +use simdnbt::{DeserializeError, FromNbtTag}; + +// TODO: remove this after simdnbt refactor +#[cfg(feature = "simdnbt")] +pub(crate) fn get_in_compound<T: FromNbtTag>( + compound: &simdnbt::borrow::NbtCompound, + key: &str, +) -> Result<T, DeserializeError> { + let value = compound.get(key).ok_or(DeserializeError::MissingField)?; + T::from_nbt_tag(value).ok_or(DeserializeError::MissingField) +} diff --git a/azalea-chat/src/style.rs b/azalea-chat/src/style.rs index 245e95c1..59518eb6 100644 --- a/azalea-chat/src/style.rs +++ b/azalea-chat/src/style.rs @@ -585,22 +585,34 @@ impl simdnbt::Deserialize for Style { fn from_compound( compound: simdnbt::borrow::NbtCompound, ) -> Result<Self, simdnbt::DeserializeError> { - let bold = compound.byte("bold").map(|v| v != 0); - let italic = compound.byte("italic").map(|v| v != 0); - let underlined = compound.byte("underlined").map(|v| v != 0); - let strikethrough = compound.byte("strikethrough").map(|v| v != 0); - let obfuscated = compound.byte("obfuscated").map(|v| v != 0); + use crate::get_in_compound; + let color: Option<TextColor> = compound .string("color") .and_then(|v| TextColor::parse(&v.to_str())); + let shadow_color = get_in_compound(&compound, "shadow_color").ok(); + let bold = get_in_compound(&compound, "bold").ok(); + let italic = get_in_compound(&compound, "italic").ok(); + let underlined = get_in_compound(&compound, "underlined").ok(); + let strikethrough = get_in_compound(&compound, "strikethrough").ok(); + let obfuscated = get_in_compound(&compound, "obfuscated").ok(); + let click_event = get_in_compound(&compound, "click_event").ok(); + // TODO + // let hover_event = get_in_compound(&compound, "hover_event")?; + let insertion = get_in_compound(&compound, "insertion").ok(); + let font = get_in_compound(&compound, "font").ok(); Ok(Style { color, + shadow_color, bold, italic, underlined, strikethrough, obfuscated, - ..Style::default() + click_event, + hover_event: None, + insertion, + font, }) } } |
