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