From 8f8f6a05f7622e6c88154f96dee16e29d1ae031e Mon Sep 17 00:00:00 2001 From: mezz Date: Wed, 9 Dec 2015 14:43:58 -0800 Subject: [PATCH] Fix #1006 Farm Block Recipes broken --- .../forestry/core/recipes/RecipeUtil.java | 5 +- .../core/recipes/ShapedRecipeCustom.java | 170 +++++++++++++++--- 2 files changed, 150 insertions(+), 25 deletions(-) diff --git a/src/main/java/forestry/core/recipes/RecipeUtil.java b/src/main/java/forestry/core/recipes/RecipeUtil.java index e1ba4b7cd..d842d639b 100644 --- a/src/main/java/forestry/core/recipes/RecipeUtil.java +++ b/src/main/java/forestry/core/recipes/RecipeUtil.java @@ -24,7 +24,6 @@ import net.minecraft.world.World; import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.oredict.OreDictionary; -import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; import cpw.mods.fml.common.registry.GameRegistry; @@ -215,12 +214,12 @@ public abstract class RecipeUtil { @SuppressWarnings("unchecked") public static void addRecipe(ItemStack itemstack, Object... obj) { - CraftingManager.getInstance().getRecipeList().add(new ShapedOreRecipe(itemstack, obj)); + CraftingManager.getInstance().getRecipeList().add(new ShapedRecipeCustom(itemstack, obj)); } @SuppressWarnings("unchecked") public static void addPriorityRecipe(ItemStack itemStack, Object... obj) { - CraftingManager.getInstance().getRecipeList().add(0, new ShapedOreRecipe(itemStack, obj)); + CraftingManager.getInstance().getRecipeList().add(0, new ShapedRecipeCustom(itemStack, obj)); } public static void addShapelessRecipe(Item item, Object... obj) { diff --git a/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java b/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java index 99c5157ac..64caf1d43 100644 --- a/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java +++ b/src/main/java/forestry/core/recipes/ShapedRecipeCustom.java @@ -10,22 +10,106 @@ ******************************************************************************/ package forestry.core.recipes; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; + +import net.minecraft.block.Block; import net.minecraft.inventory.IInventory; +import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.World; +import net.minecraftforge.oredict.OreDictionary; import net.minecraftforge.oredict.ShapedOreRecipe; import forestry.api.recipes.IDescriptiveRecipe; +import forestry.core.utils.ItemStackUtil; public class ShapedRecipeCustom extends ShapedOreRecipe implements IDescriptiveRecipe { - private final int width; - private final int height; + //Added in for future ease of change, but hard coded for now. + private static final int MAX_CRAFT_GRID_WIDTH = 3; + private static final int MAX_CRAFT_GRID_HEIGHT = 3; - public ShapedRecipeCustom(int width, int height, ItemStack product, Object... ingredients) { - super(product, ingredients); - this.width = width; - this.height = height; + private ItemStack output = null; + private Object[] input = null; + private int width; + private int height; + private boolean mirrored = true; + + public ShapedRecipeCustom(ItemStack result, Object... recipe) { + super(result, recipe); + output = result.copy(); + + String shape = ""; + int idx = 0; + + if (recipe[idx] instanceof Boolean) { + mirrored = (Boolean) recipe[idx]; + if (recipe[idx + 1] instanceof Object[]) { + recipe = (Object[]) recipe[idx + 1]; + } else { + idx = 1; + } + } + + if (recipe[idx] instanceof String[]) { + String[] parts = ((String[]) recipe[idx++]); + + for (String s : parts) { + width = s.length(); + shape += s; + } + + height = parts.length; + } else { + while (recipe[idx] instanceof String) { + String s = (String) recipe[idx++]; + shape += s; + width = s.length(); + height++; + } + } + + if (width * height != shape.length()) { + String ret = "Invalid shaped ore recipe: "; + for (Object tmp : recipe) { + ret += tmp + ", "; + } + ret += output; + throw new RuntimeException(ret); + } + + HashMap itemMap = new HashMap(); + + for (; idx < recipe.length; idx += 2) { + Character chr = (Character) recipe[idx]; + Object in = recipe[idx + 1]; + + if (in instanceof ItemStack) { + itemMap.put(chr, ((ItemStack) in).copy()); + } else if (in instanceof Item) { + itemMap.put(chr, new ItemStack((Item) in)); + } else if (in instanceof Block) { + itemMap.put(chr, new ItemStack((Block) in, 1, OreDictionary.WILDCARD_VALUE)); + } else if (in instanceof String) { + itemMap.put(chr, OreDictionary.getOres((String) in)); + } else { + String ret = "Invalid shaped ore recipe: "; + for (Object tmp : recipe) { + ret += tmp + ", "; + } + ret += output; + throw new RuntimeException(ret); + } + } + + input = new Object[width * height]; + int x = 0; + for (char chr : shape.toCharArray()) { + input[x++] = itemMap.get(chr); + } } @Override @@ -54,24 +138,66 @@ public class ShapedRecipeCustom extends ShapedOreRecipe implements IDescriptiveR return false; } - public static ShapedRecipeCustom createShapedRecipe(ItemStack product, Object... materials) { - int index = 0; - int columns = 0; - int rows = 0; - if (materials[index] instanceof String[]) { - String as[] = (String[]) materials[index]; - for (String pattern : as) { - rows++; - columns = pattern.length(); - } - } else { - while (materials[index] instanceof String) { - String pattern = (String) materials[index++]; - rows++; - columns = pattern.length(); + @Override + public boolean matches(InventoryCrafting inv, World world) { + for (int x = 0; x <= MAX_CRAFT_GRID_WIDTH - width; x++) { + for (int y = 0; y <= MAX_CRAFT_GRID_HEIGHT - height; ++y) { + if (checkMatch(inv, x, y, false)) { + return true; + } + + if (mirrored && checkMatch(inv, x, y, true)) { + return true; + } } } - return new ShapedRecipeCustom(columns, rows, product, materials); + return false; + } + + @SuppressWarnings("unchecked") + private boolean checkMatch(InventoryCrafting inv, int startX, int startY, boolean mirror) { + for (int x = 0; x < MAX_CRAFT_GRID_WIDTH; x++) { + for (int y = 0; y < MAX_CRAFT_GRID_HEIGHT; y++) { + int subX = x - startX; + int subY = y - startY; + Object target = null; + + if (subX >= 0 && subY >= 0 && subX < width && subY < height) { + if (mirror) { + target = input[width - subX - 1 + subY * width]; + } else { + target = input[subX + subY * width]; + } + } + + ItemStack slot = inv.getStackInRowAndColumn(x, y); + + if (target instanceof ItemStack) { + if (!ItemStackUtil.isCraftingEquivalent((ItemStack) target, slot)) { + return false; + } + } else if (target instanceof ArrayList) { + boolean matched = false; + + Iterator itr = ((ArrayList) target).iterator(); + while (itr.hasNext() && !matched) { + matched = ItemStackUtil.isCraftingEquivalent(itr.next(), slot); + } + + if (!matched) { + return false; + } + } else if (target == null && slot != null) { + return false; + } + } + } + + return true; + } + + public static ShapedRecipeCustom createShapedRecipe(ItemStack product, Object... materials) { + return new ShapedRecipeCustom(product, materials); } }