Server books: positional page indicator and component-list pages

The page indicator only replaced bare %s, but 26.3's en_us template is
"Page %1$s of %2$s"; it now goes through format_java like other
translations (BookViewScreen.getPageNumberMessage).

Pages sent as a component list (Hypixel through ViaVersion) were dropped
by item_data::read_text, which only took a string or a compound, so the
book opened blank. ComponentSerialization.CODEC also takes a list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavel Mikhailovin
2026-10-26 20:43:35 +03:00
parent 9422f698ee
commit 76d748caf8
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
@@ -305,7 +305,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,
}
}
@@ -899,6 +901,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);