diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index b342f45fd..fcf9f3498 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -205,6 +205,29 @@ pub fn upgrade_recipe(recipe: &mut Value) -> bool { true } +/// Block and item ids vanilla renamed after 1.21.1, as its datafixers +/// rename them in worlds (DataVersion 4541: the chain became the iron chain +/// when copper chains came in). A data file naming the old id fails to load +/// in 26.3: a recipe, a loot entry, a processor's block match. +const RENAMED_IDS: [(&str, &str); 1] = [("minecraft:chain", "minecraft:iron_chain")]; + +/// Every string of a data file that is a renamed id ([`RENAMED_IDS`]) set to +/// the new one, in place; whether any was. +pub fn renamed_ids(v: &mut Value) -> bool { + match v { + Value::String(s) => match RENAMED_IDS.iter().find(|(old, _)| s == old) { + Some((_, new)) => { + *s = (*new).to_owned(); + true + } + None => false, + }, + Value::Array(a) => a.iter_mut().fold(false, |c, x| renamed_ids(x) | c), + Value::Object(o) => o.values_mut().fold(false, |c, x| renamed_ids(x) | c), + _ => false, + } +} + /// A 1.21.1 loot table in 26.3's shape, in place: `conditions` lists become /// one `condition` (`all_of` for several), `functions` become `modifier`, /// and conditions and functions name their kind in `type`. A tool match on @@ -410,12 +433,16 @@ pub fn carry( { if let Ok(mut recipe) = serde_json::from_slice::(&bytes) { tags_used(&recipe, &mut convention); - if upgrade_recipe(&mut recipe) { + let renamed = renamed_ids(&mut recipe); + let upgraded = upgrade_recipe(&mut recipe); + if upgraded { report.recipes_upgraded += 1; - bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); } else { report.recipes_kept.push(file.to_owned()); } + if upgraded || renamed { + bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); + } } } else if let Some(kind) = rel .strip_prefix("worldgen/") @@ -433,6 +460,10 @@ pub fn carry( &mut v, &processor_data, &mut types.processors_carried, + ) | crate::processors::rule_tests( + kind, + &mut v, + &mut types.rule_tests_carried, ); let mut changed = crate::worldgen::upgrade(kind, &mut v, types, &set_tags) || carried; @@ -463,6 +494,7 @@ pub fn carry( &processor_data, &mut types.processors_carried, ); + crate::processors::rule_tests(kind, &mut added, &mut types.rule_tests_carried); crate::worldgen::upgrade(kind, &mut added, types, &set_tags); let to = format!( "data/{id}/worldgen/{kind}/{}/{path}", @@ -487,6 +519,7 @@ pub fn carry( if let Ok(mut table) = serde_json::from_slice::(&bytes) { let before = table.clone(); upgrade_loot(&mut table); + renamed_ids(&mut table); if table != before { report.loot_tables_upgraded += 1; bytes = serde_json::to_vec_pretty(&table).unwrap_or(bytes); @@ -809,6 +842,27 @@ mod tests { assert_eq!(new, glass); } + #[test] + fn renames_ids_vanilla_renamed() { + let mut recipe = json!({"type": "minecraft:crafting_shaped", "pattern": ["c"], + "key": {"c": {"item": "minecraft:chain"}}, "result": {"id": "m:sign", "count": 1}}); + assert!(renamed_ids(&mut recipe)); + assert_eq!(recipe["key"]["c"]["item"], "minecraft:iron_chain"); + let mut rule = json!({"input_predicate": {"predicate_type": "minecraft:block_match", + "block": "minecraft:chain"}, "output_state": {"id": "minecraft:chain", + "properties": {"axis": "y"}}}); + assert!(renamed_ids(&mut rule)); + assert_eq!(rule["input_predicate"]["block"], "minecraft:iron_chain"); + assert_eq!(rule["output_state"]["id"], "minecraft:iron_chain"); + // Only whole ids: other blocks and mods' own chains stay. + let mut other = json!([ + "minecraft:chain_command_block", + "m:chain", + "minecraft:iron_chain" + ]); + assert!(!renamed_ids(&mut other)); + } + #[test] fn templates_from_blockstate_properties() { let door = diff --git a/crates/horizon-port-pack/src/processors.rs b/crates/horizon-port-pack/src/processors.rs index 7fcc0fd94..6e22133df 100644 --- a/crates/horizon-port-pack/src/processors.rs +++ b/crates/horizon-port-pack/src/processors.rs @@ -150,6 +150,49 @@ pub fn carry( }) } +/// Repurposed Structures' rule tests and the game's types that run them +/// (`horizon_world::worldgen`'s `rule_test` and `structure::processor`). +const RULE_TESTS: [(&str, &str); 3] = [ + ( + "repurposed_structures:matter_phase_rule_test", + "horizon:matter_phase", + ), + ( + "repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test", + "horizon:piece_origin_axis_aligned_linear_pos", + ), + ( + "repurposed_structures:y_value_pos_rule_test", + "horizon:y_value", + ), +]; + +/// Renames the rule tests written in Java that the game runs under its own +/// type ([`RULE_TESTS`]) in a worldgen file's processor lists; each is +/// counted by type in `carried`. Their fields are the mod's. Whether +/// anything changed. Without this the game, like vanilla, refuses the list +/// and every pool naming it: Repurposed's mineshafts and Nether monument. +pub fn rule_tests(kind: &str, v: &mut Value, carried: &mut BTreeMap) -> bool { + fn rename(v: &mut Value, carried: &mut BTreeMap) -> bool { + match v { + Value::Object(o) => { + let mut changed = false; + if let Some(Value::String(t)) = o.get_mut("predicate_type") + && let Some((old, new)) = RULE_TESTS.iter().find(|(old, _)| t == old) + { + *carried.entry((*old).to_owned()).or_default() += 1; + *t = (*new).to_owned(); + changed = true; + } + o.values_mut().fold(changed, |c, x| rename(x, carried) | c) + } + Value::Array(a) => a.iter_mut().fold(false, |c, x| rename(x, carried) | c), + _ => false, + } + } + each_list(kind, v, &mut |list| rename(list, carried)) +} + /// The data form of a Java processor: the processors to put in its place /// (none for a no-op), or `None` when it has none. fn as_data(kind: &str, p: &Value, data: &ProcessorData) -> Option> { @@ -502,4 +545,58 @@ mod tests { assert_eq!(p[3]["processor_type"], "betterdungeons:candle_processor"); assert_eq!(carried.values().sum::(), 3); } + + #[test] + fn repurposed_rule_tests_become_the_games() { + let rule = |input: Value, position: Value| { + json!({"input_predicate": input, "location_predicate": {"predicate_type": + "repurposed_structures:matter_phase_rule_test", "phase_to_test_for": "AIR"}, + "position_predicate": position, "output_state": {"id": "minecraft:lava"}}) + }; + let mut pool = json!({"elements": [{"weight": 1, "element": { + "element_type": "minecraft:single_pool_element", "processors": {"processors": [ + {"processor_type": "minecraft:rule", "rules": [rule( + json!({"predicate_type": "minecraft:always_true"}), + json!({"predicate_type": "repurposed_structures:y_value_pos_rule_test", + "min_y_value": 0, "max_y_value": 31}))]}, + {"processor_type": "minecraft:capped", "limit": 2, "delegate": { + "processor_type": "minecraft:rule", "rules": [rule( + json!({"predicate_type": "minecraft:block_match", "block": "minecraft:rail"}), + json!({"predicate_type": + "repurposed_structures:piece_origin_axis_aligned_linear_pos_rule_test", + "axis": "y", "min_dist": 0, "max_dist": 1}))]}}]}}}]}); + let mut carried = BTreeMap::new(); + assert!(rule_tests("template_pool", &mut pool, &mut carried)); + let list = &pool["elements"][0]["element"]["processors"]["processors"]; + let first = &list[0]["rules"][0]; + assert_eq!( + first["location_predicate"]["predicate_type"], + "horizon:matter_phase" + ); + assert_eq!(first["location_predicate"]["phase_to_test_for"], "AIR"); + assert_eq!( + first["position_predicate"]["predicate_type"], + "horizon:y_value" + ); + assert_eq!(first["position_predicate"]["max_y_value"], 31); + let delegated = &list[1]["delegate"]["rules"][0]; + assert_eq!( + delegated["position_predicate"]["predicate_type"], + "horizon:piece_origin_axis_aligned_linear_pos" + ); + assert_eq!( + delegated["input_predicate"]["predicate_type"], + "minecraft:block_match" + ); + assert_eq!(carried.values().sum::(), 4); + assert_eq!(carried["repurposed_structures:matter_phase_rule_test"], 2); + // A file holding none is left as it is. + assert!(!rule_tests("template_pool", &mut pool, &mut carried)); + assert!(!rule_tests( + "structure", + &mut json!({"predicate_type": + "repurposed_structures:y_value_pos_rule_test"}), + &mut carried + )); + } } diff --git a/crates/horizon-port-pack/src/worldgen.rs b/crates/horizon-port-pack/src/worldgen.rs index 7de9f39ce..507819566 100644 --- a/crates/horizon-port-pack/src/worldgen.rs +++ b/crates/horizon-port-pack/src/worldgen.rs @@ -131,6 +131,9 @@ pub struct Upgrade { /// Processors written in Java carried as data the game runs /// ([`crate::processors`]), by type. pub processors_carried: BTreeMap, + /// Rule tests written in Java carried as the game's own + /// ([`crate::processors::rule_tests`]), by type. + pub rule_tests_carried: BTreeMap, /// Entries added to other structure sets and template pools /// (`horizon:add_to`), by the set or pool they join. pub added_to: BTreeMap, @@ -345,10 +348,11 @@ pub fn upgrade( ) -> bool { let states = block_states(v); report.block_states += usize::from(states); + let renamed = crate::carry::renamed_ids(v); let dropped = crate::processors::each_list(kind, v, &mut |list| { drop_java_processors(list, &mut report.processors_dropped) }); - upgrade_types(kind, v, report, set_tags) || states || dropped + upgrade_types(kind, v, report, set_tags) || states || dropped || renamed } /// Takes the processors written in Java (any type outside `minecraft:` and diff --git a/docs/modpacks.md b/docs/modpacks.md index 12e522e24..0e8a0121f 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -455,6 +455,17 @@ Two cases the oracle fills in: Moog's were empty (763 spawners with a mob, up from 628); in the Nether the 52 empty ones in Moog's Nether Structures are filled (162, up from 110). Chests are unchanged: 2,777 and 1,405 with a loot table. + - **Rule tests** written in Java. Repurposed Structures' mineshafts and Nether monument test + blocks with three of the mod's own rule tests inside vanilla `rule` processors: what a + block is made of (solid, liquid, air, with rails and chains), a chance over the distance + from the template's origin along one axis, and a world height range. The game runs them + as `horizon:matter_phase`, `horizon:piece_origin_axis_aligned_linear_pos` and + `horizon:y_value` with the mod's fields, and carry renames them (213 in 75 lists). + - **Renamed ids.** 26.3 calls 1.21.1's chain the iron chain (its datafixers rename it in + worlds). Carry renames it in recipes, loot tables and worldgen files (112 files in 5 mods: + Repurposed's mineshafts, Better Dungeons' processors, Regions Unexplored's hanging sign + recipes...), which otherwise fail to load in 26.3. With both, the 5 Nether mineshaft pools load: 47 of + Repurposed's Nether mineshafts in the 128×128-chunk square (seed 1234), under the lava sea. - **Big templates** (#838). Vanilla reads structure templates with no limit on their tag count; Horizon now does too (`nbt::Limits::templates`). Seven of ATM10's 8,962 carried templates are past the old 1,000,000-tag cap: YUNG's monument start, four When Dungeons