diff --git a/src/main/java/appeng/crafting/CraftingEvent.java b/src/main/java/appeng/crafting/CraftingEvent.java index 46ef1cb3e3f..879cdf416e0 100644 --- a/src/main/java/appeng/crafting/CraftingEvent.java +++ b/src/main/java/appeng/crafting/CraftingEvent.java @@ -20,7 +20,6 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.world.Container; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.neoforged.neoforge.common.NeoForge; @@ -31,12 +30,6 @@ public class CraftingEvent { - public static void fireCraftingEvent(Player player, - ItemStack craftedItem, - Container container) { - NeoForge.EVENT_BUS.post(new PlayerEvent.ItemCraftedEvent(player, craftedItem, container)); - } - public static void fireAutoCraftingEvent(Level level, // NOTE: We want to be able to include the recipe in the event later @SuppressWarnings("unused") IPatternDetails pattern, diff --git a/src/main/java/appeng/menu/slot/AppEngCraftingSlot.java b/src/main/java/appeng/menu/slot/AppEngCraftingSlot.java index 97238b9547e..e7bcf935957 100644 --- a/src/main/java/appeng/menu/slot/AppEngCraftingSlot.java +++ b/src/main/java/appeng/menu/slot/AppEngCraftingSlot.java @@ -18,21 +18,26 @@ package appeng.menu.slot; +import com.google.common.collect.Lists; + +import org.jetbrains.annotations.Nullable; + import net.minecraft.core.NonNullList; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.CraftingContainer; +import net.minecraft.world.inventory.RecipeCraftingHolder; import net.minecraft.world.inventory.TransientCraftingContainer; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.item.crafting.RecipeType; import net.minecraft.world.level.Level; import net.neoforged.neoforge.common.CommonHooks; import appeng.api.inventories.InternalInventory; -import appeng.crafting.CraftingEvent; import appeng.helpers.Inventories; import appeng.util.inv.AppEngInternalInventory; -public class AppEngCraftingSlot extends AppEngSlot { +public class AppEngCraftingSlot extends AppEngSlot implements RecipeCraftingHolder { /** * The craft matrix inventory linked to this result slot. @@ -49,6 +54,9 @@ public class AppEngCraftingSlot extends AppEngSlot { */ private int amountCrafted; + @Nullable + private RecipeHolder recipeUsed; + public AppEngCraftingSlot(Player player, InternalInventory craftingGrid) { super(new AppEngInternalInventory(1), 0); this.player = player; @@ -65,33 +73,42 @@ public boolean mayPlace(ItemStack stack) { * internal count then calls onCrafting(item). */ @Override - protected void onQuickCraft(ItemStack par1ItemStack, int par2) { - this.amountCrafted += par2; - this.checkTakeAchievements(par1ItemStack); + protected void onQuickCraft(ItemStack stack, int amount) { + this.amountCrafted += amount; + this.checkTakeAchievements(stack); } /** - * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. + * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. This replicates Vanilla + * {@link net.minecraft.world.inventory.ResultSlot}. */ @Override - protected void checkTakeAchievements(ItemStack par1ItemStack) { - par1ItemStack.onCraftedBy(this.player.level(), this.player, this.amountCrafted); + protected void checkTakeAchievements(ItemStack stack) { + var craftContainer = craftingGrid.toContainer(); + + if (amountCrafted > 0) { + stack.onCraftedBy(this.player.level(), this.player, this.amountCrafted); + net.neoforged.neoforge.event.EventHooks.firePlayerCraftingEvent(this.player, stack, craftContainer); + } + + var ingredients = Lists.newArrayList(craftingGrid); + awardUsedRecipes(this.player, ingredients); + this.amountCrafted = 0; } @Override - public void onTake(Player playerIn, ItemStack stack) { - CraftingEvent.fireCraftingEvent(playerIn, stack, this.craftingGrid.toContainer()); + public void onTake(Player player, ItemStack stack) { this.amountCrafted += stack.getCount(); this.checkTakeAchievements(stack); - CommonHooks.setCraftingPlayer(playerIn); + CommonHooks.setCraftingPlayer(player); final CraftingContainer ic = new TransientCraftingContainer(this.getMenu(), 3, 3); for (int x = 0; x < this.craftingGrid.size(); x++) { ic.setItem(x, this.craftingGrid.getStackInSlot(x)); } - var aitemstack = this.getRemainingItems(ic, playerIn.level()); + var aitemstack = this.getRemainingItems(ic, player.level()); Inventories.copy(ic, this.craftingGrid, false); @@ -142,4 +159,15 @@ protected NonNullList getRemainingItems(CraftingContainer ic, Level l .map(recipe -> recipe.value().getRemainingItems(ic)) .orElse(NonNullList.withSize(9, ItemStack.EMPTY)); } + + @Override + public void setRecipeUsed(@Nullable RecipeHolder recipe) { + this.recipeUsed = recipe; + } + + @Nullable + @Override + public RecipeHolder getRecipeUsed() { + return recipeUsed; + } } diff --git a/src/main/java/appeng/menu/slot/CraftingTermSlot.java b/src/main/java/appeng/menu/slot/CraftingTermSlot.java index 97fc2140415..f57a168f5c5 100644 --- a/src/main/java/appeng/menu/slot/CraftingTermSlot.java +++ b/src/main/java/appeng/menu/slot/CraftingTermSlot.java @@ -80,7 +80,7 @@ public boolean mayPickup(Player player) { } @Override - public void onTake(Player p, ItemStack is) { + public void onTake(Player player, ItemStack is) { } public void doClick(InventoryAction action, Player who) { @@ -197,6 +197,7 @@ private ItemStack craftItem(Player p, MEStorage inv, KeyCounter all) { } final var r = this.findRecipe(ic, level); + setRecipeUsed(r); if (r == null) { final var target = is.getItem(); @@ -239,7 +240,6 @@ private ItemStack craftItem(Player p, MEStorage inv, KeyCounter all) { if (this.preCraft(p, inv, set, is)) { this.makeItem(p, is); - this.postCraft(p, inv, set, is); } @@ -250,6 +250,7 @@ private ItemStack craftItem(Player p, MEStorage inv, KeyCounter all) { private boolean preCraft(Player p, MEStorage inv, ItemStack[] set, ItemStack result) { + return true; }