001package net.minecraft.item.crafting;
002
003import java.util.ArrayList;
004import net.minecraft.block.BlockCloth;
005import net.minecraft.entity.passive.EntitySheep;
006import net.minecraft.inventory.InventoryCrafting;
007import net.minecraft.item.EnumArmorMaterial;
008import net.minecraft.item.Item;
009import net.minecraft.item.ItemArmor;
010import net.minecraft.item.ItemStack;
011import net.minecraft.world.World;
012
013public class RecipesArmorDyes implements IRecipe
014{
015    /**
016     * Used to check if a recipe matches current crafting inventory
017     */
018    public boolean matches(InventoryCrafting par1InventoryCrafting, World par2World)
019    {
020        ItemStack itemstack = null;
021        ArrayList arraylist = new ArrayList();
022
023        for (int i = 0; i < par1InventoryCrafting.getSizeInventory(); ++i)
024        {
025            ItemStack itemstack1 = par1InventoryCrafting.getStackInSlot(i);
026
027            if (itemstack1 != null)
028            {
029                if (itemstack1.getItem() instanceof ItemArmor)
030                {
031                    ItemArmor itemarmor = (ItemArmor)itemstack1.getItem();
032
033                    if (itemarmor.getArmorMaterial() != EnumArmorMaterial.CLOTH || itemstack != null)
034                    {
035                        return false;
036                    }
037
038                    itemstack = itemstack1;
039                }
040                else
041                {
042                    if (itemstack1.itemID != Item.dyePowder.itemID)
043                    {
044                        return false;
045                    }
046
047                    arraylist.add(itemstack1);
048                }
049            }
050        }
051
052        return itemstack != null && !arraylist.isEmpty();
053    }
054
055    /**
056     * Returns an Item that is the result of this recipe
057     */
058    public ItemStack getCraftingResult(InventoryCrafting par1InventoryCrafting)
059    {
060        ItemStack itemstack = null;
061        int[] aint = new int[3];
062        int i = 0;
063        int j = 0;
064        ItemArmor itemarmor = null;
065        int k;
066        int l;
067        float f;
068        float f1;
069        int i1;
070
071        for (k = 0; k < par1InventoryCrafting.getSizeInventory(); ++k)
072        {
073            ItemStack itemstack1 = par1InventoryCrafting.getStackInSlot(k);
074
075            if (itemstack1 != null)
076            {
077                if (itemstack1.getItem() instanceof ItemArmor)
078                {
079                    itemarmor = (ItemArmor)itemstack1.getItem();
080
081                    if (itemarmor.getArmorMaterial() != EnumArmorMaterial.CLOTH || itemstack != null)
082                    {
083                        return null;
084                    }
085
086                    itemstack = itemstack1.copy();
087                    itemstack.stackSize = 1;
088
089                    if (itemarmor.hasColor(itemstack1))
090                    {
091                        l = itemarmor.getColor(itemstack);
092                        f = (float)(l >> 16 & 255) / 255.0F;
093                        f1 = (float)(l >> 8 & 255) / 255.0F;
094                        float f2 = (float)(l & 255) / 255.0F;
095                        i = (int)((float)i + Math.max(f, Math.max(f1, f2)) * 255.0F);
096                        aint[0] = (int)((float)aint[0] + f * 255.0F);
097                        aint[1] = (int)((float)aint[1] + f1 * 255.0F);
098                        aint[2] = (int)((float)aint[2] + f2 * 255.0F);
099                        ++j;
100                    }
101                }
102                else
103                {
104                    if (itemstack1.itemID != Item.dyePowder.itemID)
105                    {
106                        return null;
107                    }
108
109                    float[] afloat = EntitySheep.fleeceColorTable[BlockCloth.getBlockFromDye(itemstack1.getItemDamage())];
110                    int j1 = (int)(afloat[0] * 255.0F);
111                    int k1 = (int)(afloat[1] * 255.0F);
112                    i1 = (int)(afloat[2] * 255.0F);
113                    i += Math.max(j1, Math.max(k1, i1));
114                    aint[0] += j1;
115                    aint[1] += k1;
116                    aint[2] += i1;
117                    ++j;
118                }
119            }
120        }
121
122        if (itemarmor == null)
123        {
124            return null;
125        }
126        else
127        {
128            k = aint[0] / j;
129            int l1 = aint[1] / j;
130            l = aint[2] / j;
131            f = (float)i / (float)j;
132            f1 = (float)Math.max(k, Math.max(l1, l));
133            k = (int)((float)k * f / f1);
134            l1 = (int)((float)l1 * f / f1);
135            l = (int)((float)l * f / f1);
136            i1 = (k << 8) + l1;
137            i1 = (i1 << 8) + l;
138            itemarmor.func_82813_b(itemstack, i1);
139            return itemstack;
140        }
141    }
142
143    /**
144     * Returns the size of the recipe area
145     */
146    public int getRecipeSize()
147    {
148        return 10;
149    }
150
151    public ItemStack getRecipeOutput()
152    {
153        return null;
154    }
155}