diff options
| author | H3XÐΛΞMѲИ <42803553+H3XDaemon@users.noreply.github.com> | 2025-12-02 00:23:37 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-12-01 10:23:37 -0600 |
| commit | 0110c630aed4b1800a94c22f74a2403df6f30295 (patch) | |
| tree | eb5404295381c7698229d6a72eb8098254758a16 | |
| parent | 6c110f2731c3893af397cc6a660f374ff705c99b (diff) | |
| download | azalea-drasl-0110c630aed4b1800a94c22f74a2403df6f30295.tar.xz | |
fix TranslatableComponent color inheritance (#287)
Recursively process translated components instead of flattening to string to preserve nested styling. Fixes advancement messages not showing correct colors.
| -rw-r--r-- | azalea-chat/src/component.rs | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index be95638d..aabb155f 100644 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -153,28 +153,42 @@ impl FormattedText { F: FnMut(&Style, &Style) -> (String, String), S: FnMut(&str) -> String, { - let component_text = match &self { - Self::Text(c) => c.text.to_string(), - Self::Translatable(c) => match c.read() { - Ok(c) => c.to_string(), - Err(_) => c.key.to_string(), - }, - }; - let component_style = &self.get_base().style; let new_style = parent_style.merged_with(component_style); - if !component_text.is_empty() { - let (formatted_style_prefix, formatted_style_suffix) = - style_formatter(running_style, &new_style); - let formatted_text = text_formatter(&component_text); + let mut append_text = |text: &str| { + if !text.is_empty() { + let (formatted_style_prefix, formatted_style_suffix) = + style_formatter(running_style, &new_style); + let formatted_text = text_formatter(text); - output.push_str(&formatted_style_prefix); - output.push_str(&formatted_text); - output.push_str(&formatted_style_suffix); + output.push_str(&formatted_style_prefix); + output.push_str(&formatted_text); + output.push_str(&formatted_style_suffix); - *running_style = new_style.clone(); - } + *running_style = new_style.clone(); + } + }; + + match &self { + Self::Text(c) => { + append_text(&c.text); + } + Self::Translatable(c) => match c.read() { + Ok(c) => { + FormattedText::Text(c).to_custom_format_recursive( + output, + style_formatter, + text_formatter, + &new_style, + running_style, + ); + } + Err(_) => { + append_text(&c.key); + } + }, + }; for sibling in &self.get_base().siblings { sibling.to_custom_format_recursive( @@ -759,4 +773,16 @@ mod tests { )) ); } + + #[test] + fn test_translatable_with_color_inheritance() { + let json = serde_json::json!({ + "translate": "advancements.story.smelt_iron.title", + "color": "green", + "with": [{"text": "Acquire Hardware"}] + }); + let component = FormattedText::deserialize(&json).unwrap(); + let ansi = component.to_ansi(); + assert!(ansi.contains("\u{1b}[38;2;85;255;85m")); + } } |
