001    package net.minecraft.item;
002    
003    import cpw.mods.fml.relauncher.Side;
004    import cpw.mods.fml.relauncher.SideOnly;
005    import net.minecraft.creativetab.CreativeTabs;
006    import net.minecraft.nbt.NBTTagCompound;
007    
008    public class ItemArmor extends Item
009    {
010        /** Holds the 'base' maxDamage that each armorType have. */
011        private static final int[] maxDamageArray = new int[] {11, 16, 15, 13};
012    
013        /**
014         * Stores the armor type: 0 is helmet, 1 is plate, 2 is legs and 3 is boots
015         */
016        public final int armorType;
017    
018        /** Holds the amount of damage that the armor reduces at full durability. */
019        public final int damageReduceAmount;
020    
021        /**
022         * Used on RenderPlayer to select the correspondent armor to be rendered on the player: 0 is cloth, 1 is chain, 2 is
023         * iron, 3 is diamond and 4 is gold.
024         */
025        public final int renderIndex;
026    
027        /** The EnumArmorMaterial used for this ItemArmor */
028        private final EnumArmorMaterial material;
029    
030        public ItemArmor(int par1, EnumArmorMaterial par2EnumArmorMaterial, int par3, int par4)
031        {
032            super(par1);
033            this.material = par2EnumArmorMaterial;
034            this.armorType = par4;
035            this.renderIndex = par3;
036            this.damageReduceAmount = par2EnumArmorMaterial.getDamageReductionAmount(par4);
037            this.setMaxDamage(par2EnumArmorMaterial.getDurability(par4));
038            this.maxStackSize = 1;
039            this.setCreativeTab(CreativeTabs.tabCombat);
040        }
041    
042        @SideOnly(Side.CLIENT)
043        public int getColorFromItemStack(ItemStack par1ItemStack, int par2)
044        {
045            if (par2 > 0)
046            {
047                return 16777215;
048            }
049            else
050            {
051                int var3 = this.getColor(par1ItemStack);
052    
053                if (var3 < 0)
054                {
055                    var3 = 16777215;
056                }
057    
058                return var3;
059            }
060        }
061    
062        @SideOnly(Side.CLIENT)
063        public boolean requiresMultipleRenderPasses()
064        {
065            return this.material == EnumArmorMaterial.CLOTH;
066        }
067    
068        /**
069         * Return the enchantability factor of the item, most of the time is based on material.
070         */
071        public int getItemEnchantability()
072        {
073            return this.material.getEnchantability();
074        }
075    
076        /**
077         * Return the armor material for this armor item.
078         */
079        public EnumArmorMaterial getArmorMaterial()
080        {
081            return this.material;
082        }
083    
084        /**
085         * Return whether the specified armor ItemStack has a color.
086         */
087        public boolean hasColor(ItemStack par1ItemStack)
088        {
089            return this.material != EnumArmorMaterial.CLOTH ? false : (!par1ItemStack.hasTagCompound() ? false : (!par1ItemStack.getTagCompound().hasKey("display") ? false : par1ItemStack.getTagCompound().getCompoundTag("display").hasKey("color")));
090        }
091    
092        /**
093         * Return the color for the specified armor ItemStack.
094         */
095        public int getColor(ItemStack par1ItemStack)
096        {
097            if (this.material != EnumArmorMaterial.CLOTH)
098            {
099                return -1;
100            }
101            else
102            {
103                NBTTagCompound var2 = par1ItemStack.getTagCompound();
104    
105                if (var2 == null)
106                {
107                    return 10511680;
108                }
109                else
110                {
111                    NBTTagCompound var3 = var2.getCompoundTag("display");
112                    return var3 == null ? 10511680 : (var3.hasKey("color") ? var3.getInteger("color") : 10511680);
113                }
114            }
115        }
116    
117        @SideOnly(Side.CLIENT)
118    
119        /**
120         * Gets an icon index based on an item's damage value and the given render pass
121         */
122        public int getIconFromDamageForRenderPass(int par1, int par2)
123        {
124            return par2 == 1 ? this.iconIndex + 144 : super.getIconFromDamageForRenderPass(par1, par2);
125        }
126    
127        /**
128         * Remove the color from the specified armor ItemStack.
129         */
130        public void removeColor(ItemStack par1ItemStack)
131        {
132            if (this.material == EnumArmorMaterial.CLOTH)
133            {
134                NBTTagCompound var2 = par1ItemStack.getTagCompound();
135    
136                if (var2 != null)
137                {
138                    NBTTagCompound var3 = var2.getCompoundTag("display");
139    
140                    if (var3.hasKey("color"))
141                    {
142                        var3.removeTag("color");
143                    }
144                }
145            }
146        }
147    
148        public void func_82813_b(ItemStack par1ItemStack, int par2)
149        {
150            if (this.material != EnumArmorMaterial.CLOTH)
151            {
152                throw new UnsupportedOperationException("Can\'t dye non-leather!");
153            }
154            else
155            {
156                NBTTagCompound var3 = par1ItemStack.getTagCompound();
157    
158                if (var3 == null)
159                {
160                    var3 = new NBTTagCompound();
161                    par1ItemStack.setTagCompound(var3);
162                }
163    
164                NBTTagCompound var4 = var3.getCompoundTag("display");
165    
166                if (!var3.hasKey("display"))
167                {
168                    var3.setCompoundTag("display", var4);
169                }
170    
171                var4.setInteger("color", par2);
172            }
173        }
174    
175        /**
176         * Return whether this item is repairable in an anvil.
177         */
178        public boolean getIsRepairable(ItemStack par1ItemStack, ItemStack par2ItemStack)
179        {
180            return this.material.getArmorCraftingMaterial() == par2ItemStack.itemID ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
181        }
182    
183        /**
184         * Returns the 'max damage' factor array for the armor, each piece of armor have a durability factor (that gets
185         * multiplied by armor material factor)
186         */
187        static int[] getMaxDamageArray()
188        {
189            return maxDamageArray;
190        }
191    }