Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
Fix #126 (Compatibility with Fabric API 0.67.0+)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siphalor committed Nov 22, 2022
1 parent 75cf6b6 commit daa9ab0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ org.gradle.jvmargs=-Xmx1G
minecraft_version=1.17.1
minecraft_major_version = 1.17
yarn_mappings=1:v2
loader_version=0.11.6
loader_version=0.14.10

# Mod Properties
mod_version = 2.2.2
Expand Down
70 changes: 43 additions & 27 deletions src/main/java/de/siphalor/nbtcrafting/mixin/MixinRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package de.siphalor.nbtcrafting.mixin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import de.siphalor.nbtcrafting.api.RecipeUtil;
import de.siphalor.nbtcrafting.api.recipe.NBTCRecipe;
import de.siphalor.nbtcrafting.ingredient.IIngredient;

import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
Expand All @@ -28,53 +28,69 @@
import net.minecraft.util.collection.DefaultedList;
import org.apache.commons.lang3.ArrayUtils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import de.siphalor.nbtcrafting.api.RecipeUtil;
import de.siphalor.nbtcrafting.api.recipe.NBTCRecipe;
import de.siphalor.nbtcrafting.ingredient.IIngredient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mixin(Recipe.class)
public interface MixinRecipe {
@Shadow
DefaultedList<Ingredient> getIngredients();

/**
* @reason Returns the recipe remainders. Sadly has to overwrite since this is an interface.
* @author Siphalor
*/
@Overwrite
default DefaultedList<ItemStack> getRemainder(Inventory inventory) {
final DefaultedList<ItemStack> stackList = DefaultedList.ofSize(inventory.size(), ItemStack.EMPTY);
Map<String, Object> reference;
@Inject(method = "getRemainder", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILSOFT)
default void modifyRemainingStacks(Inventory inventory, CallbackInfoReturnable<DefaultedList<ItemStack>> cir, DefaultedList<ItemStack> stackList) {
// The stackList is already pre-populated with Vanilla and Fabric API remainders
List<Ingredient> ingredients;
int[] resolvedIngredientStacks;
if (this instanceof NBTCRecipe) {
boolean customRecipe = this instanceof NBTCRecipe;
if (customRecipe) {
ingredients = new ArrayList<>(((NBTCRecipe<?>) this).getIngredients());
resolvedIngredientStacks = RecipeUtil.resolveIngredients(ingredients, inventory);
// noinspection unchecked
reference = ((NBTCRecipe<Inventory>) this).buildDollarReference(inventory, resolvedIngredientStacks);
} else {
ingredients = getIngredients();
resolvedIngredientStacks = RecipeUtil.resolveIngredients(ingredients, inventory);
}

boolean shallContinue = false;
for (Ingredient ingredient : ingredients) {
if (((IIngredient) (Object) ingredient).nbtCrafting$isAdvanced()) {
shallContinue = true;
break;
}
}
if (!shallContinue) {
return;
}

// Resolve the ingredient indexes to the belonging stack indices
int[] resolvedIngredientStacks = RecipeUtil.resolveIngredients(ingredients, inventory);
Map<String, Object> reference;

if (customRecipe) {
// noinspection unchecked
reference = ((NBTCRecipe<Inventory>) this).buildDollarReference(inventory, resolvedIngredientStacks);
} else {
reference = RecipeUtil.buildReferenceMapFromResolvedIngredients(resolvedIngredientStacks, inventory);
}

for (int i = 0; i < stackList.size(); ++i) {
ItemStack stack = inventory.getStack(i);
int ingredientIndex = ArrayUtils.indexOf(resolvedIngredientStacks, i);
if (ingredientIndex >= 0) {
ItemStack remainder = ((IIngredient) (Object) ingredients.get(ingredientIndex)).nbtCrafting$getRecipeRemainder(stack, reference);
IIngredient ingredient = (IIngredient) (Object) ingredients.get(ingredientIndex);
// Simple, Vanilla-ish entries should already be set
if (!ingredient.nbtCrafting$isAdvanced()) {
continue;
}

ItemStack remainder = ingredient.nbtCrafting$getRecipeRemainder(stack, reference);
if (remainder != null) {
stackList.set(i, remainder);
continue;
}
}
if (stack.getItem().hasRecipeRemainder()) {
stackList.set(i, new ItemStack(stack.getItem().getRecipeRemainder()));
}
}
return stackList;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
]
},
"depends": {
"fabricloader": ">=0.4.0"
"fabricloader": ">=0.13.0"
},
"custom": {
"modmenu": {
Expand Down

0 comments on commit daa9ab0

Please sign in to comment.