Skip to content

Commit

Permalink
Readd recipe awarding
Browse files Browse the repository at this point in the history
Now recipes you use will be awarded as expected.
  • Loading branch information
Ampflower committed Dec 6, 2022
1 parent fa6efc6 commit eb134a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/main/java/tfar/fastbench/MixinHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import tfar.fastbench.interfaces.CraftingInventoryDuck;
import tfar.fastbench.mixin.ContainerAccessor;

import java.util.Collections;

public class MixinHooks {

public static boolean hascachedrecipe = false;
Expand All @@ -38,19 +40,18 @@ public static void slotChangedCraftingGrid(Level level, CraftingContainer inv, R

public static ItemStack handleShiftCraft(Player player, AbstractContainerMenu container, Slot resultSlot, CraftingContainer input, ResultContainer craftResult, int outStart, int outEnd) {
ItemStack outputCopy = ItemStack.EMPTY;
CraftingInventoryDuck duck = (CraftingInventoryDuck)input;
CraftingInventoryDuck duck = (CraftingInventoryDuck) input;
duck.setCheckMatrixChanges(false);
if (resultSlot != null && resultSlot.hasItem()) {

Recipe<CraftingContainer> recipe = (Recipe<CraftingContainer>) craftResult.getRecipeUsed();
Recipe<CraftingContainer> recipe = (Recipe<CraftingContainer>) craftResult.getRecipeUsed();

while (recipe != null && recipe.matches(input, player.level)) {
if (recipe != null && resultSlot != null && resultSlot.hasItem()) {
while (recipe.matches(input, player.level)) {
ItemStack recipeOutput = resultSlot.getItem().copy();
outputCopy = recipeOutput.copy();

recipeOutput.getItem().onCraftedBy(recipeOutput, player.level, player);

if (!player.level.isClientSide && !((ContainerAccessor)container).insert(recipeOutput, outStart, outEnd,true)) {
if (!player.level.isClientSide && !((ContainerAccessor) container).insert(recipeOutput, outStart, outEnd, true)) {
duck.setCheckMatrixChanges(true);
return ItemStack.EMPTY;
}
Expand All @@ -69,9 +70,14 @@ public static ItemStack handleShiftCraft(Player player, AbstractContainerMenu co
}
duck.setCheckMatrixChanges(true);
slotChangedCraftingGrid(player.level, input, craftResult);

// Award the player the recipe for using it. Mimics vanilla behaviour.
if (!recipe.isSpecial()) {
player.awardRecipes(Collections.singleton(recipe));
}
}
duck.setCheckMatrixChanges(true);
return craftResult.getRecipeUsed() == null ? ItemStack.EMPTY : outputCopy;
return recipe == null ? ItemStack.EMPTY : outputCopy;
}

public static Recipe<CraftingContainer> findRecipe(CraftingContainer inv, Level level) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/tfar/fastbench/mixin/CraftingResultSlotMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import tfar.fastbench.MixinHooks;
import tfar.fastbench.interfaces.CraftingInventoryDuck;

import java.util.Collections;

@Mixin(ResultSlot.class)
public class CraftingResultSlotMixin extends Slot {
Expand Down Expand Up @@ -46,7 +49,13 @@ public void set(ItemStack stack) {
@Redirect(method = "checkTakeAchievements",
at = @At(value = "INVOKE",target = "Lnet/minecraft/world/inventory/RecipeHolder;awardUsedRecipes(Lnet/minecraft/world/entity/player/Player;)V"))
public void no(RecipeHolder recipeUnlocker, Player player) {
//do nothing
if (((CraftingInventoryDuck) craftSlots).getCheckMatrixChanges() &&
this.container instanceof RecipeHolder recipeHolder) {
var recipeUsed = recipeHolder.getRecipeUsed();
if (recipeUsed != null && !recipeUsed.isSpecial()) {
player.awardRecipes(Collections.singleton(recipeUsed));
}
}
}

//this.container is actually the crafting result inventory so it's a safe cast
Expand Down

0 comments on commit eb134a5

Please sign in to comment.