From 240e76160bfcb0bcb1f2bfc537f6317aed14d7f0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:56:19 +0200 Subject: [PATCH] Mod content: give mod items and blocks network ids, so they don't travel as air The game and its integrated server map items and blocks to network ids through the registries report (registries.json), which the content root left vanilla: a mod's item in a slot, given or picked up, reached the client as air. The content root now rewrites registries.json too, appending the new items and blocks after vanilla's ids in plan order. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/mod_content.rs | 34 ++++++++++++++++++- .../horizon-client/src/mod_content_tests.rs | 29 ++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index 0497f06b4..b3a836b3a 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -538,6 +538,37 @@ impl<'a> Plan<'a> { } } "items.json" => self.items(doc, true), + "registries.json" => { + // Network ids: the game and its server map items and blocks through + // this report, so new ones get the ids after vanilla's, in plan order. + let new_items = self.items.iter().map(|i| i.rename.to.clone()); + let new_blocks = self.blocks.iter().map(|b| b.decl.id.clone()); + for (registry, names) in [ + ("minecraft:item", new_items.collect::>()), + ("minecraft:block", new_blocks.collect()), + ] { + let Some(J::Obj(entries)) = + doc.get_mut(registry).and_then(|r| r.get_mut("entries")) + else { + return Err(format!("no {registry} entries").into()); + }; + let next = entries + .iter() + .filter_map(|(_, e)| match e { + J::Obj(o) => o.iter().find(|(k, _)| k == "protocol_id")?.1.as_u32(), + _ => None, + }) + .max() + .map_or(0, |m| m + 1); + for (i, name) in names.into_iter().enumerate() { + if entries.iter().any(|(k, _)| *k == name) { + continue; + } + let id = J::Num((next + i as u32).into()); + entries.push((name, J::Obj(vec![("protocol_id".into(), id)]))); + } + } + } "command-registries.json" => { self.names(doc.at("blocks").ok_or("no blocks")?, false); let new_items = || self.items.iter().map(|i| J::Str(i.rename.to.clone())); @@ -618,8 +649,9 @@ impl<'a> Plan<'a> { } /// The JSON and NBT tables rewritten for new blocks and items. -const JSON_TABLES: [&str; 13] = [ +const JSON_TABLES: [&str; 14] = [ "facts.json", + "registries.json", "gameplay-facts.json", "block-behaviours.json", "model-states.json", diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index 1c0907bde..3d38fae4d 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -148,6 +148,35 @@ fn content_root_loads_with_every_table() { new.default_state("ruby:ruby_glass"), Some(BlockStateId(n + 65)) ); + // Network ids: the new items and blocks follow vanilla's in the registries report, which + // the game and its server both read (a missing one travels as air). + let report = |root: &Path| { + horizon_protocol::registry::Builtin::load(&root.join("registries.json")).unwrap() + }; + let (vanilla_ids, ids) = (report(&base), report(&root)); + let item = |b: &horizon_protocol::registry::Builtin, name: &str| { + b.get("minecraft:item").and_then(|r| r.id(name)) + }; + let vanilla_items = (0..) + .take_while(|&i| vanilla_ids.get("minecraft:item").unwrap().name(i).is_some()) + .count() as i32; + assert_eq!( + item(&ids, "minecraft:stone"), + item(&vanilla_ids, "minecraft:stone") + ); + let ruby_item = item(&ids, "ruby:ruby_block").unwrap(); + assert!(ruby_item >= vanilla_items, "{ruby_item} < {vanilla_items}"); + assert_eq!( + ids.get("minecraft:item").unwrap().name(ruby_item), + Some("ruby:ruby_block") + ); + assert!(item(&ids, "ruby:ruby_glass").is_none(), "no item declared"); + assert!( + ids.get("minecraft:block") + .unwrap() + .id("ruby:ruby_glass") + .is_some() + ); let old_facts = BlockFacts::load(&base, old).unwrap(); let facts = BlockFacts::load(&root, new).unwrap();