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

Commit

Permalink
Merge branch '1.18-2.0' into 1.19-2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Siphalor committed Nov 22, 2022
2 parents 2fa8a77 + f1708c7 commit 977e7c2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/siphalor/nbtcrafting/api/recipe/NBTCRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,15 @@ public interface NBTCRecipe<I extends Inventory> extends Recipe<I> {
* @return A map consisting of keys and belonging {@link net.minecraft.nbt.NbtCompound}s, {@link Number}s or {@link String}s
*/
Map<String, Object> 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<String, Object> buildDollarReference(I inv, int[] ingredientToStackResolution) {
return buildDollarReference(inv);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
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());
// noinspection unchecked
reference = ((NBTCRecipe<Inventory>) 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<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 977e7c2

Please sign in to comment.