Merge pull request #2320 from pqhaz3925/fix/server-book-pages

🤖 Agent: verified by Ludovic's AI agent (contrib gate 20): server books: page indicator and component-list pages. It passed in this wave's union with main on the Mac mini: the selected parity harnesses (332/332, then 122/122 on the final union), clippy -D warnings and the tests, and lan_e2e with the host on its server thread and in-frame (18/18 each). Thanks @pqhaz3925!
This commit is contained in:
Ludovic Marie
2026-10-27 08:55:04 +01:00
committed by GitHub
2 changed files with 40 additions and 4 deletions
+6 -3
View File
@@ -1009,9 +1009,12 @@ impl Game<'_> {
}
};
let indicator = |batch: &mut HudBatch, page: usize, count: usize| {
let text = tr("book.pageIndicator", "Page %s of %s")
.replacen("%s", &(page + 1).to_string(), 1)
.replacen("%s", &count.max(1).to_string(), 1);
// BookViewScreen.getPageNumberMessage: 26.3's en_us template is
// positional ("Page %1$s of %2$s").
let text = horizon_protocol::text::format_java(
&tr("book.pageIndicator", "Page %1$s of %2$s"),
&[(page + 1).to_string(), count.max(1).to_string()],
);
let tw = HudBatch::text_width(atlas, &text);
batch.text(atlas, &text, left + 148.0 - tw, top + 16.0, BLACK, false);
};
+34 -1
View File
@@ -321,7 +321,9 @@ fn read_text(t: &Tag) -> Option<String> {
// A component's root `text` may be empty with the visible words in
// `extra` (every legacy display name ViaVersion produces looks like
// this), so flatten the whole component instead of reading one key.
Tag::Compound(_) => Some(crate::entity_nbt::name_text(t).plain()),
// ComponentSerialization.CODEC also takes a list (its first element
// styling the rest), as servers behind ViaVersion send book pages.
Tag::Compound(_) | Tag::List(_) => Some(crate::entity_nbt::name_text(t).plain()),
_ => None,
}
}
@@ -916,6 +918,37 @@ mod tests {
);
}
#[test]
fn written_book_pages_may_be_component_lists() {
// A page as Hypixel sends it through ViaVersion: a list, not a compound.
let page = Tag::List(vec![
Tag::compound([("text", Tag::String("".into()))]),
Tag::compound([("text", Tag::String("Please read ".into()))]),
Tag::compound([("text", Tag::String("the rules".into()))]),
]);
let book = Tag::compound([
(
"title",
Tag::compound([("raw", Tag::String("Rules".into()))]),
),
("author", Tag::String("Server".into())),
(
"pages",
Tag::List(vec![
page,
Tag::compound([("raw", Tag::String("Two".into()))]),
]),
),
]);
let components = Tag::compound([("minecraft:written_book_content", book)]);
let (_, extra) = decode_components(Some(&components));
let pages = extra.and_then(|d| d.written.clone()).map(|w| w.pages);
assert_eq!(
pages,
Some(vec!["Please read the rules".to_owned(), "Two".to_owned()])
);
}
#[test]
fn banner_layers_and_shield_base_round_trip() {
let mut stack = ItemStack::new("minecraft:shield", 1);