package common.entity.projectile;

import common.entity.DamageSource;
import common.entity.EntityType;
import common.entity.types.EntityLiving;
import common.entity.types.EntityThrowable;
import common.entity.types.IObjectData;
import common.init.Config;
import common.init.ItemRegistry;
import common.init.Items;
import common.model.ParticleType;
import common.nbt.NBTTagCompound;
import common.util.HitPosition;
import common.world.World;

public class EntityDynamite extends EntityThrowable implements IObjectData
{
	public int explosionSize;
	
    public EntityDynamite(World worldIn)
    {
        super(worldIn);
    }

    public EntityDynamite(World worldIn, EntityLiving throwerIn, int power)
    {
        super(worldIn, throwerIn);
        this.explosionSize = power & 7;
    }

    public EntityDynamite(World worldIn, double x, double y, double z, int power)
    {
        super(worldIn, x, y, z);
        this.explosionSize = power & 7;
    }
    
    protected void onImpact(HitPosition pos)
    {
        if (pos.entity != null && Config.knockDynamite)
        {
            pos.entity.attackEntityFrom(DamageSource.causeThrownDamage(this, this.getThrower()), 0);
        }

        if (!this.worldObj.client)
        {
        	float f = 4.0F * (((float)this.explosionSize)+1.0f);
        	EntityLiving thrower = this.getThrower();
            this.worldObj.createAltExplosion(thrower == null ? this : thrower, this.posX, this.posY + (double)(this.height / 2.0F), this.posZ, f, true);
        }

        for (int k = 0; k < 8; ++k)
        {
            this.worldObj.spawnParticle(ParticleType.ITEM_CRACK, this.posX, this.posY, this.posZ, ((double)this.rand.floatv() - 0.5D) * 0.08D, ((double)this.rand.floatv() - 0.5D) * 0.08D, ((double)this.rand.floatv() - 0.5D) * 0.08D, ItemRegistry.getIdFromItem(Items.dynamite), this.explosionSize);
        }

        if (!this.worldObj.client)
        {
            this.setDead();
        }
    }
    
    public void writeEntityToNBT(NBTTagCompound tagCompound)
    {
        super.writeEntityToNBT(tagCompound);
        tagCompound.setByte("Power", (byte)this.explosionSize);
    }
    
    public void readEntityFromNBT(NBTTagCompound tagCompund)
    {
        super.readEntityFromNBT(tagCompund);
        this.explosionSize = ((int)tagCompund.getByte("Power")) & 7;
    }
	
	public int getPacketData() {
		return this.explosionSize;
	}
	
//	public boolean hasSpawnVelocity() {
//		return false;
//	}
    
    public EntityType getType() {
    	return EntityType.EXPLOSIVE;
    }
}
