diff --git a/src/main/java/net/minecraftforge/oredict/OreDictionary.java b/src/main/java/net/minecraftforge/oredict/OreDictionary.java index 74fa602adb..0dbdcd2fbd 100644 --- a/src/main/java/net/minecraftforge/oredict/OreDictionary.java +++ b/src/main/java/net/minecraftforge/oredict/OreDictionary.java @@ -294,7 +294,7 @@ public class OreDictionary { for(ItemStack target : ore.getValue()) { - if(itemStack.getItem() == target.getItem() && (target.getItemDamage() == WILDCARD_VALUE || itemStack.getItemDamage() == target.getItemDamage())) + if (itemMatches(itemStack, target, false)) { return ore.getKey(); } @@ -303,6 +303,34 @@ public class OreDictionary return -1; // didn't find it. } + /** + * Gets all the integer ID for the ores that the specified item stakc is registered to. + * If the item stack is not linked to any ore, this will return an empty array and no new entry will be created. + * + * @param itemStack The item stack of the ore. + * @return An array of ids that this ore is registerd as. + */ + public static int[] getOreIDs(ItemStack itemStack) + { + if (itemStack == null) return new int[0]; + + List ids = new ArrayList(); + for(Entry> ore : oreStacks.entrySet()) + { + for(ItemStack target : ore.getValue()) + { + if (itemMatches(itemStack, target, false)) + { + ids.add(ore.getKey()); + } + } + } + int[] ret = new int[ids.size()]; + for (int x = 0; x < ids.size(); x++) + ret[x] = ids.get(x); + return ret; + } + /** * Retrieves the ArrayList of items that are registered to this ore type. * Creates the list as empty if it did not exist. @@ -358,6 +386,21 @@ public class OreDictionary return false; } + private static boolean containsMatch(boolean strict, List inputs, ItemStack... targets) + { + for (ItemStack input : inputs) + { + for (ItemStack target : targets) + { + if (itemMatches(target, input, strict)) + { + return true; + } + } + } + return false; + } + public static boolean itemMatches(ItemStack target, ItemStack input, boolean strict) { if (input == null && target != null || input != null && target == null) @@ -386,6 +429,7 @@ public class OreDictionary private static void registerOre(String name, int id, ItemStack ore) { ArrayList ores = getOres(id); + if (containsMatch(false, ores, ore)) return; // Prevent duplicates. ore = ore.copy(); ores.add(ore); MinecraftForge.EVENT_BUS.post(new OreRegisterEvent(name, ore)); diff --git a/src/main/java/net/minecraftforge/oredict/ShapedOreRecipe.java b/src/main/java/net/minecraftforge/oredict/ShapedOreRecipe.java index a9f3d61710..1451122cae 100644 --- a/src/main/java/net/minecraftforge/oredict/ShapedOreRecipe.java +++ b/src/main/java/net/minecraftforge/oredict/ShapedOreRecipe.java @@ -2,6 +2,7 @@ package net.minecraftforge.oredict; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; @@ -209,7 +210,7 @@ public class ShapedOreRecipe implements IRecipe if (target instanceof ItemStack) { - if (!checkItemEquals((ItemStack)target, slot)) + if (!OreDictionary.itemMatches((ItemStack)target, slot, false)) { return false; } @@ -218,9 +219,10 @@ public class ShapedOreRecipe implements IRecipe { boolean matched = false; - for (ItemStack item : (ArrayList)target) + Iterator itr = ((ArrayList)target).iterator(); + while (itr.hasNext() && !matched) { - matched = matched || checkItemEquals(item, slot); + matched = OreDictionary.itemMatches(itr.next(), slot, false); } if (!matched) @@ -238,15 +240,6 @@ public class ShapedOreRecipe implements IRecipe return true; } - private boolean checkItemEquals(ItemStack target, ItemStack input) - { - if (input == null && target != null || input != null && target == null) - { - return false; - } - return (target.getItem() == input.getItem() && (target.getItemDamage() == OreDictionary.WILDCARD_VALUE|| target.getItemDamage() == input.getItemDamage())); - } - public ShapedOreRecipe setMirrored(boolean mirror) { mirrored = mirror; diff --git a/src/main/java/net/minecraftforge/oredict/ShapelessOreRecipe.java b/src/main/java/net/minecraftforge/oredict/ShapelessOreRecipe.java index 36bfbc96ae..c7a0111a4d 100644 --- a/src/main/java/net/minecraftforge/oredict/ShapelessOreRecipe.java +++ b/src/main/java/net/minecraftforge/oredict/ShapelessOreRecipe.java @@ -108,13 +108,14 @@ public class ShapelessOreRecipe implements IRecipe if (next instanceof ItemStack) { - match = checkItemEquals((ItemStack)next, slot); + match = OreDictionary.itemMatches((ItemStack)next, slot, false); } else if (next instanceof ArrayList) { - for (ItemStack item : (ArrayList)next) + Iterator itr = ((ArrayList)next).iterator(); + while (itr.hasNext() && !match) { - match = match || checkItemEquals(item, slot); + match = OreDictionary.itemMatches(itr.next(), slot, false); } } @@ -136,11 +137,6 @@ public class ShapelessOreRecipe implements IRecipe return required.isEmpty(); } - private boolean checkItemEquals(ItemStack target, ItemStack input) - { - return (target.getItem() == input.getItem() && (target.getItemDamage() == OreDictionary.WILDCARD_VALUE || target.getItemDamage() == input.getItemDamage())); - } - /** * Returns the input for this recipe, any mod accessing this value should never * manipulate the values in this array as it will effect the recipe itself.