diff --git a/gradle.properties b/gradle.properties index c9e21529..04d05ecc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,10 +7,10 @@ org.gradle.jvmargs=-Xmx1G minecraft_version=1.19 minecraft_major_version = 1.19 yarn_mappings=4:v2 - loader_version=0.14.8 + loader_version=0.14.10 # Mod Properties - mod_version = 2.2.2 + mod_version = 2.2.3 mod_release = release mod_mc_version_specifier = 1.19 mod_mc_versions = 1.19;1.19.1;1.19.2 diff --git a/src/main/java/de/siphalor/nbtcrafting/api/recipe/NBTCRecipe.java b/src/main/java/de/siphalor/nbtcrafting/api/recipe/NBTCRecipe.java index b6c561ad..de4fbc48 100644 --- a/src/main/java/de/siphalor/nbtcrafting/api/recipe/NBTCRecipe.java +++ b/src/main/java/de/siphalor/nbtcrafting/api/recipe/NBTCRecipe.java @@ -35,4 +35,15 @@ public interface NBTCRecipe extends Recipe { * @return A map consisting of keys and belonging {@link net.minecraft.nbt.NbtCompound}s, {@link Number}s or {@link String}s */ Map buildDollarReference(I inv); + + /** + * An advanced form of the reference map, that allows to make use of the ingredient to stack resolution that Nbt Crafting does anyway. + * @implNote For backwards compatibility, you must still override the simpler form {@link #buildDollarReference(Inventory)} either way. + * @param inv the inventory for that this method is being called + * @param ingredientToStackResolution An array which resolves the ingredient indexes from {@link #getIngredients()} to the stacks in the inventory. + * @return A map consisting of keys and belonging {@link net.minecraft.nbt.CompoundTag}s, {@link Number}s or {@link String}s + */ + default Map buildDollarReference(I inv, int[] ingredientToStackResolution) { + return buildDollarReference(inv); + } } diff --git a/src/main/java/de/siphalor/nbtcrafting/mixin/MixinIngredient.java b/src/main/java/de/siphalor/nbtcrafting/mixin/MixinIngredient.java index eb9334cf..059ef568 100644 --- a/src/main/java/de/siphalor/nbtcrafting/mixin/MixinIngredient.java +++ b/src/main/java/de/siphalor/nbtcrafting/mixin/MixinIngredient.java @@ -312,10 +312,7 @@ private static IngredientEntryCondition loadIngredientEntryCondition(JsonObject if (advancedEntries != null) { for (IngredientEntry entry : advancedEntries) { if (entry.matches(stack)) { - ItemStack remainder = entry.getRecipeRemainder(stack, reference); - if (remainder != null) { - return remainder; - } + return entry.getRecipeRemainder(stack, reference); } } } diff --git a/src/main/java/de/siphalor/nbtcrafting/mixin/MixinRecipe.java b/src/main/java/de/siphalor/nbtcrafting/mixin/MixinRecipe.java index 321a65d2..fa3189ca 100644 --- a/src/main/java/de/siphalor/nbtcrafting/mixin/MixinRecipe.java +++ b/src/main/java/de/siphalor/nbtcrafting/mixin/MixinRecipe.java @@ -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; @@ -28,36 +28,51 @@ 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 getIngredients(); - /** - * @reason Returns the recipe remainders. Sadly has to overwrite since this is an interface. - * @author Siphalor - */ - @Overwrite - default DefaultedList getRemainder(Inventory inventory) { - final DefaultedList stackList = DefaultedList.ofSize(inventory.size(), ItemStack.EMPTY); - Map reference; + @Inject(method = "getRemainder", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILSOFT) + default void modifyRemainingStacks(Inventory inventory, CallbackInfoReturnable> cir, DefaultedList stackList) { + // The stackList is already pre-populated with Vanilla and Fabric API remainders List ingredients; - int[] resolvedIngredientStacks; - if (this instanceof NBTCRecipe) { + boolean customRecipe = this instanceof NBTCRecipe; + if (customRecipe) { ingredients = new ArrayList<>(((NBTCRecipe) this).getIngredients()); - // noinspection unchecked - reference = ((NBTCRecipe) this).buildDollarReference(inventory); - resolvedIngredientStacks = RecipeUtil.resolveIngredients(ingredients, inventory); } 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 reference; + + if (customRecipe) { + // noinspection unchecked + reference = ((NBTCRecipe) this).buildDollarReference(inventory, resolvedIngredientStacks); + } else { reference = RecipeUtil.buildReferenceMapFromResolvedIngredients(resolvedIngredientStacks, inventory); } @@ -65,16 +80,17 @@ default DefaultedList getRemainder(Inventory inventory) { 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; } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 79fe2a46..1d5f59d9 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -43,7 +43,7 @@ ] }, "depends": { - "fabricloader": ">=0.4.0" + "fabricloader": ">=0.13.0" }, "custom": { "modmenu": {