Skip to content

Commit

Permalink
Implement awarding of crafted recipes advancement in crafting terminal (
Browse files Browse the repository at this point in the history
#7678)

Fixes #7574
  • Loading branch information
shartte authored Feb 25, 2024
1 parent 3f298ba commit 51cb354
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
7 changes: 0 additions & 7 deletions src/main/java/appeng/crafting/CraftingEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
52 changes: 40 additions & 12 deletions src/main/java/appeng/menu/slot/AppEngCraftingSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -142,4 +159,15 @@ protected NonNullList<ItemStack> 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;
}
}
5 changes: 3 additions & 2 deletions src/main/java/appeng/menu/slot/CraftingTermSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 51cb354

Please sign in to comment.