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.

Fire the item crafted hook when a crafting result is taken

MSedgeMC234a17bd63cb
diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs
index dde193489..0d8b6f329 100644
--- a/crates/horizon-client/src/game.rs
+++ b/crates/horizon-client/src/game.rs
@@ -10372,6 +10372,7 @@ impl Game<'_> {
let mut crafted = 0;
while let Some((recipe, result)) = self.craft_recipe() {
let made = (result.item.clone(), result.count);
+ let taken = result.clone();
let cells = self.craft_size * self.craft_size;
let ingredients = self.craft_grid[..cells].to_vec();
let remainders = world_recipes!(self).remainders(
@@ -10412,6 +10413,10 @@ impl Game<'_> {
}
}
crafted += 1;
+ // `ResultSlot.onTake`: the recipe and the stack taken
+ // (native_item_crafted).
+ let player = self.native_acting_id();
+ self.native_item_crafted(player, &format!("minecraft:{recipe}"), &taken);
self.stats.add("crafted", &made.0, made.1 as i32);
self.award_crafted(&[(format!("minecraft:{recipe}"), ingredients)]);
if !shift || crafted >= 64 {
diff --git a/crates/horizon-client/src/game/lan_crafting.rs b/crates/horizon-client/src/game/lan_crafting.rs
index 8a867abba..55e5943df 100644
--- a/crates/horizon-client/src/game/lan_crafting.rs
+++ b/crates/horizon-client/src/game/lan_crafting.rs
@@ -130,6 +130,7 @@ impl Game<'_> {
let equipped = std::mem::take(&mut menu.equipped);
let brewed = std::mem::take(&mut menu.brewed);
let crafted = std::mem::take(&mut menu.crafted);
+ let item_crafted = std::mem::take(&mut menu.item_crafted);
menu.drops.clear();
store_player(menu, &mut host.hub.guests[gi]);
if !brewed.is_empty() {
@@ -148,6 +149,15 @@ impl Game<'_> {
}
});
}
+ if !item_crafted.is_empty() {
+ let conn = host.hub.guests[gi].conn;
+ self.as_guest(host, conn, |g| {
+ let player = g.native_acting_id();
+ for (recipe, stack) in &item_crafted {
+ g.native_item_crafted(player, recipe, stack);
+ }
+ });
+ }
if !equipped.is_empty() {
let conn = host.hub.guests[gi].conn;
self.as_guest(host, conn, |g| {
diff --git a/crates/horizon-client/src/game/native_hooks.rs b/crates/horizon-client/src/game/native_hooks.rs
index 14b884c4a..3f085f6f1 100644
--- a/crates/horizon-client/src/game/native_hooks.rs
+++ b/crates/horizon-client/src/game/native_hooks.rs
@@ -3503,6 +3503,17 @@ impl Game<'_> {
) {
let _ = (player, item, count, canceled);
}
+ /// A player took a crafting grid's result (`ResultSlot.onTake`, NeoForge's
+ /// `PlayerEvent.ItemCraftedEvent`): `player` is the player's uuid, `recipe`
+ /// the recipe the grid matched (`minecraft:oak_planks`) and `stack` the
+ /// result it took, with its components. It only hears: the take has
+ /// already happened. It fires for the crafting grid of an inventory or a
+ /// crafting table, on the integrated server and a dedicated server, guests
+ /// included, and not for a stonecutter's, a smithing table's or a
+ /// furnace's result. Server-side. API @API:item_crafted@.
+ pub(super) fn native_item_crafted(&mut self, player: u128, recipe: &str, stack: &ItemStack) {
+ let _ = (player, recipe, stack);
+ }
/// Whether a mod hears `native_experience_orb_pickup`: set `listened`
/// (a HEAD injection), or the server skips swapping the player in for it,
/// and with this empty the pickup costs nothing more without mods.
diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs
index d0f716aec..4225d9f92 100644
--- a/crates/horizon-client/src/menu.rs
+++ b/crates/horizon-client/src/menu.rs
@@ -297,6 +297,9 @@ pub struct Menu {
/// Recipes whose result was taken (`awardUsedRecipes`) and the stacks that
/// went into each, for the caller.
pub crafted: Vec<(String, Vec<Option<ItemStack>>)>,
+ /// A crafting grid's result taken (`ResultSlot.onTake`): the recipe and
+ /// the stack taken, for the caller (`native_item_crafted`).
+ pub item_crafted: Vec<(String, ItemStack)>,
/// Potions taken from a brewing stand's bottle slots
/// (`PotionSlot.onTake`), for the caller.
pub brewed: Vec<ItemStack>,
@@ -729,6 +732,7 @@ impl Menu {
sounds: Vec::new(),
player_sounds: Vec::new(),
crafted: Vec::new(),
+ item_crafted: Vec::new(),
brewed: Vec::new(),
equipped: Vec::new(),
merchant: None,
@@ -1732,6 +1736,8 @@ impl Menu {
let grid = self.containers[GRID].clone();
if let Some((id, _)) = env.recipes.find(&grid, n) {
self.crafted.push((format!("minecraft:{id}"), grid.clone()));
+ self.item_crafted
+ .push((format!("minecraft:{id}"), taken.clone()));
}
let remaining = env
.recipes
@@ -3292,6 +3298,25 @@ pub(crate) mod tests {
});
}
+ /// A crafting grid's result taken is recorded for `native_item_crafted`:
+ /// the recipe the grid matched and the stack the player took.
+ #[test]
+ fn taking_a_crafting_result_records_the_crafted_stack() {
+ with_menu(Kind::Crafting, |env, menu| {
+ menu.creative = false;
+ menu.set_slot(env, 1, stack("oak_log", 1));
+ menu.settle(env);
+ menu.click(env, 0, 0, Input::Pickup).unwrap();
+ assert_eq!(
+ menu.item_crafted,
+ vec![(
+ "minecraft:oak_planks".to_owned(),
+ stack("oak_planks", 4).unwrap()
+ )]
+ );
+ });
+ }
+
#[test]
fn quick_move_landing_routes_plain_containers() {
for (kind, size) in [
diff --git a/tools/native-hook-fragments/item_crafted.json b/tools/native-hook-fragments/item_crafted.json
new file mode 100644
index 000000000..e570cebac
--- /dev/null
+++ b/tools/native-hook-fragments/item_crafted.json
@@ -0,0 +1,13 @@
+{
+ "hooks": [
+ {
+ "name": "item_crafted",
+ "target": "horizon_client::game::native_hooks::Game<'_>::native_item_crafted",
+ "neoforge": "PlayerEvent.ItemCraftedEvent",
+ "side": "server"
+ }
+ ],
+ "rust_summary": "a player taking a crafting grid's result (`native_item_crafted`).",
+ "doc_summary": "a player took a crafting grid's result, through `native_item_crafted` (NeoForge's `PlayerEvent.ItemCraftedEvent`): the player's uuid, the recipe the grid matched (`minecraft:oak_planks`) and the result stack it took, with its components. It only hears; the take has already happened. It fires for the crafting grid of an inventory or a crafting table, on the integrated server and on a dedicated server, guests included, and not for a stonecutter's, a smithing table's or a furnace's result. Bundles that use it need \"hooks\": @API:item_crafted@.",
+ "comments": []
+}