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