001package net.minecraft.item;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import net.minecraft.client.renderer.texture.IconRegister;
006import net.minecraft.creativetab.CreativeTabs;
007import net.minecraft.enchantment.Enchantment;
008import net.minecraft.enchantment.EnchantmentHelper;
009import net.minecraft.entity.player.EntityPlayer;
010import net.minecraft.entity.projectile.EntityArrow;
011import net.minecraft.util.Icon;
012import net.minecraft.world.World;
013
014import net.minecraftforge.common.MinecraftForge;
015import net.minecraftforge.event.entity.player.ArrowLooseEvent;
016import net.minecraftforge.event.entity.player.ArrowNockEvent;
017
018public class ItemBow extends Item
019{
020    public static final String[] field_94601_a = new String[] {"bow_pull_0", "bow_pull_1", "bow_pull_2"};
021    @SideOnly(Side.CLIENT)
022    private Icon[] field_94600_b;
023
024    public ItemBow(int par1)
025    {
026        super(par1);
027        this.maxStackSize = 1;
028        this.setMaxDamage(384);
029        this.setCreativeTab(CreativeTabs.tabCombat);
030    }
031
032    /**
033     * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
034     */
035    public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
036    {
037        int j = this.getMaxItemUseDuration(par1ItemStack) - par4;
038
039        ArrowLooseEvent event = new ArrowLooseEvent(par3EntityPlayer, par1ItemStack, j);
040        MinecraftForge.EVENT_BUS.post(event);
041        if (event.isCanceled())
042        {
043            return;
044        }
045        j = event.charge;
046
047        boolean flag = par3EntityPlayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack) > 0;
048
049        if (flag || par3EntityPlayer.inventory.hasItem(Item.arrow.itemID))
050        {
051            float f = (float)j / 20.0F;
052            f = (f * f + f * 2.0F) / 3.0F;
053
054            if ((double)f < 0.1D)
055            {
056                return;
057            }
058
059            if (f > 1.0F)
060            {
061                f = 1.0F;
062            }
063
064            EntityArrow entityarrow = new EntityArrow(par2World, par3EntityPlayer, f * 2.0F);
065
066            if (f == 1.0F)
067            {
068                entityarrow.setIsCritical(true);
069            }
070
071            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);
072
073            if (k > 0)
074            {
075                entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
076            }
077
078            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, par1ItemStack);
079
080            if (l > 0)
081            {
082                entityarrow.setKnockbackStrength(l);
083            }
084
085            if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, par1ItemStack) > 0)
086            {
087                entityarrow.setFire(100);
088            }
089
090            par1ItemStack.damageItem(1, par3EntityPlayer);
091            par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);
092
093            if (flag)
094            {
095                entityarrow.canBePickedUp = 2;
096            }
097            else
098            {
099                par3EntityPlayer.inventory.consumeInventoryItem(Item.arrow.itemID);
100            }
101
102            if (!par2World.isRemote)
103            {
104                par2World.spawnEntityInWorld(entityarrow);
105            }
106        }
107    }
108
109    public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
110    {
111        return par1ItemStack;
112    }
113
114    /**
115     * How long it takes to use or consume an item
116     */
117    public int getMaxItemUseDuration(ItemStack par1ItemStack)
118    {
119        return 72000;
120    }
121
122    /**
123     * returns the action that specifies what animation to play when the items is being used
124     */
125    public EnumAction getItemUseAction(ItemStack par1ItemStack)
126    {
127        return EnumAction.bow;
128    }
129
130    /**
131     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
132     */
133    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
134    {
135        ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);
136        MinecraftForge.EVENT_BUS.post(event);
137        if (event.isCanceled())
138        {
139            return event.result;
140        }
141
142        if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(Item.arrow.itemID))
143        {
144            par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
145        }
146
147        return par1ItemStack;
148    }
149
150    /**
151     * Return the enchantability factor of the item, most of the time is based on material.
152     */
153    public int getItemEnchantability()
154    {
155        return 1;
156    }
157
158    @SideOnly(Side.CLIENT)
159    public void func_94581_a(IconRegister par1IconRegister)
160    {
161        super.func_94581_a(par1IconRegister);
162        this.field_94600_b = new Icon[field_94601_a.length];
163
164        for (int i = 0; i < this.field_94600_b.length; ++i)
165        {
166            this.field_94600_b[i] = par1IconRegister.func_94245_a(field_94601_a[i]);
167        }
168    }
169
170    @SideOnly(Side.CLIENT)
171    public Icon func_94599_c(int par1)
172    {
173        return this.field_94600_b[par1];
174    }
175}