    001package net.minecraft.item;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import net.minecraft.block.BlockDispenser;
    006import net.minecraft.client.renderer.texture.IconRegister;
    007import net.minecraft.creativetab.CreativeTabs;
    008import net.minecraft.dispenser.IBehaviorDispenseItem;
    009import net.minecraft.entity.EntityLiving;
    010import net.minecraft.entity.player.EntityPlayer;
    011import net.minecraft.nbt.NBTTagCompound;
    012import net.minecraft.util.Icon;
    013import net.minecraft.world.World;
    014
    015public class ItemArmor extends Item
    016{
    017    /** Holds the 'base' maxDamage that each armorType have. */
    018    private static final int[] maxDamageArray = new int[] {11, 16, 15, 13};
    019    private static final String[] field_94606_cu = new String[] {"helmetCloth_overlay", "chestplateCloth_overlay", "leggingsCloth_overlay", "bootsCloth_overlay"};
    020    public static final String[] field_94603_a = new String[] {"slot_empty_helmet", "slot_empty_chestplate", "slot_empty_leggings", "slot_empty_boots"};
    021    private static final IBehaviorDispenseItem field_96605_cw = new BehaviorDispenseArmor();
    022
    023    /**
    024     * Stores the armor type: 0 is helmet, 1 is plate, 2 is legs and 3 is boots
    025     */
    026    public final int armorType;
    027
    028    /** Holds the amount of damage that the armor reduces at full durability. */
    029    public final int damageReduceAmount;
    030
    031    /**
    032     * Used on RenderPlayer to select the correspondent armor to be rendered on the player: 0 is cloth, 1 is chain, 2 is
    033     * iron, 3 is diamond and 4 is gold.
    034     */
    035    public final int renderIndex;
    036
    037    /** The EnumArmorMaterial used for this ItemArmor */
    038    private final EnumArmorMaterial material;
    039    @SideOnly(Side.CLIENT)
    040    private Icon field_94605_cw;
    041    @SideOnly(Side.CLIENT)
    042    private Icon field_94604_cx;
    043
    044    public ItemArmor(int par1, EnumArmorMaterial par2EnumArmorMaterial, int par3, int par4)
    045    {
    046        super(par1);
    047        this.material = par2EnumArmorMaterial;
    048        this.armorType = par4;
    049        this.renderIndex = par3;
    050        this.damageReduceAmount = par2EnumArmorMaterial.getDamageReductionAmount(par4);
    051        this.setMaxDamage(par2EnumArmorMaterial.getDurability(par4));
    052        this.maxStackSize = 1;
    053        this.setCreativeTab(CreativeTabs.tabCombat);
    054        BlockDispenser.dispenseBehaviorRegistry.putObject(this, field_96605_cw);
    055    }
    056
    057    @SideOnly(Side.CLIENT)
    058    public int getColorFromItemStack(ItemStack par1ItemStack, int par2)
    059    {
    060        if (par2 > 0)
    061        {
    062            return 16777215;
    063        }
    064        else
    065        {
    066            int j = this.getColor(par1ItemStack);
    067
    068            if (j < 0)
    069            {
    070                j = 16777215;
    071            }
    072
    073            return j;
    074        }
    075    }
    076
    077    @SideOnly(Side.CLIENT)
    078    public boolean requiresMultipleRenderPasses()
    079    {
    080        return this.material == EnumArmorMaterial.CLOTH;
    081    }
    082
    083    /**
    084     * Return the enchantability factor of the item, most of the time is based on material.
    085     */
    086    public int getItemEnchantability()
    087    {
    088        return this.material.getEnchantability();
    089    }
    090
    091    /**
    092     * Return the armor material for this armor item.
    093     */
    094    public EnumArmorMaterial getArmorMaterial()
    095    {
    096        return this.material;
    097    }
    098
    099    /**
    100     * Return whether the specified armor ItemStack has a color.
    101     */
    102    public boolean hasColor(ItemStack par1ItemStack)
    103    {
    104        return this.material != EnumArmorMaterial.CLOTH ? false : (!par1ItemStack.hasTagCompound() ? false : (!par1ItemStack.getTagCompound().hasKey("display") ? false : par1ItemStack.getTagCompound().getCompoundTag("display").hasKey("color")));
    105    }
    106
    107    /**
    108     * Return the color for the specified armor ItemStack.
    109     */
    110    public int getColor(ItemStack par1ItemStack)
    111    {
    112        if (this.material != EnumArmorMaterial.CLOTH)
    113        {
    114            return -1;
    115        }
    116        else
    117        {
    118            NBTTagCompound nbttagcompound = par1ItemStack.getTagCompound();
    119
    120            if (nbttagcompound == null)
    121            {
    122                return 10511680;
    123            }
    124            else
    125            {
    126                NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("display");
    127                return nbttagcompound1 == null ? 10511680 : (nbttagcompound1.hasKey("color") ? nbttagcompound1.getInteger("color") : 10511680);
    128            }
    129        }
    130    }
    131
    132    @SideOnly(Side.CLIENT)
    133
    134    /**
    135     * Gets an icon index based on an item's damage value and the given render pass
    136     */
    137    public Icon getIconFromDamageForRenderPass(int par1, int par2)
    138    {
    139        return par2 == 1 ? this.field_94605_cw : super.getIconFromDamageForRenderPass(par1, par2);
    140    }
    141
    142    /**
    143     * Remove the color from the specified armor ItemStack.
    144     */
    145    public void removeColor(ItemStack par1ItemStack)
    146    {
    147        if (this.material == EnumArmorMaterial.CLOTH)
    148        {
    149            NBTTagCompound nbttagcompound = par1ItemStack.getTagCompound();
    150
    151            if (nbttagcompound != null)
    152            {
    153                NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("display");
    154
    155                if (nbttagcompound1.hasKey("color"))
    156                {
    157                    nbttagcompound1.removeTag("color");
    158                }
    159            }
    160        }
    161    }
    162
    163    public void func_82813_b(ItemStack par1ItemStack, int par2)
    164    {
    165        if (this.material != EnumArmorMaterial.CLOTH)
    166        {
    167            throw new UnsupportedOperationException("Can\'t dye non-leather!");
    168        }
    169        else
    170        {
    171            NBTTagCompound nbttagcompound = par1ItemStack.getTagCompound();
    172
    173            if (nbttagcompound == null)
    174            {
    175                nbttagcompound = new NBTTagCompound();
    176                par1ItemStack.setTagCompound(nbttagcompound);
    177            }
    178
    179            NBTTagCompound nbttagcompound1 = nbttagcompound.getCompoundTag("display");
    180
    181            if (!nbttagcompound.hasKey("display"))
    182            {
    183                nbttagcompound.setCompoundTag("display", nbttagcompound1);
    184            }
    185
    186            nbttagcompound1.setInteger("color", par2);
    187        }
    188    }
    189
    190    /**
    191     * Return whether this item is repairable in an anvil.
    192     */
    193    public boolean getIsRepairable(ItemStack par1ItemStack, ItemStack par2ItemStack)
    194    {
    195        return this.material.getArmorCraftingMaterial() == par2ItemStack.itemID ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
    196    }
    197
    198    @SideOnly(Side.CLIENT)
    199    public void registerIcons(IconRegister par1IconRegister)
    200    {
    201        super.registerIcons(par1IconRegister);
    202
    203        if (this.material == EnumArmorMaterial.CLOTH)
    204        {
    205            this.field_94605_cw = par1IconRegister.registerIcon(field_94606_cu[this.armorType]);
    206        }
    207
    208        this.field_94604_cx = par1IconRegister.registerIcon(field_94603_a[this.armorType]);
    209    }
    210
    211    /**
    212     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
    213     */
    214    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    215    {
    216        int i = EntityLiving.getArmorPosition(par1ItemStack) - 1;
    217        ItemStack itemstack1 = par3EntityPlayer.getCurrentArmor(i);
    218
    219        if (itemstack1 == null)
    220        {
    221            par3EntityPlayer.setCurrentItemOrArmor(i + 1, par1ItemStack.copy()); //Forge: Vanilla bug fix associated with fixed setCurrentItemOrArmor indexs for players.
    222            par1ItemStack.stackSize = 0;
    223        }
    224
    225        return par1ItemStack;
    226    }
    227
    228    @SideOnly(Side.CLIENT)
    229    public static Icon func_94602_b(int par0)
    230    {
    231        switch (par0)
    232        {
    233            case 0:
    234                return Item.helmetDiamond.field_94604_cx;
    235            case 1:
    236                return Item.plateDiamond.field_94604_cx;
    237            case 2:
    238                return Item.legsDiamond.field_94604_cx;
    239            case 3:
    240                return Item.bootsDiamond.field_94604_cx;
    241            default:
    242                return null;
    243        }
    244    }
    245
    246    /**
    247     * Returns the 'max damage' factor array for the armor, each piece of armor have a durability factor (that gets
    248     * multiplied by armor material factor)
    249     */
    250    static int[] getMaxDamageArray()
    251    {
    252        return maxDamageArray;
    253    }
    254}