port-pack carry: leave out recipes for items it didn't declare

A recipe whose result is one of the mod's own items that the carry didn't declare
(its block had no vanilla template) loaded and crafted air. Vanilla refuses a
recipe for an unknown item; the carry now moves such recipes to outside/ and lists
them (recipes_unregistered), and check says "N recipes for left-out blocks".
Found by the mod browser's end-to-end run on Macaw's Fences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-22 17:57:24 +02:00
parent da996b9fd1
commit 4a350bb0a0
2 changed files with 135 additions and 1 deletions
+89
View File
@@ -37,6 +37,10 @@ pub struct Report {
pub worldgen: usize,
/// Recipes left as they were: custom types, NeoForge conditions.
pub recipes_kept: Vec<String>,
/// Recipes whose result is one of the mod's own items that isn't declared (its block was
/// left out), moved to `outside/`: vanilla refuses a recipe for an unknown item, and the
/// game would craft air.
pub recipes_unregistered: Vec<String>,
pub item_definitions_added: usize,
/// Templates and properties came from the NeoForge oracle's facts, not
/// from guesses.
@@ -1083,6 +1087,9 @@ pub fn carry(
report.items += 1;
}
}
let owned: Vec<String> = owned.iter().map(|n| n.to_string()).collect();
let owned: Vec<&str> = owned.iter().map(String::as_str).collect();
unregistered_recipes(&inside, &outside, id, &owned, &mut report)?;
let q = |s: &str| toml::Value::String(s.to_owned()).to_string();
let name = if info.name.is_empty() { id } else { &info.name };
let mut manifest = format!(
@@ -1109,6 +1116,88 @@ pub fn carry(
Ok(report)
}
/// The item a recipe makes: `result.id` (1.21 and later), `result.item` or a bare id.
fn recipe_result(recipe: &Value) -> Option<&str> {
let r = &recipe["result"];
r.as_str()
.or_else(|| r["id"].as_str())
.or_else(|| r["item"].as_str())
}
/// Moves the recipes whose result is an undeclared item of the mod's own namespaces from
/// `inside` to `outside` ([`Report::recipes_unregistered`]). The declared items are the blocks
/// with their item and the items under `data/<id>/horizon/`.
fn unregistered_recipes(
inside: &Path,
outside: &Path,
id: &str,
owned: &[&str],
report: &mut Report,
) -> Result<(), String> {
let decl = inside.join(format!("data/{id}/horizon"));
let mut declared = BTreeSet::new();
for kind in ["block", "item"] {
for f in fs::read_dir(decl.join(kind))
.into_iter()
.flatten()
.flatten()
{
let path = f.path();
let Some(name) = path.file_stem().and_then(|n| n.to_str()) else {
continue;
};
let has_item = kind == "item"
|| fs::read(&path)
.ok()
.and_then(|b| serde_json::from_slice::<Value>(&b).ok())
.is_none_or(|v| v["item"] != json!(false));
if has_item {
declared.insert(format!("{id}:{name}"));
}
}
}
for ns in owned {
let dir = inside.join(format!("data/{ns}/recipe"));
let mut stack = vec![dir.clone()];
while let Some(d) = stack.pop() {
for f in fs::read_dir(&d).into_iter().flatten().flatten() {
let path = f.path();
if path.is_dir() {
stack.push(path);
continue;
}
let Some(recipe) = fs::read(&path)
.ok()
.and_then(|b| serde_json::from_slice::<Value>(&b).ok())
else {
continue;
};
let Some(result) = recipe_result(&recipe) else {
continue;
};
let unknown = result
.split_once(':')
.is_some_and(|(n, _)| owned.contains(&n) && !declared.contains(result));
if !unknown {
continue;
}
let rel = path.strip_prefix(inside).map_err(|e| e.to_string())?;
let to = outside.join(rel);
if let Some(parent) = to.parent() {
fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?;
}
fs::rename(&path, &to).map_err(|e| format!("{}: {e}", to.display()))?;
report.copied = report.copied.saturating_sub(1);
report
.recipes_unregistered
.push(rel.to_string_lossy().into_owned());
}
}
}
report.recipes_unregistered.sort();
Ok(())
}
/// The tags a recipe's ingredients name (`#ns:path` or `{"tag": ...}`).
fn tags_used(v: &Value, out: &mut BTreeSet<String>) {
match v {
+46 -1
View File
@@ -156,7 +156,9 @@ pub fn check(jar_path: &Path, out: &Path) -> Result<Check, String> {
.chain(report.namespaces.iter().cloned())
.collect();
let c = &info.census;
let recipes = report.recipes_upgraded;
let recipes = report
.recipes_upgraded
.saturating_sub(report.recipes_unregistered.len());
let loot = c.loot_tables + report.loot_tables_added;
let content =
report.blocks.len() + report.items + recipes + loot + report.worldgen + c.structures;
@@ -185,6 +187,13 @@ pub fn check(jar_path: &Path, out: &Path) -> Result<Check, String> {
"blocks with no vanilla block to copy (left out)",
));
}
if !report.recipes_unregistered.is_empty() {
skipped.push(plural(
report.recipes_unregistered.len(),
"recipe for a left-out block",
"recipes for left-out blocks",
));
}
if !report.recipes_kept.is_empty() {
skipped.push(plural(
report.recipes_kept.len(),
@@ -331,6 +340,42 @@ displayName = "Ruby"
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn recipes_for_left_out_blocks_are_left_out_too() {
let dir = temp("left-out");
let odd = br#"{"variants": {"wobble=1": {"model": "ruby:block/odd"}}}"#;
let plain = br#"{"variants": {"": {"model": "ruby:block/ruby_block"}}}"#;
let recipe = |result: &str| {
format!(
r#"{{"type": "minecraft:crafting_shapeless", "ingredients": ["minecraft:red_dye"], "result": {{"id": "{result}"}}}}"#
)
};
let (odd_recipe, block_recipe) = (recipe("ruby:odd"), recipe("ruby:ruby_block"));
let files = [
("META-INF/neoforge.mods.toml", TOML),
("assets/ruby/blockstates/odd.json", odd.as_slice()),
("assets/ruby/blockstates/ruby_block.json", plain.as_slice()),
("data/ruby/recipe/odd.json", odd_recipe.as_bytes()),
("data/ruby/recipe/ruby_block.json", block_recipe.as_bytes()),
];
let c = check(&jar(&dir, &files), &dir.join("out")).unwrap();
assert_eq!(c.verdict, Verdict::Partly);
assert!(c.ports.contains(&"1 recipe".to_string()), "{:?}", c.ports);
assert!(
c.skipped
.contains(&"1 recipe for a left-out block".to_string()),
"{:?}",
c.skipped
);
assert!(!dir.join("out/carry/data/ruby/recipe/odd.json").exists());
assert!(dir.join("out/outside/data/ruby/recipe/odd.json").exists());
assert!(
dir.join("out/carry/data/ruby/recipe/ruby_block.json")
.exists()
);
fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn code_only_and_old_layouts_dont_run() {
let dir = temp("code");