Send a bundle's contents to a server's clients

BundleContents had no stream codec here: the host never sent what a bundle
holds, so on the server thread, LAN and a dedicated server the new tooltip
showed every bundle empty, and a vanilla server's bundle made its packet
undecodable. Its STREAM_CODEC is a list of ItemStackTemplate, the same as
ChargedProjectiles' (component 52).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 14:20:25 +01:00
parent 7a2de4e328
commit 8d6d1f26a4
3 changed files with 69 additions and 1 deletions
+40
View File
@@ -238,6 +238,46 @@ mod tests {
use super::*;
use horizon_protocol::registry::Ids;
/// A bundle's contents cross the wire, so a server's client can draw
/// its tooltip (BundleContents' stream codec: a list of templates).
#[test]
fn a_bundle_keeps_its_contents_across_the_wire() {
let mut components = vec![String::new(); 60];
components[component::BUNDLE_CONTENTS as usize] = "minecraft:bundle_contents".into();
let items = ["minecraft:air", "minecraft:bundle", "minecraft:stone"];
let builtin = Builtin {
registries: [
(
"minecraft:item".to_owned(),
Ids::from_names(items.iter().map(|s| s.to_string()).collect()),
),
(
"minecraft:data_component_type".to_owned(),
Ids::from_names(components),
),
]
.into(),
};
let dynamic = Dynamic {
registries: Default::default(),
};
let mut bundle = ItemStack::new("minecraft:bundle", 1);
bundle.data_mut().bundle_contents = vec![ItemStack::new("minecraft:stone", 32)];
let wire = crate::lan::guest::stack_to_network(
|name| items.iter().position(|i| *i == name).unwrap_or(0) as i32,
|_, _| None,
&Some(bundle),
)
.unwrap();
let back = from_network(&wire, &builtin, &dynamic);
let contents = &back.data().expect("components").bundle_contents;
assert_eq!(contents.len(), 1);
assert_eq!(
(&*contents[0].item, contents[0].count),
("minecraft:stone", 32)
);
}
/// What a creative client sends (`stack_to_network`) comes back whole on
/// the server: damage, both enchantment lists, repair cost, name, trim.
#[test]
+12
View File
@@ -601,6 +601,18 @@ fn network_stack(
horizon_protocol::item::Component::Stacks(charged),
));
}
// What a bundle holds, for its tooltip (ClientBundleTooltip).
if !d.bundle_contents.is_empty() {
let contents = d
.bundle_contents
.iter()
.filter_map(|c| network_stack(item_id, synced_id, &Some(c.clone())))
.collect();
stack.added.push((
horizon_protocol::item::component::BUNDLE_CONTENTS,
horizon_protocol::item::Component::Stacks(contents),
));
}
// The books a lectern shows and a guest reads.
if !d.writable_pages.is_empty() {
stack.added.push((
+17 -1
View File
@@ -35,6 +35,7 @@ pub mod component {
pub const MAP_ID: i32 = 48;
pub const MAP_POST_PROCESSING: i32 = 50;
pub const CHARGED_PROJECTILES: i32 = 51;
pub const BUNDLE_CONTENTS: i32 = 52;
pub const POTION_CONTENTS: i32 = 53;
pub const WRITABLE_BOOK_CONTENT: i32 = 56;
pub const WRITTEN_BOOK_CONTENT: i32 = 57;
@@ -192,7 +193,7 @@ impl Component {
resolved: r.bool()?,
})
}
CHARGED_PROJECTILES => {
CHARGED_PROJECTILES | BUNDLE_CONTENTS => {
let n = r.count(1024)?;
Self::Stacks((0..n).map(|_| Template::read(r)).collect::<Result<_>>()?)
}
@@ -784,6 +785,21 @@ mod tests {
assert_eq!(w.data, bytes);
}
#[test]
fn bundle_contents_are_item_stack_templates() {
// A bundle (id 7) holding 32 of item 9: BundleContents is a list of
// ItemStackTemplate, as ChargedProjectiles is.
let bytes = [1, 7, 1, 0, 52, 1, 9, 32, 0, 0];
let stack = Slot::read(&mut Reader::new(&bytes)).unwrap().unwrap();
assert_eq!(
stack.component(component::BUNDLE_CONTENTS),
Some(&Component::Stacks(vec![Stack::new(9, 32)]))
);
let mut w = Writer::new();
Slot::write(&Some(stack), &mut w);
assert_eq!(w.data, bytes);
}
#[test]
fn filtered_book_pages_are_read_and_dropped() {
// A writable book with one page "hi" and its filtered "**".