Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Native mods: a recipe ingredient of a mod's own kind

huidf3a7ffd7315
diff --git a/crates/horizon-client/src/crafting.rs b/crates/horizon-client/src/crafting.rs
index 808c08613..cbf2247dc 100644
--- a/crates/horizon-client/src/crafting.rs
+++ b/crates/horizon-client/src/crafting.rs
@@ -19,6 +19,30 @@ use std::{
sync::Arc,
};
+/// A recipe ingredient of a mod's own kind: the entry named a type in a
+/// namespace outside `minecraft:`, and the loader kept it for this call.
+/// The kind and the JSON the file wrote are here; fill `items` with the
+/// item ids the ingredient matches (item ids only: components are not
+/// checked here; a match-time hook follows separately). Duplicates are
+/// removed keeping first-seen order. The hook may be called more than
+/// once per recipe; the answer must be the same. An unclaimed kind
+/// leaves the list empty and the recipe fails, as an unknown kind does.
+/// API @API:ingredient@.
+#[inline(never)]
+pub fn native_ingredient(
+ kind: &str,
+ data: &serde_json::Map<String, Value>,
+ items: &mut Vec<String>,
+) {
+ let _ = (kind, data, items);
+}
+
+/// Keep first-seen order, drop the rest.
+fn dedup_keep_order(items: &mut Vec<String>) {
+ let mut seen = std::collections::HashSet::new();
+ items.retain(|i| seen.insert(i.clone()));
+}
+
/// A set of accepted items (expanded from an item, list or tag), and the
/// items in its `HolderSet` order (`Ingredient.display`'s `TagSlotDisplay`).
#[derive(Clone, Debug)]
@@ -107,6 +131,14 @@ impl Ingredient {
.cloned()
.collect()
}
+ // A mod's own kind: the hook answers with the item list it
+ // matches; a bare type name is minecraft's own, not a mod's.
+ k if k.contains(':') && !k.starts_with("minecraft:") => {
+ let mut list = Vec::new();
+ native_ingredient(k, o, &mut list);
+ dedup_keep_order(&mut list);
+ return (!list.is_empty()).then(|| Self::of(list));
+ }
_ => return None,
};
(!kept.is_empty()).then(|| Self::of(kept))
@@ -1913,6 +1945,23 @@ mod tests {
/// NeoForge's standard ingredient shapes resolve to their flat item
/// lists; a kind the loader doesn't know still fails the file.
+ #[test]
+ fn an_unclaimed_ingredient_kind_fails_and_dedup_keeps_order() {
+ let items = Items::for_tests(HashMap::new());
+ let json = |s: &str| serde_json::from_str::<Value>(s).unwrap();
+ assert!(
+ Ingredient::parse(&json(r#"{"type":"mymod:aspect","aspect":"fire"}"#), &items,)
+ .is_none()
+ );
+ assert!(
+ Ingredient::parse(&json(r#"{"type":"minecraft:future_shape"}"#), &items,).is_none()
+ );
+ assert!(Ingredient::parse(&json(r#"{"type":"aspect"}"#), &items,).is_none());
+ let mut list = vec!["b".to_string(), "a".to_string(), "b".to_string()];
+ dedup_keep_order(&mut list);
+ assert_eq!(list, vec!["b".to_string(), "a".to_string()]);
+ }
+
#[test]
fn neoforge_ingredient_shapes_parse() {
let mut tags = HashMap::new();
diff --git a/crates/horizon-client/src/game/native_hooks.rs b/crates/horizon-client/src/game/native_hooks.rs
index 663f50403..7d5294677 100644
--- a/crates/horizon-client/src/game/native_hooks.rs
+++ b/crates/horizon-client/src/game/native_hooks.rs
@@ -6357,6 +6357,23 @@ mod tests {
}
}
+ #[test]
+ fn native_ingredient_stays_hookable() {
+ let source = include_str!("../crafting.rs");
+ let at = source.find("pub fn native_ingredient(").unwrap();
+ let start = source[..at].rfind("///").unwrap();
+ let body = &source[start..start + source[start..].find("\n}\n").unwrap()];
+ for line in body.lines() {
+ assert!(
+ !line.contains("cfg"),
+ "a cfg in the ingredient hook: {line}"
+ );
+ for mac in ["!(", "![", "! {", "!{"] {
+ assert!(!line.contains(mac), "a macro: {line}");
+ }
+ }
+ }
+
#[test]
fn loot_table_load_stays_hookable() {
let source = include_str!("../../../horizon-world/src/loot.rs").replace("\r\n", "\n");
@@ -6459,6 +6476,8 @@ mod tests {
let menu_hooks = free_hooks(include_str!("../menu.rs"));
// The air supply's (`horizon_client::breathing`).
let breath_hooks = free_hooks(include_str!("../breathing.rs"));
+ // The crafting loaders' (`horizon_client_lib::crafting`).
+ let crafting_hooks = free_hooks(include_str!("../crafting.rs"));
let declared: BTreeSet<_> = section
.lines()
.chain(level_section.lines())
@@ -6473,6 +6492,7 @@ mod tests {
.chain(loot_hooks.iter().copied())
.chain(menu_hooks.iter().copied())
.chain(breath_hooks.iter().copied())
+ .chain(crafting_hooks.iter().copied())
.collect();
let mut names = BTreeSet::new();
let mut contracts = BTreeSet::new();
@@ -6490,6 +6510,10 @@ mod tests {
format!("horizon_world::item_defaults::native_{name}")
} else if loot_hooks.contains(name) {
format!("horizon_world::loot::native_{name}")
+ } else if crafting_hooks.contains(name) {
+ // crafting.rs compiles into horizon-client-lib (#5446's
+ // split): horizon-mixins resolves the crate by its package.
+ format!("horizon_client_lib::crafting::native_{name}")
} else if menu_hooks.contains(name) {
format!("horizon_client::menu::native_{name}")
} else if breath_hooks.contains(name) {
diff --git a/docs/native-mods.md b/docs/native-mods.md
index ed4446a93..73bb463a1 100644
--- a/docs/native-mods.md
+++ b/docs/native-mods.md
@@ -436,6 +436,15 @@ position), `block_entity` (the broken block's entity as it was before the break,
vanilla's `time_check` and `weather_check`). The game sets it for a block a player or an explosion breaks and for a
mob's death loot; on other rolls `place` is `None`.
+### A mod's own ingredient kinds
+
+A recipe's ingredient that names a type in a namespace outside `minecraft:` reaches
+`native_ingredient(kind, data, items)` at load time. Fill `items` with the item ids the
+ingredient matches (item ids only: components are not checked; a match-time hook follows
+separately). Duplicates are removed keeping first-seen order. The hook may be called more
+than once per recipe; the answer must be the same. An unclaimed kind leaves the list empty
+and the recipe fails, as an unknown kind does.
+
### Data files
A bundle's `data/` folder (any files) is staged as `native-data/<mod>/` at the root of the workspace. A helper or
diff --git a/tools/native-hook-fragments/ingredient.json b/tools/native-hook-fragments/ingredient.json
new file mode 100644
index 000000000..d2a2f7c36
--- /dev/null
+++ b/tools/native-hook-fragments/ingredient.json
@@ -0,0 +1,6 @@
+{
+ "hooks": [{"name": "ingredient", "target": "horizon_client_lib::crafting::native_ingredient"}],
+ "rust_summary": "a recipe ingredient of a mod's own kind\n(`native_ingredient`, which the unknown ingredient type in a namespace of\nown reaches with the kind, its JSON and the item list to fill).",
+ "doc_summary": "a recipe ingredient of a mod's own kind. The ingredient entry that names a type in a namespace outside minecraft: reaches native_ingredient(kind, data, items) at load time. Fill items with the item ids the ingredient matches (item ids only: components are not checked; a match-time hook follows separately). Duplicates are removed keeping first-seen order. An unclaimed kind leaves the list empty and the recipe fails. The hook may be called more than once per recipe; the answer must be the same. Bundles that use it need \"hooks\": @API:ingredient@.",
+ "comments": ["crates/horizon-client/src/crafting.rs"]
+}