From 888047cad8bfc8f6f43838c8a542c1d855ab4612 Mon Sep 17 00:00:00 2001 From: mezz Date: Sat, 24 Oct 2015 19:59:59 -0700 Subject: [PATCH] Add preserveNBT option to IDescriptiveRecipe, simplify IFabricatorRecipe --- api/forestry | 2 +- .../forestry/core/recipes/RecipeUtil.java | 116 +++++++++++-- .../core/recipes/ShapedRecipeCustom.java | 152 +----------------- .../craftguide/CraftGuideCustomRecipes.java | 2 +- .../recipes/nei/NEIHandlerShapedCustom.java | 2 +- .../recipes/CarpenterRecipeManager.java | 3 +- .../factory/recipes/FabricatorRecipe.java | 7 +- .../recipes/FabricatorRecipeManager.java | 7 +- .../factory/tiles/TileFabricator.java | 3 +- 9 files changed, 131 insertions(+), 163 deletions(-) diff --git a/api/forestry b/api/forestry index 8cccb7b2a..7aafb5e52 160000 --- a/api/forestry +++ b/api/forestry @@ -1 +1 @@ -Subproject commit 8cccb7b2aa78304a38ff987d805667e8a3eccdf4 +Subproject commit 7aafb5e5215d7e2acf70fa96046a21c035adcf43 diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index c5442bc93..3dd823e1d 100644 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -98,17 +98,10 @@ public abstract class RecipeUtil { return craftingNbt; } - public static Object[] getCraftingRecipeAsArray(Object rec) { + public static Object[] getCraftingRecipeAsArray(IDescriptiveRecipe recipe) { try { - - if (rec instanceof IDescriptiveRecipe) { - - IDescriptiveRecipe recipe = (IDescriptiveRecipe) rec; - return getShapedRecipeAsArray(recipe.getWidth(), recipe.getHeight(), recipe.getIngredients(), recipe.getRecipeOutput()); - - } - + return getShapedRecipeAsArray(recipe.getWidth(), recipe.getHeight(), recipe.getIngredients(), recipe.getRecipeOutput()); } catch (Exception ex) { Log.warning("Exception while trying to parse an ItemStack[10] from an IRecipe:"); Log.warning(ex.getMessage()); @@ -268,4 +261,109 @@ public abstract class RecipeUtil { } GameRegistry.addSmelting(res, prod, xp); } + + public static ItemStack getCraftingResult(IDescriptiveRecipe recipe, IInventory inventoryCrafting) { + return getCraftingResult(recipe.getRecipeOutput(), recipe.preserveNBT(), inventoryCrafting); + } + + public static ItemStack getCraftingResult(ItemStack result, boolean preserveNBT, IInventory inventoryCrafting) { + result = result.copy(); + + if (preserveNBT) { + NBTTagCompound craftingNbt = getCraftingNbt(inventoryCrafting); + if (craftingNbt == null) { + return null; + } + + result.setTagCompound(craftingNbt); + } + + return result; + } + + public static boolean matches(IDescriptiveRecipe recipe, IInventory inventoryCrafting) { + if (!matches(recipe.getIngredients(), recipe.getWidth(), recipe.getHeight(), inventoryCrafting)) { + return false; + } + + if (recipe.preserveNBT()) { + if (getCraftingNbt(inventoryCrafting) == null) { + return false; + } + } + + return true; + } + + public static boolean matches(Object[] ingredients, int width, int height, IInventory inventoryCrafting) { + ItemStack[][] resources = new ItemStack[3][3]; + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + int k = i + j * 3; + resources[i][j] = inventoryCrafting.getStackInSlot(k); + } + } + + return matches(ingredients, width, height, resources); + } + + public static boolean matches(Object[] ingredients, int width, int height, ItemStack[][] resources) { + for (int i = 0; i <= 3 - width; i++) { + for (int j = 0; j <= 3 - height; j++) { + if (checkMatch(ingredients, width, height, resources, i, j, true)) { + return true; + } + + if (checkMatch(ingredients, width, height, resources, i, j, false)) { + return true; + } + } + } + + return false; + } + + private static boolean checkMatch(Object[] ingredients, int width, int height, ItemStack[][] resources, int xInGrid, int yInGrid, boolean flag) { + + for (int k = 0; k < 3; k++) { + for (int l = 0; l < 3; l++) { + + int widthIt = k - xInGrid; + int heightIt = l - yInGrid; + Object compare = null; + + if (widthIt >= 0 && heightIt >= 0 && widthIt < width && heightIt < height) { + if (flag) { + compare = ingredients[(width - widthIt - 1) + heightIt * width]; + } else { + compare = ingredients[widthIt + heightIt * width]; + } + } + ItemStack resource = resources[k][l]; + + if (compare instanceof ItemStack) { + if (!ItemStackUtil.isIdenticalItem((ItemStack) compare, resource)) { + return false; + } + } else if (compare instanceof Iterable) { + boolean matched = false; + + for (Object item : (Iterable) compare) { + if (item instanceof ItemStack) { + matched = matched || ItemStackUtil.isIdenticalItem((ItemStack) item, resource); + } + } + + if (!matched) { + return false; + } + + } else if (compare == null && resource != null) { + return false; + } + } + } + + return true; + } } diff --git a/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java b/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java index 16d6aac13..78d011b3a 100644 --- a/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java +++ b/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java @@ -10,7 +10,6 @@ ******************************************************************************/ package forestry.core.recipes; -import java.util.ArrayList; import java.util.HashMap; import net.minecraft.block.Block; @@ -19,7 +18,6 @@ import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.oredict.OreDictionary; @@ -50,7 +48,8 @@ public class ShapedRecipeCustom implements IDescriptiveRecipe { return this; } - public boolean preservesNbt() { + @Override + public boolean preserveNBT() { return preserveNBT; } @@ -76,130 +75,18 @@ public class ShapedRecipeCustom implements IDescriptiveRecipe { @Override public boolean matches(InventoryCrafting inventoryCrafting, World world) { - if (!matches((IInventory) inventoryCrafting, world)) { - return false; - } - - if (preserveNBT) { - if (RecipeUtil.getCraftingNbt(inventoryCrafting) == null) { - return false; - } - } - - return true; + return RecipeUtil.matches(this, inventoryCrafting); } + @Override + @Deprecated public boolean matches(IInventory inventoryCrafting, World world) { - ItemStack[][] resources = new ItemStack[3][3]; - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - int k = i + j * 3; - resources[i][j] = inventoryCrafting.getStackInSlot(k); - } - } - - return matches(resources); - } - - public boolean matches(ItemStack[][] resources) { - - for (int i = 0; i <= 3 - width; i++) { - for (int j = 0; j <= 3 - height; j++) { - if (checkMatch(resources, i, j, true)) { - return true; - } - - if (checkMatch(resources, i, j, false)) { - return true; - } - } - } - - return false; - } - - @SuppressWarnings("unchecked") - private boolean checkMatch(ItemStack[][] resources, int xInGrid, int yInGrid, boolean flag) { - - for (int k = 0; k < 3; k++) { - for (int l = 0; l < 3; l++) { - - int widthIt = k - xInGrid; - int heightIt = l - yInGrid; - Object compare = null; - - if (widthIt >= 0 && heightIt >= 0 && widthIt < width && heightIt < height) { - if (flag) { - compare = ingredients[(width - widthIt - 1) + heightIt * width]; - } else { - compare = ingredients[widthIt + heightIt * width]; - } - } - ItemStack resource = resources[k][l]; - - if (compare instanceof ItemStack) { - if (!checkItemMatch((ItemStack) compare, resource)) { - return false; - } - } else if (compare instanceof ArrayList) { - boolean matched = false; - - for (ItemStack item : (ArrayList) compare) { - matched = matched || checkItemMatch(item, resource); - } - - if (!matched) { - return false; - } - - } else if (compare == null && resource != null) { - return false; - } - } - } - - return true; - } - - private static boolean checkItemMatch(ItemStack compare, ItemStack resource) { - - if (resource == null && compare == null) { - return true; - } - - if (resource == null || compare == null) { - return false; - } - - if (compare.getItem() != resource.getItem()) { - return false; - } - - if (compare.getItemDamage() != OreDictionary.WILDCARD_VALUE && compare.getItemDamage() != resource.getItemDamage()) { - return false; - } - - return true; + return RecipeUtil.matches(this, inventoryCrafting); } @Override public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting) { - return getCraftingResult((IInventory) inventoryCrafting); - } - - public ItemStack getCraftingResult(IInventory inventoryCrafting) { - ItemStack result = product.copy(); - - if (preserveNBT) { - NBTTagCompound craftingNbt = RecipeUtil.getCraftingNbt(inventoryCrafting); - if (craftingNbt == null) { - return null; - } - - result.setTagCompound(craftingNbt); - } - - return result; + return RecipeUtil.getCraftingResult(this, inventoryCrafting); } @Override @@ -207,31 +94,6 @@ public class ShapedRecipeCustom implements IDescriptiveRecipe { return width * height; } - /** - * @return true if resource is a valid ingredient in this recipe. - */ - @SuppressWarnings("unchecked") - public boolean isIngredient(ItemStack resource) { - - for (Object ingredient : ingredients) { - if (ingredient instanceof ItemStack) { - if (checkItemMatch((ItemStack) ingredient, resource)) { - return true; - } - - } else if (ingredient instanceof ArrayList) { - for (ItemStack item : (ArrayList) ingredient) { - if (checkItemMatch(item, resource)) { - return true; - } - } - } - } - - return false; - - } - public static ShapedRecipeCustom createShapedRecipe(ItemStack product, Object... materials) { String s = ""; diff --git a/src/main/java/forestry/core/recipes/craftguide/CraftGuideCustomRecipes.java b/src/main/java/forestry/core/recipes/craftguide/CraftGuideCustomRecipes.java index d30778a0e..bf825d28f 100644 --- a/src/main/java/forestry/core/recipes/craftguide/CraftGuideCustomRecipes.java +++ b/src/main/java/forestry/core/recipes/craftguide/CraftGuideCustomRecipes.java @@ -49,7 +49,7 @@ public class CraftGuideCustomRecipes implements RecipeProvider { Object[] array = new Object[10]; - Object[] flattened = RecipeUtil.getCraftingRecipeAsArray(rec); + Object[] flattened = RecipeUtil.getCraftingRecipeAsArray((IDescriptiveRecipe) rec); if (flattened == null) { continue; } diff --git a/src/main/java/forestry/core/recipes/nei/NEIHandlerShapedCustom.java b/src/main/java/forestry/core/recipes/nei/NEIHandlerShapedCustom.java index 8ab6e23c2..57e9c2702 100644 --- a/src/main/java/forestry/core/recipes/nei/NEIHandlerShapedCustom.java +++ b/src/main/java/forestry/core/recipes/nei/NEIHandlerShapedCustom.java @@ -37,7 +37,7 @@ public class NEIHandlerShapedCustom extends RecipeHandlerBase { ItemStack output = recipe.getRecipeOutput(); this.output = new PositionedStack(output, 119, 24); } - this.preservesNbt = recipe.preservesNbt(); + this.preservesNbt = recipe.preserveNBT(); } public void setIngredients(int width, int height, Object[] items) { diff --git a/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java b/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java index fdd9217ee..edbd99503 100644 --- a/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java +++ b/src/main/java/forestry/factory/recipes/CarpenterRecipeManager.java @@ -26,6 +26,7 @@ import net.minecraftforge.fluids.FluidStack; import forestry.api.recipes.ICarpenterManager; import forestry.api.recipes.ICarpenterRecipe; import forestry.api.recipes.IDescriptiveRecipe; +import forestry.core.recipes.RecipeUtil; import forestry.core.recipes.ShapedRecipeCustom; import forestry.core.utils.ItemStackUtil; @@ -73,7 +74,7 @@ public class CarpenterRecipeManager implements ICarpenterManager { } IDescriptiveRecipe internal = recipe.getCraftingGridRecipe(); - return internal.matches(inventoryCrafting, world); + return RecipeUtil.matches(internal, inventoryCrafting); } public static boolean isBox(ItemStack resource) { diff --git a/src/main/java/forestry/factory/recipes/FabricatorRecipe.java b/src/main/java/forestry/factory/recipes/FabricatorRecipe.java index 337c06929..2a2dafddc 100644 --- a/src/main/java/forestry/factory/recipes/FabricatorRecipe.java +++ b/src/main/java/forestry/factory/recipes/FabricatorRecipe.java @@ -18,6 +18,7 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import forestry.api.recipes.IFabricatorRecipe; +import forestry.core.recipes.RecipeUtil; import forestry.core.recipes.ShapedRecipeCustom; import forestry.core.utils.ItemStackUtil; @@ -44,7 +45,7 @@ public class FabricatorRecipe implements IFabricatorRecipe { return false; } - return internal.matches(resources); + return RecipeUtil.matches(internal.getIngredients(), internal.getWidth(), internal.getHeight(), resources); } @Override @@ -64,7 +65,7 @@ public class FabricatorRecipe implements IFabricatorRecipe { @Override public boolean preservesNbt() { - return internal.preservesNbt(); + return internal.preserveNBT(); } @Override @@ -80,7 +81,7 @@ public class FabricatorRecipe implements IFabricatorRecipe { @Override public ItemStack getCraftingResult(IInventory craftingInventory) { - return internal.getCraftingResult(craftingInventory); + return RecipeUtil.getCraftingResult(internal, craftingInventory); } @Override diff --git a/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java b/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java index ee4b966b2..6ad04a6b2 100644 --- a/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java +++ b/src/main/java/forestry/factory/recipes/FabricatorRecipeManager.java @@ -23,6 +23,7 @@ import net.minecraftforge.fluids.FluidStack; import forestry.api.recipes.IFabricatorManager; import forestry.api.recipes.IFabricatorRecipe; import forestry.api.recipes.RecipeManagers; +import forestry.core.recipes.RecipeUtil; import forestry.core.recipes.ShapedRecipeCustom; import forestry.core.utils.ItemStackUtil; @@ -50,7 +51,11 @@ public class FabricatorRecipeManager implements IFabricatorManager { } for (IFabricatorRecipe recipe : recipes) { - if (recipe.matches(plan, gridResources)) { + if (recipe.getPlan() != null && !ItemStackUtil.isCraftingEquivalent(recipe.getPlan(), plan)) { + continue; + } + + if (RecipeUtil.matches(recipe.getIngredients(), recipe.getWidth(), recipe.getHeight(), gridResources)) { if (liquid == null || liquid.containsFluid(recipe.getLiquid())) { return recipe; } diff --git a/src/main/java/forestry/factory/tiles/TileFabricator.java b/src/main/java/forestry/factory/tiles/TileFabricator.java index 97adf4e8a..cf8265eaa 100644 --- a/src/main/java/forestry/factory/tiles/TileFabricator.java +++ b/src/main/java/forestry/factory/tiles/TileFabricator.java @@ -41,6 +41,7 @@ import forestry.core.items.ICraftingPlan; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.GuiId; +import forestry.core.recipes.RecipeUtil; import forestry.core.tiles.ICrafter; import forestry.core.tiles.ILiquidTankTile; import forestry.core.tiles.TilePowered; @@ -228,7 +229,7 @@ public class TileFabricator extends TilePowered implements ICrafter, ILiquidTank return null; } - return myRecipe.getCraftingResult(craftingInventory); + return RecipeUtil.getCraftingResult(myRecipe.getRecipeOutput(), myRecipe.preservesNbt(), craftingInventory); } @Override