    001    package net.minecraftforge.oredict;
    002
    003    import java.util.ArrayList;
    004    import java.util.Iterator;
    005
    006    import net.minecraft.src.Block;
    007    import net.minecraft.src.CraftingManager;
    008    import net.minecraft.src.IRecipe;
    009    import net.minecraft.src.InventoryCrafting;
    010    import net.minecraft.src.Item;
    011    import net.minecraft.src.ItemStack;
    012    import net.minecraft.src.ShapelessRecipes;
    013
    014    public class ShapelessOreRecipe implements IRecipe
    015    {
    016        private ItemStack output = null;
    017        private ArrayList input = new ArrayList();
    018
    019        public ShapelessOreRecipe(Block result, Object... recipe){ this(new ItemStack(result), recipe); }
    020        public ShapelessOreRecipe(Item  result, Object... recipe){ this(new ItemStack(result), recipe); }
    021
    022        public ShapelessOreRecipe(ItemStack result, Object... recipe)
    023        {
    024            output = result.copy();
    025            for (Object in : recipe)
    026            {
    027                if (in instanceof ItemStack)
    028                {
    029                    input.add(((ItemStack)in).copy());
    030                }
    031                else if (in instanceof Item)
    032                {
    033                    input.add(new ItemStack((Item)in));
    034                }
    035                else if (in instanceof Block)
    036                {
    037                    input.add(new ItemStack((Block)in));
    038                }
    039                else if (in instanceof String)
    040                {
    041                    input.add(OreDictionary.getOres((String)in));
    042                }
    043                else
    044                {
    045                    String ret = "Invalid shapeless ore recipe: ";
    046                    for (Object tmp :  recipe)
    047                    {
    048                        ret += tmp + ", ";
    049                    }
    050                    ret += output;
    051                    throw new RuntimeException(ret);
    052                }
    053            }
    054        }
    055
    056        @Override
    057        public int getRecipeSize(){ return input.size(); }
    058
    059        @Override
    060        public ItemStack getRecipeOutput(){ return output; }
    061
    062        @Override
    063        public ItemStack getCraftingResult(InventoryCrafting var1){ return output.copy(); }
    064
    065        @Override
    066        public boolean matches(InventoryCrafting var1)
    067        {
    068            ArrayList required = new ArrayList(input);
    069
    070            for (int x = 0; x < var1.getSizeInventory(); x++)
    071            {
    072                ItemStack slot = var1.getStackInSlot(x);
    073
    074                if (slot != null)
    075                {
    076                    boolean inRecipe = false;
    077                    Iterator req = required.iterator();
    078
    079                    while (req.hasNext())
    080                    {
    081                        boolean match = false;
    082
    083                        Object next = req.next();
    084
    085                        if (next instanceof ItemStack)
    086                        {
    087                            match = checkItemEquals((ItemStack)next, slot);
    088                        }
    089                        else if (next instanceof ArrayList)
    090                        {
    091                            for (ItemStack item : (ArrayList<ItemStack>)next)
    092                            {
    093                                match = match || checkItemEquals(item, slot);
    094                            }
    095                        }
    096
    097                        if (match)
    098                        {
    099                            inRecipe = true;
    100                            required.remove(next);
    101                            break;
    102                        }
    103                    }
    104
    105                    if (!inRecipe)
    106                    {
    107                        return false;
    108                    }
    109                }
    110            }
    111
    112            return required.isEmpty();
    113        }
    114
    115        private boolean checkItemEquals(ItemStack target, ItemStack input)
    116        {
    117            return (target.itemID == input.itemID && (target.getItemDamage() == -1 || target.getItemDamage() == input.getItemDamage()));
    118        }
    119    }