Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PlayerEvent.ItemSmithingEvent #1693

Open
wants to merge 12 commits into
base: 1.21.x
Choose a base branch
from
10 changes: 10 additions & 0 deletions patches/net/minecraft/world/inventory/SmithingMenu.java.patch
Original file line number Diff line number Diff line change
@@ -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);
35 changes: 31 additions & 4 deletions src/main/java/net/neoforged/neoforge/event/EventHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,39 +387,130 @@ public String getPlayerUUID() {
}
}

/**
* An event triggered when a player crafts an item.
* <p>
* This event is fired at the following stages:
* <ul>
* <li>After clicking the craft button in the crafting interface.</li>
* <li>Before awarding the used recipe and resizing the stacks.</li>
* </ul>
* <p>
* The event is fired via {@link EventHooks#firePlayerCraftingEvent(Player, ItemStack, Container)}<br>
* 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() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Method name, and the same method from the ItemSmeltedEvent are misleading.

I recommend renaming them to getResult or getCrafted(for the ItemSmeltedEvent, getSmelted) since they return the result, and not the matrix/item that is being smelted.

Since this is a Breaking Change i was requested in the Discord to make a comment about it

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.
* <p>
* This event is fired at the following stages:
* <ul>
* <li>After awarding the used recipe.</li>
* <li>Before resizing the stacks.</li>
* </ul>
* <p>
* The event is fired via {@link EventHooks#firePlayerSmithingEvent(Player, ItemStack, ItemStack, ItemStack, ItemStack)} <br>
* and is posted to the {@link NeoForge#EVENT_BUS}.
*/
public static class ItemSmithingEvent extends PlayerEvent {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see some documentation on this class. Most of our events should have a javadoc explaining:

  • What the event's purpose is (e.g. "An event for when a player smiths an item")
  • When the event is fired (e.g. "After awarding used recipes, but before resizing the stacks")
  • Where the event is fired (e.g. on the logical server, client, both)
  • Which bus it's fired on (e.g. the game bus)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, i will see to this asap

Copy link
Author

@MeAlam1 MeAlam1 Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been completely resolved!

I will not close this conversation since i do not want to hide it before it has been confirmed by a Maintainer

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.
* <p>
* This event is fired after:
* <ul>
* <li>Awarding the used recipe.</li>
* <li>Resizing the item stacks.</li>
* </ul>
* <p>
* The event is fired via {@link EventHooks#firePlayerSmeltedEvent(Player, ItemStack, int)} <br>
* 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;
}
Expand Down
Loading