diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index ce771e240..b5f60c997 100644 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -196,16 +196,17 @@ public abstract class RecipeUtil { GameRegistry.addSmelting(res, prod, xp); } - public static boolean matches(IDescriptiveRecipe recipe, IInventory inventoryCrafting) { + public static String[][] matches(IDescriptiveRecipe recipe, IInventory inventoryCrafting) { NonNullList> recipeIngredients = recipe.getIngredients(); + NonNullList oreDicts = recipe.getOreDicts(); int width = recipe.getWidth(); int height = recipe.getHeight(); - return matches(recipeIngredients, width, height, inventoryCrafting); + return matches(recipeIngredients, oreDicts, width, height, inventoryCrafting); } - public static boolean matches(NonNullList> recipeIngredients, int width, int height, IInventory inventoryCrafting) { + public static String[][] matches(NonNullList> recipeIngredients, NonNullList oreDicts, int width, int height, IInventory inventoryCrafting) { ItemStack[][] resources = getResources(inventoryCrafting); - return matches(recipeIngredients, width, height, resources); + return matches(recipeIngredients, oreDicts, width, height, resources); } public static ItemStack[][] getResources(IInventory inventoryCrafting) { @@ -219,23 +220,28 @@ public abstract class RecipeUtil { return resources; } - public static boolean matches(NonNullList> recipeIngredients, int width, int height, ItemStack[][] resources) { + @Nullable + public static String[][] matches(NonNullList> recipeIngredients, NonNullList oreDicts, int width, int height, ItemStack[][] resources) { for (int i = 0; i <= 3 - width; i++) { for (int j = 0; j <= 3 - height; j++) { - if (checkMatch(recipeIngredients, width, height, resources, i, j, true)) { - return true; + String[][] resourceDicts = checkMatch(recipeIngredients, oreDicts, width, height, resources, i, j, true); + if (resourceDicts != null) { + return resourceDicts; } - if (checkMatch(recipeIngredients, width, height, resources, i, j, false)) { - return true; + resourceDicts = checkMatch(recipeIngredients, oreDicts, width, height, resources, i, j, false); + if (resourceDicts != null) { + return resourceDicts; } } } - return false; + return null; } - private static boolean checkMatch(NonNullList> recipeIngredients, int width, int height, ItemStack[][] resources, int xInGrid, int yInGrid, boolean mirror) { + @Nullable + private static String[][] checkMatch(NonNullList> recipeIngredients, NonNullList oreDicts, int width, int height, ItemStack[][] resources, int xInGrid, int yInGrid, boolean mirror) { + String[][] resourceDicts = new String[3][3]; for (int k = 0; k < 3; k++) { for (int l = 0; l < 3; l++) { ItemStack resource = resources[k][l]; @@ -243,22 +249,27 @@ public abstract class RecipeUtil { int widthIt = k - xInGrid; int heightIt = l - yInGrid; NonNullList recipeIngredient = null; + String oreDict = ""; if (widthIt >= 0 && heightIt >= 0 && widthIt < width && heightIt < height) { + int position; if (mirror) { - recipeIngredient = recipeIngredients.get(width - widthIt - 1 + heightIt * width); + position = width - widthIt - 1 + heightIt * width; } else { - recipeIngredient = recipeIngredients.get(widthIt + heightIt * width); + position = widthIt + heightIt * width; } + recipeIngredient = recipeIngredients.get(position); + oreDict = oreDicts.get(position); } if (!checkIngredientMatch(recipeIngredient, resource)) { - return false; + return null; } + resourceDicts[k][l] = oreDict; } } - return true; + return resourceDicts; } private static boolean checkIngredientMatch(@Nullable NonNullList recipeIngredient, ItemStack resource) { diff --git a/src/main/java/forestry/core/utils/InventoryUtil.java b/src/main/java/forestry/core/utils/InventoryUtil.java index 2b5851893..2c24b0df8 100644 --- a/src/main/java/forestry/core/utils/InventoryUtil.java +++ b/src/main/java/forestry/core/utils/InventoryUtil.java @@ -377,6 +377,19 @@ public abstract class InventoryUtil { } return result; } + + public static NonNullList getOreDictAsList(String[][] oreDicts) { + NonNullList result = NonNullList.withSize(9, ""); + if(oreDicts == null || oreDicts.length == 0){ + return result; + } + for (int i = 0; i < oreDicts.length; i++) { + for (int d = 0; d < oreDicts.length; d++) { + result.set(i, oreDicts[i][d]); + } + } + return result; + } public static boolean tryAddStacksCopy(IInventory inventory, NonNullList stacks, int startSlot, int slots, boolean all) { diff --git a/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java b/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java index 9b6d6d4b2..ef831f1a7 100644 --- a/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java +++ b/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java @@ -11,6 +11,9 @@ package forestry.factory.recipes; import javax.annotation.Nullable; + +import org.apache.commons.lang3.tuple.Pair; + import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -48,30 +51,31 @@ public class CarpenterRecipeManager implements ICarpenterManager { } @Nullable - public static ICarpenterRecipe findMatchingRecipe(@Nullable FluidStack liquid, ItemStack item, IInventory inventorycrafting) { + public static Pair findMatchingRecipe(@Nullable FluidStack liquid, ItemStack item, IInventory inventorycrafting) { for (ICarpenterRecipe recipe : recipes) { - if (matches(recipe, liquid, item, inventorycrafting)) { - return recipe; + String[][] resourceDicts = matches(recipe, liquid, item, inventorycrafting); + if (resourceDicts != null) { + return Pair.of(recipe, resourceDicts); } } - return null; + return Pair.of(null, null); } - public static boolean matches(@Nullable ICarpenterRecipe recipe, @Nullable FluidStack resource, ItemStack item, IInventory inventoryCrafting) { + public static String[][] matches(@Nullable ICarpenterRecipe recipe, @Nullable FluidStack resource, ItemStack item, IInventory inventoryCrafting) { if (recipe == null) { - return false; + return null; } FluidStack liquid = recipe.getFluidResource(); if (liquid != null) { if (resource == null || !resource.containsFluid(liquid)) { - return false; + return null; } } ItemStack box = recipe.getBox(); if (!box.isEmpty() && !ItemStackUtil.isCraftingEquivalent(box, item)) { - return false; + return null; } IDescriptiveRecipe internal = recipe.getCraftingGridRecipe(); diff --git a/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java b/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java index 8034e12df..e50a055e5 100644 --- a/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java +++ b/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java @@ -11,6 +11,9 @@ package forestry.factory.recipes; import javax.annotation.Nullable; + +import org.apache.commons.lang3.tuple.Pair; + import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -39,19 +42,20 @@ public class FabricatorRecipeManager implements IFabricatorManager { } @Nullable - public static IFabricatorRecipe findMatchingRecipe(ItemStack plan, IInventory resources) { + public static Pair findMatchingRecipe(ItemStack plan, IInventory resources) { ItemStack[][] gridResources = RecipeUtil.getResources(resources); for (IFabricatorRecipe recipe : recipes) { if (!recipe.getPlan().isEmpty() && !ItemStackUtil.isCraftingEquivalent(recipe.getPlan(), plan)) { continue; } - if (RecipeUtil.matches(recipe.getIngredients(), recipe.getWidth(), recipe.getHeight(), gridResources)) { - return recipe; + String[][] oreDicts = RecipeUtil.matches(recipe.getIngredients(), recipe.getOreDicts(), recipe.getWidth(), recipe.getHeight(), gridResources); + if (oreDicts != null) { + return Pair.of(recipe, oreDicts); } } - return null; + return Pair.of(null, null); } public static boolean isPlan(ItemStack plan) { diff --git a/src/main/java/forestry/factory/tiles/TileCarpenter.java b/src/main/java/forestry/factory/tiles/TileCarpenter.java index 30b337427..93a2e45d2 100644 --- a/src/main/java/forestry/factory/tiles/TileCarpenter.java +++ b/src/main/java/forestry/factory/tiles/TileCarpenter.java @@ -11,6 +11,9 @@ package forestry.factory.tiles; import javax.annotation.Nullable; + +import org.apache.commons.lang3.tuple.Pair; + import java.io.IOException; import forestry.api.core.IErrorLogic; @@ -57,6 +60,8 @@ public class TileCarpenter extends TilePowered implements ISidedInventory, ILiqu @Nullable private ICarpenterRecipe currentRecipe; + @Nullable + private String[][] oreDicts; private ItemStack getBoxStack() { return getInternalInventory().getStackInSlot(InventoryCarpenter.SLOT_BOX); @@ -109,8 +114,10 @@ public class TileCarpenter extends TilePowered implements ISidedInventory, ILiqu return; } - if (!CarpenterRecipeManager.matches(currentRecipe, resourceTank.getFluid(), getBoxStack(), craftingInventory)) { - currentRecipe = CarpenterRecipeManager.findMatchingRecipe(resourceTank.getFluid(), getBoxStack(), craftingInventory); + if (CarpenterRecipeManager.matches(currentRecipe, resourceTank.getFluid(), getBoxStack(), craftingInventory) == null) { + Pair recipePair = CarpenterRecipeManager.findMatchingRecipe(resourceTank.getFluid(), getBoxStack(), craftingInventory); + currentRecipe = recipePair.getLeft(); + oreDicts = recipePair.getRight(); if (currentRecipe != null) { int recipeTime = currentRecipe.getPackagingTime(); @@ -186,7 +193,7 @@ public class TileCarpenter extends TilePowered implements ISidedInventory, ILiqu NonNullList craftingSets = InventoryUtil.getStacks(craftingInventory, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT); IInventory inventory = new InventoryMapper(getInternalInventory(), InventoryCarpenter.SLOT_INVENTORY_1, InventoryCarpenter.SLOT_INVENTORY_COUNT); - return InventoryUtil.removeSets(inventory, 1, craftingSets, currentRecipe.getCraftingGridRecipe().getOreDicts(), null, true, false, doRemove); + return InventoryUtil.removeSets(inventory, 1, craftingSets, InventoryUtil.getOreDictAsList(oreDicts), null, true, false, doRemove); } /* STATE INFORMATION */ diff --git a/src/main/java/forestry/factory/tiles/TileFabricator.java b/src/main/java/forestry/factory/tiles/TileFabricator.java index 663e88193..7b623e8a5 100644 --- a/src/main/java/forestry/factory/tiles/TileFabricator.java +++ b/src/main/java/forestry/factory/tiles/TileFabricator.java @@ -11,6 +11,9 @@ package forestry.factory.tiles; import javax.annotation.Nullable; + +import org.apache.commons.lang3.tuple.Pair; + import java.io.IOException; import forestry.api.core.IErrorLogic; @@ -161,19 +164,20 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I } @Nullable - private IFabricatorRecipe getRecipe() { + private Pair getRecipe() { IInventoryAdapter inventory = getInternalInventory(); ItemStack plan = inventory.getStackInSlot(InventoryFabricator.SLOT_PLAN); FluidStack liquid = moltenTank.getFluid(); - IFabricatorRecipe recipe = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory); + Pair recipePair = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory); + IFabricatorRecipe recipe = recipePair.getLeft(); if (liquid != null && recipe != null && !liquid.containsFluid(recipe.getLiquid())) { return null; } - return recipe; + return recipePair; } public ItemStack getResult() { - IFabricatorRecipe myRecipe = getRecipe(); + IFabricatorRecipe myRecipe = getRecipe().getLeft(); if (myRecipe == null) { return ItemStack.EMPTY; @@ -191,17 +195,18 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I } private void craftResult() { - IFabricatorRecipe myRecipe = getRecipe(); + Pair myRecipePair = getRecipe(); ItemStack craftResult = getResult(); + IFabricatorRecipe myRecipe = myRecipePair.getLeft(); if (myRecipe != null && !craftResult.isEmpty() && getStackInSlot(InventoryFabricator.SLOT_RESULT).isEmpty()) { FluidStack liquid = myRecipe.getLiquid(); // Remove resources NonNullList crafting = InventoryUtil.getStacks(craftingInventory, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT); - if (removeFromInventory(crafting, myRecipe, false)) { + if (removeFromInventory(crafting, myRecipePair.getRight(), myRecipe, false)) { FluidStack drained = moltenTank.drainInternal(liquid, false); if (drained != null && drained.isFluidStackIdentical(liquid)) { - removeFromInventory(crafting, myRecipe, true); + removeFromInventory(crafting, myRecipePair.getRight(), myRecipe, true); moltenTank.drain(liquid.amount, true); // Damage plan @@ -219,9 +224,9 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I } } - private boolean removeFromInventory(NonNullList set, IFabricatorRecipe recipe, boolean doRemove) { + private boolean removeFromInventory(NonNullList set, String[][] oreDicts, IFabricatorRecipe recipe, boolean doRemove) { IInventory inventory = new InventoryMapper(this, InventoryFabricator.SLOT_INVENTORY_1, InventoryFabricator.SLOT_INVENTORY_COUNT); - return InventoryUtil.removeSets(inventory, 1, set, recipe.getOreDicts(), null, true, false, doRemove); + return InventoryUtil.removeSets(inventory, 1, set, InventoryUtil.getOreDictAsList(oreDicts), null, true, false, doRemove); } @Override @@ -231,10 +236,11 @@ public class TileFabricator extends TilePowered implements ISlotPickupWatcher, I boolean hasResources = true; ItemStack plan = getStackInSlot(InventoryFabricator.SLOT_PLAN); - IFabricatorRecipe recipe = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory); + Pair recipePair = FabricatorRecipeManager.findMatchingRecipe(plan, craftingInventory); + IFabricatorRecipe recipe = recipePair.getLeft(); if (recipe != null) { NonNullList crafting = InventoryUtil.getStacks(craftingInventory, InventoryGhostCrafting.SLOT_CRAFTING_1, InventoryGhostCrafting.SLOT_CRAFTING_COUNT); - hasResources = removeFromInventory(crafting, recipe, false); + hasResources = removeFromInventory(crafting, recipePair.getRight(), recipe, false); FluidStack toDrain = recipe.getLiquid(); FluidStack drained = moltenTank.drainInternal(toDrain, false); hasLiquidResources = drained != null && drained.isFluidStackIdentical(toDrain);