diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index de987df2c..90834c2c6 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -114,6 +114,52 @@ pub fn template(name: &str, properties: &BTreeSet) -> Option<&'static st .map(|(_, t)| t) } +/// [`template`] for a blockstate file. A wall's sides are `none`, `low` or +/// `tall`; sides and `up` that are `true` or `false` are a vine's (Chipped's +/// vines), which the names alone take for a wall. +pub fn block_template(name: &str, blockstate: &Value) -> Option<&'static str> { + let t = template(name, &properties(blockstate))?; + let boolean = ["north", "east", "south", "west"] + .iter() + .flat_map(|side| values(blockstate, side)) + .any(|v| v == "true" || v == "false"); + Some(if t == "minecraft:cobblestone_wall" && boolean { + "minecraft:vine" + } else { + t + }) +} + +/// The values a blockstate file tests a property for. +fn values(blockstate: &Value, property: &str) -> BTreeSet { + let mut out = BTreeSet::new(); + for key in blockstate["variants"] + .as_object() + .into_iter() + .flatten() + .map(|(k, _)| k) + { + for pair in key.split(',') { + if let Some((k, v)) = pair.split_once('=') + && k == property + { + out.insert(v.to_owned()); + } + } + } + for part in blockstate["multipart"].as_array().into_iter().flatten() { + let when = &part["when"]; + let mut conditions: Vec<&Value> = vec![when]; + for or in ["OR", "AND"] { + conditions.extend(when[or].as_array().into_iter().flatten()); + } + for v in conditions.iter().filter_map(|c| c[property].as_str()) { + out.extend(v.trim_start_matches('!').split('|').map(str::to_owned)); + } + } + out +} + /// The property names a blockstate file uses. pub fn properties(blockstate: &Value) -> BTreeSet { let mut out = BTreeSet::new(); @@ -892,7 +938,7 @@ pub fn carry( } } else { for (b, state) in &blockstates { - match template(b, &properties(state)) { + match block_template(b, state) { Some(t) => { write_decl("block", b, &json!({"template": t}))?; report.blocks.insert(b.clone(), t.to_owned()); @@ -1114,6 +1160,18 @@ mod tests { template("gem_block", &properties(&json!({"variants": {"": {}}}))), Some("minecraft:stone") ); + // Walls and vines have the same sides and up; their values tell them apart. + let wall = json!({"multipart": [{"when": {"up": "true"}}, + {"when": {"north": "low"}}, {"when": {"east": "tall"}}, + {"when": {"south": "low"}}, {"when": {"west": "low"}}]}); + assert_eq!( + block_template("gem_wall", &wall), + Some("minecraft:cobblestone_wall") + ); + let vine = json!({"multipart": [{"when": {"up": "true"}}, + {"when": {"up": "false", "north": "false", "east": "false", "south": "false", + "west": "false"}}, {"when": {"north": "true"}}]}); + assert_eq!(block_template("gem_vine", &vine), Some("minecraft:vine")); let odd = json!({"variants": {"charge=1,mode=fast": {}}}); assert_eq!(template("gem_core", &properties(&odd)), None); } diff --git a/docs/modpacks.md b/docs/modpacks.md index 86083be5a..bc6215f2b 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -343,7 +343,9 @@ same states (Macaw's garage doors, multi-part gates). Recipes get the convention (`c:dyes/red`), filled from the oracle with vanilla's and the mod's own members. Without the oracle, templates are guessed from the blockstate's properties: the template whose properties include them with the fewest left over (models often ignore `powered` or `waterlogged`), the -block's name breaking ties (`_pane`, `_gate`, `_trapdoor`). +block's name breaking ties (`_pane`, `_gate`, `_trapdoor`). Walls and vines have the same +property names; sides tested as `true` / `false` make a vine (Chipped's 11 vines, which the game +refused as walls: their blockstates did not load). ### 1. Import