Skip to content

Commit 0110c63

Browse files
authored
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.
1 parent 6c110f2 commit 0110c63

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

azalea-chat/src/component.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,28 +153,42 @@ impl FormattedText {
153153
F: FnMut(&Style, &Style) -> (String, String),
154154
S: FnMut(&str) -> String,
155155
{
156-
let component_text = match &self {
157-
Self::Text(c) => c.text.to_string(),
158-
Self::Translatable(c) => match c.read() {
159-
Ok(c) => c.to_string(),
160-
Err(_) => c.key.to_string(),
161-
},
162-
};
163-
164156
let component_style = &self.get_base().style;
165157
let new_style = parent_style.merged_with(component_style);
166158

167-
if !component_text.is_empty() {
168-
let (formatted_style_prefix, formatted_style_suffix) =
169-
style_formatter(running_style, &new_style);
170-
let formatted_text = text_formatter(&component_text);
159+
let mut append_text = |text: &str| {
160+
if !text.is_empty() {
161+
let (formatted_style_prefix, formatted_style_suffix) =
162+
style_formatter(running_style, &new_style);
163+
let formatted_text = text_formatter(text);
171164

172-
output.push_str(&formatted_style_prefix);
173-
output.push_str(&formatted_text);
174-
output.push_str(&formatted_style_suffix);
165+
output.push_str(&formatted_style_prefix);
166+
output.push_str(&formatted_text);
167+
output.push_str(&formatted_style_suffix);
175168

176-
*running_style = new_style.clone();
177-
}
169+
*running_style = new_style.clone();
170+
}
171+
};
172+
173+
match &self {
174+
Self::Text(c) => {
175+
append_text(&c.text);
176+
}
177+
Self::Translatable(c) => match c.read() {
178+
Ok(c) => {
179+
FormattedText::Text(c).to_custom_format_recursive(
180+
output,
181+
style_formatter,
182+
text_formatter,
183+
&new_style,
184+
running_style,
185+
);
186+
}
187+
Err(_) => {
188+
append_text(&c.key);
189+
}
190+
},
191+
};
178192

179193
for sibling in &self.get_base().siblings {
180194
sibling.to_custom_format_recursive(
@@ -759,4 +773,16 @@ mod tests {
759773
))
760774
);
761775
}
776+
777+
#[test]
778+
fn test_translatable_with_color_inheritance() {
779+
let json = serde_json::json!({
780+
"translate": "advancements.story.smelt_iron.title",
781+
"color": "green",
782+
"with": [{"text": "Acquire Hardware"}]
783+
});
784+
let component = FormattedText::deserialize(&json).unwrap();
785+
let ansi = component.to_ansi();
786+
assert!(ansi.contains("\u{1b}[38;2;85;255;85m"));
787+
}
762788
}

0 commit comments

Comments
 (0)