Merge pull request #2212 from ludovic111/fix/furnace-recipes-used-namespace

🤖 Agent: Merging. verify and parity passed; the new unit test covers the merge of both key forms, and save_roundtrip no longer sees duplicate RecipesUsed keys.
This commit is contained in:
Ludovic Marie
2026-10-26 15:20:16 +01:00
committed by GitHub
3 changed files with 45 additions and 3 deletions
+15 -1
View File
@@ -49,6 +49,17 @@ pub struct Furnace {
/// rather than per item, and a save keeps it as the `RecipesUsed` compound.
pub recipes_used: HashMap<String, u32>,
}
/// The key `recipes_used` keeps a recipe under: its full id, as vanilla's `ResourceKey<Recipe>`
/// writes it in `RecipesUsed`. Horizon's own recipe ids are file names without the namespace, so
/// keeping both forms would write a key twice, and vanilla refuses such a furnace
/// ("Duplicate entry for key").
pub fn recipe_used_key(id: &str) -> String {
if id.contains(':') {
id.to_owned()
} else {
format!("minecraft:{id}")
}
}
/// A hopper's slots: the only plain container with five.
pub const HOPPER_SLOTS: usize = 5;
@@ -638,7 +649,10 @@ impl Furnace {
Some(out) => out.count += recipe.result.count,
None => self.items[2] = Some(recipe.result.clone()),
}
*self.recipes_used.entry(recipe.id.clone()).or_insert(0) += 1;
*self
.recipes_used
.entry(recipe_used_key(&recipe.id))
.or_insert(0) += 1;
if let Some(i) = self.items[0].as_mut() {
i.count -= 1;
if i.count == 0 {
+2
View File
@@ -1612,7 +1612,9 @@ impl Recipes {
}
/// The `experience` of the cooking recipe with this file name, and 0 for one this data
/// pack does not have (a save from another pack can name it).
/// `id` with or without the `minecraft:` namespace (a furnace keeps the full id).
pub fn cooking_experience(&self, id: &str) -> f32 {
let id = id.strip_prefix("minecraft:").unwrap_or(id);
self.cooking
.iter()
.find(|c| c.id == id)
+28 -2
View File
@@ -676,8 +676,10 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain
Tag::Int(n) => *n,
_ => continue,
};
f.recipes_used
.insert(id.to_string_lossy(), count.max(0) as u32);
// Saves from before the key was namespaced hold both forms of an id.
*f.recipes_used
.entry(crate::containers::recipe_used_key(&id.to_string_lossy()))
.or_insert(0) += count.max(0) as u32;
}
}
}
@@ -4382,4 +4384,28 @@ mod tests {
};
assert_eq!(back.recipes_used.get("minecraft:cooked_porkchop"), Some(&3));
}
/// A save written before the key was namespaced holds an id twice, once bare: vanilla
/// refuses such a furnace, so a load merges them under the full id.
#[test]
fn a_furnaces_recipes_used_merges_bare_and_full_ids() {
use crate::containers::BlockEntity;
let Some(BlockEntity::Furnace(mut f)) = BlockEntity::for_block("minecraft:furnace") else {
panic!("a furnace");
};
f.recipes_used
.insert("iron_ingot_from_smelting_iron_ore".to_owned(), 2);
f.recipes_used
.insert("minecraft:iron_ingot_from_smelting_iron_ore".to_owned(), 3);
let tag = encode_block_entity((1, 64, 2), &BlockEntity::Furnace(f), "minecraft:furnace");
let Some((_, BlockEntity::Furnace(back))) = decode_block_entity(&tag) else {
panic!("a furnace back");
};
assert_eq!(back.recipes_used.len(), 1);
assert_eq!(
back.recipes_used
.get("minecraft:iron_ingot_from_smelting_iron_ore"),
Some(&5)
);
}
}