diff --git a/patches/net/minecraft/world/inventory/SmithingMenu.java.patch b/patches/net/minecraft/world/inventory/SmithingMenu.java.patch new file mode 100644 index 00000000000..3dc44e06ed6 --- /dev/null +++ b/patches/net/minecraft/world/inventory/SmithingMenu.java.patch @@ -0,0 +1,10 @@ +--- a/net/minecraft/world/inventory/SmithingMenu.java ++++ b/net/minecraft/world/inventory/SmithingMenu.java +@@ -72,6 +_,7 @@ + protected void onTake(Player p_150663_, ItemStack p_150664_) { + p_150664_.onCraftedBy(p_150663_.level(), p_150663_, p_150664_.getCount()); + this.resultSlots.awardUsedRecipes(p_150663_, this.getRelevantItems()); ++ net.neoforged.neoforge.event.EventHooks.firePlayerSmithingEvent(p_150663_, this.inputSlots.getItem(0), this.inputSlots.getItem(1), this.inputSlots.getItem(2), p_150664_); + this.shrinkStackInSlot(0); + this.shrinkStackInSlot(1); + this.shrinkStackInSlot(2); diff --git a/src/main/java/net/neoforged/neoforge/event/EventHooks.java b/src/main/java/net/neoforged/neoforge/event/EventHooks.java index 7529a767d18..e27feef3a0f 100644 --- a/src/main/java/net/neoforged/neoforge/event/EventHooks.java +++ b/src/main/java/net/neoforged/neoforge/event/EventHooks.java @@ -904,12 +904,39 @@ public static void firePlayerRespawnEvent(ServerPlayer player, boolean fromEndFi NeoForge.EVENT_BUS.post(new PlayerEvent.PlayerRespawnEvent(player, fromEndFight)); } - public static void firePlayerCraftingEvent(Player player, ItemStack crafted, Container craftMatrix) { - NeoForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, crafted, craftMatrix)); + /** + * Called by {@link net.minecraft.world.inventory.ResultSlot#checkTakeAchievements} after the player takes an item from the result slot in the crafting grid. + * + * @param player The player who took the item. + * @param result The item that was crafted. + * @param craftMatrix The crafting matrix that was used for the recipe. + */ + public static void firePlayerCraftingEvent(Player player, ItemStack result, Container craftMatrix) { + NeoForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, result, craftMatrix)); } - public static void firePlayerSmeltedEvent(Player player, ItemStack smelted, int amountRemoved) { - NeoForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, smelted, amountRemoved)); + /** + * Called by {@link net.minecraft.world.inventory.FurnaceResultSlot#checkTakeAchievements} after the player takes an item from the furnace output slot. + * + * @param player The player who took the item. + * @param result The item that was smelted. + * @param amountRemoved The amount of items removed from the output slot. + */ + public static void firePlayerSmeltedEvent(Player player, ItemStack result, int amountRemoved) { + NeoForge.EVENT_BUS.post(new PlayerEvent.ItemSmeltedEvent(player, result, amountRemoved)); + } + + /** + * Called by {@link net.minecraft.world.inventory.SmithingMenu#onTake} after the player takes an item from the output slot in the smithing table. + * + * @param player The player who took the item. + * @param template The template item used in the smithing recipe. (ex. Smithing template) + * @param mainItem The main item used in the smithing recipe. (ex. Diamond sword) + * @param addition The addition item used in the smithing recipe. (ex. Netherite ingot) + * @param result The item that was smithed. + */ + public static void firePlayerSmithingEvent(Player player, ItemStack template, ItemStack mainItem, ItemStack addition, ItemStack result) { + NeoForge.EVENT_BUS.post(new PlayerEvent.ItemSmithingEvent(player, template, mainItem, addition, result)); } /** diff --git a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerEvent.java b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerEvent.java index 66c55569e4a..39d2389995e 100644 --- a/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerEvent.java +++ b/src/main/java/net/neoforged/neoforge/event/entity/player/PlayerEvent.java @@ -387,39 +387,130 @@ public String getPlayerUUID() { } } + /** + * An event triggered when a player crafts an item. + *

+ * This event is fired at the following stages: + *

+ *

+ * The event is fired via {@link EventHooks#firePlayerCraftingEvent(Player, ItemStack, Container)}
+ * and is posted to the {@link NeoForge#EVENT_BUS}. + */ public static class ItemCraftedEvent extends PlayerEvent { - private final ItemStack crafting; + private final ItemStack result; private final Container craftMatrix; - public ItemCraftedEvent(Player player, ItemStack crafting, Container craftMatrix) { + public ItemCraftedEvent(Player player, ItemStack result, Container craftMatrix) { super(player); - this.crafting = crafting; + this.result = result; this.craftMatrix = craftMatrix; } + /** + * {@return the item that was crafted (ex. Diamond sword)} + */ public ItemStack getCrafting() { - return this.crafting; + return this.result; } + /** + * {@return the crafting matrix used to craft the item (ex. 2x diamond, 1x stick)} + */ public Container getInventory() { return this.craftMatrix; } } + /** + * An event triggered when a player smiths an item. + *

+ * This event is fired at the following stages: + *

+ *

+ * The event is fired via {@link EventHooks#firePlayerSmithingEvent(Player, ItemStack, ItemStack, ItemStack, ItemStack)}
+ * and is posted to the {@link NeoForge#EVENT_BUS}. + */ + public static class ItemSmithingEvent extends PlayerEvent { + private final ItemStack template; + private final ItemStack mainItem; + private final ItemStack addition; + private final ItemStack result; + + public ItemSmithingEvent(Player player, ItemStack template, ItemStack mainItem, ItemStack addition, ItemStack result) { + super(player); + this.template = template; + this.mainItem = mainItem; + this.addition = addition; + this.result = result; + } + + /** + * {@return the template item used for smithing (ex. Smithing template)} + */ + public ItemStack getTemplate() { + return this.template; + } + + /** + * {@return the main item used for smithing (ex. Diamond sword)} + */ + public ItemStack getMainItem() { + return this.mainItem; + } + + /** + * {@return the item that is used as support to the item that is being smithed (ex. Netherite ingot} + */ + public ItemStack getAddition() { + return this.addition; + } + + /** + * {@return the result of the smithing in the final slot (ex. Netherite sword)} + */ + public ItemStack getResult() { + return this.result; + } + } + + /** + * An event triggered when a player smelts an item. + *

+ * This event is fired after: + *

+ *

+ * The event is fired via {@link EventHooks#firePlayerSmeltedEvent(Player, ItemStack, int)}
+ * and is posted to the {@link NeoForge#EVENT_BUS}. + */ public static class ItemSmeltedEvent extends PlayerEvent { - private final ItemStack smelting; + private final ItemStack result; private final int amountRemoved; - public ItemSmeltedEvent(Player player, ItemStack crafting, int amountRemoved) { + public ItemSmeltedEvent(Player player, ItemStack result, int amountRemoved) { super(player); - this.smelting = crafting; + this.result = result; this.amountRemoved = amountRemoved; } + /** + * {@return the item result after smelting (ex. Iron ingot)} + */ public ItemStack getSmelting() { - return this.smelting; + return this.result; } + /** + * {@return the amount of items that were removed from the inventory (ex. 1)} + */ public int getAmountRemoved() { return this.amountRemoved; }