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();