Advancement chat hover: normalise the title with canonical_component

scoreboard::canonical_component already re-encodes a component as vanilla
does, list form included, so decorate_name uses it instead of its own copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
some-du6e
2026-10-24 18:46:24 -06:00
parent 9f1a880951
commit f66c257ae5
+9 -24
View File
@@ -897,10 +897,14 @@ fn frame_color(frame: Frame) -> u32 {
/// `Advancement.decorateName`: a copy of the title with the frame's colour
/// under its own style (mergeStyles), then a new line and the description
/// after the title's own siblings, so both inherit the title's root style.
/// Built on the title's JSON: a parsed Text moves a styled root's style down
/// a level once it has siblings.
/// Built on the title's JSON (a list as its first element with the rest as
/// siblings): a parsed Text moves a styled root's style down a level once it
/// has siblings.
fn decorate_name(d: &adv::Display) -> Text {
let mut component = root_component(&d.title);
let mut component = match horizon_client::scoreboard::canonical_component(&d.title) {
Value::String(s) => serde_json::json!({ "text": s }),
c => c,
};
if let Some(o) = component.as_object_mut() {
let frame = Text::empty().with_color(frame_color(d.frame)).component();
o.entry("color").or_insert_with(|| frame["color"].clone());
@@ -911,25 +915,6 @@ fn decorate_name(d: &adv::Display) -> Text {
}
text_of(&component).with_component(component)
}
/// A component's JSON as one object whose root keeps its own style: a list is
/// its first element with the rest appended to its siblings.
fn root_component(v: &Value) -> Value {
match v {
Value::Object(_) => v.clone(),
Value::String(s) => serde_json::json!({ "text": s }),
Value::Array(list) if !list.is_empty() => {
let mut root = root_component(&list[0]);
if let Some(o) = root.as_object_mut() {
let extra = o.entry("extra").or_insert_with(|| serde_json::json!([]));
if let Some(extra) = extra.as_array_mut() {
extra.extend(list[1..].iter().cloned());
}
}
root
}
other => text_of(other).component(),
}
}
impl Game<'_> {
/// `AdvancementCommands.Action.GRANT` on one advancement (or one of
/// its criteria): false when nothing changed.
@@ -1178,7 +1163,7 @@ mod tests {
#[test]
fn chat_hover_description_takes_a_styled_titles_own_style() {
// mergeStyles keeps the title's gold; the description inherits it.
let a = announced(
let object = announced(
serde_json::json!({"text": "Quest", "color": "gold", "bold": true, "extra": [" Board"]}),
serde_json::json!("Open the board"),
);
@@ -1186,7 +1171,7 @@ mod tests {
serde_json::json!([{"text": "Quest", "color": "gold", "bold": true}, " Board"]),
serde_json::json!("Open the board"),
);
for a in [a, list] {
for a in [object, list] {
let (sent, local) = hover(&a);
let sent = horizon_client::scoreboard::comp_text(&sent);
assert_eq!(sent.plain(), "Quest Board\nOpen the board");