Prevent duplciates in registered Ores in the OreDictionary and clean up some of the code, add new function to return all ores the specified ItemStack satisfies. Closes #1102

This commit is contained in:
Lex Manos 2014-05-25 14:20:02 -07:00
parent 507f184144
commit 25ef7e3469
3 changed files with 54 additions and 21 deletions

View File

@ -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<Integer> ids = new ArrayList<Integer>();
for(Entry<Integer, ArrayList<ItemStack>> 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<ItemStack> 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<ItemStack> 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));

View File

@ -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<ItemStack>)target)
Iterator<ItemStack> itr = ((ArrayList<ItemStack>)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;

View File

@ -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<ItemStack>)next)
Iterator<ItemStack> itr = ((ArrayList<ItemStack>)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.