package net.battlefield;

import java.util.ArrayList;

import cpw.mods.fml.common.Side;
import cpw.mods.fml.common.asm.SideOnly;

import net.minecraft.client.Minecraft;
import net.minecraft.src.CreativeTabs;
import net.minecraft.src.DamageSource;
import net.minecraft.src.Entity;
import net.minecraft.src.EntityLiving;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ModLoader;
import net.minecraft.src.World;

public class ItemLauncher extends Item
{
	public ItemLauncher(int x)
	{
		super(x);
		maxStackSize = 1;
		setMaxDamage(50);
		this.setCreativeTab(CreativeTabs.tabCombat);
	}
	
	@Override
	  public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag)
	  {
	    if (itemstack.getItemDamage() > 0)
	    {
	      itemstack.damageItem(-1, (EntityLiving)entity);
	    }
	  }
	

    public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
    {
	     	if (entityplayer.inventory.hasItem(Battlefield.Rocket.shiftedIndex))
	        {
	    		if (!world.isRemote) {
		    		EntityRocket entityplasma = new EntityRocket(world, entityplayer, 3);
		    		entityplayer.inventory.consumeInventoryItem(Battlefield.Rocket.shiftedIndex);
	    			world.spawnEntityInWorld(entityplasma);
	    			//itemstack.setItemDamage(49);
	    		} else {
	    			this.onItemRightClickClient(itemstack, world, entityplayer);
	    		}
	        }
     	return itemstack;
    }
    
    @SideOnly(Side.CLIENT)
    public void onItemRightClickClient(ItemStack itemstack, World world, EntityPlayer entityplayer)
    {
    	if (entityplayer.inventory.hasItem(Battlefield.Rocket.shiftedIndex))
	    {
		    itemstack.damageItem(6, entityplayer);
		    EntityRocket entityplasma = new EntityRocket(world, entityplayer, 3);
		    entityplayer.inventory.consumeInventoryItem(Battlefield.Rocket.shiftedIndex);
		    if (!world.isRemote)
            {
            	    Minecraft mc = Minecraft.getMinecraft();
                    mc.sndManager.playSoundFX("bfsounds.Rocket", 1.0F, 1.0F);
            }
		    {
                	Minecraft mc = Minecraft.getMinecraft();
                    mc.sndManager.playSoundFX("bfsounds.rocket", 1.0F, 1.0F);
                }
				world.spawnEntityInWorld(entityplasma);
			};
		}
    
	public String getTextureFile()
	{
		return "/battlefield/items.png";
	}
	
	public void addCreativeItems(ArrayList itemList)
	{
		itemList.add(new ItemStack(this));
	}
}
