    001package net.minecraft.entity.item;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import net.minecraft.block.Block;
    006import net.minecraft.block.BlockRailBase;
    007import net.minecraft.item.ItemStack;
    008import net.minecraft.nbt.NBTTagCompound;
    009import net.minecraft.util.DamageSource;
    010import net.minecraft.world.Explosion;
    011import net.minecraft.world.World;
    012
    013public class EntityMinecartTNT extends EntityMinecart
    014{
    015    private int field_94106_a = -1;
    016
    017    public EntityMinecartTNT(World par1)
    018    {
    019        super(par1);
    020    }
    021
    022    public EntityMinecartTNT(World par1, double par2, double par4, double par6)
    023    {
    024        super(par1, par2, par4, par6);
    025    }
    026
    027    public int func_94087_l()
    028    {
    029        return 3;
    030    }
    031
    032    public Block func_94093_n()
    033    {
    034        return Block.tnt;
    035    }
    036
    037    /**
    038     * Called to update the entity's position/logic.
    039     */
    040    public void onUpdate()
    041    {
    042        super.onUpdate();
    043
    044        if (this.field_94106_a > 0)
    045        {
    046            --this.field_94106_a;
    047            this.worldObj.spawnParticle("smoke", this.posX, this.posY + 0.5D, this.posZ, 0.0D, 0.0D, 0.0D);
    048        }
    049        else if (this.field_94106_a == 0)
    050        {
    051            this.func_94103_c(this.motionX * this.motionX + this.motionZ * this.motionZ);
    052        }
    053
    054        if (this.isCollidedHorizontally)
    055        {
    056            double d0 = this.motionX * this.motionX + this.motionZ * this.motionZ;
    057
    058            if (d0 >= 0.009999999776482582D)
    059            {
    060                this.func_94103_c(d0);
    061            }
    062        }
    063    }
    064
    065    public void func_94095_a(DamageSource par1DamageSource)
    066    {
    067        super.func_94095_a(par1DamageSource);
    068        double d0 = this.motionX * this.motionX + this.motionZ * this.motionZ;
    069
    070        if (!par1DamageSource.func_94541_c())
    071        {
    072            this.entityDropItem(new ItemStack(Block.tnt, 1), 0.0F);
    073        }
    074
    075        if (par1DamageSource.isFireDamage() || par1DamageSource.func_94541_c() || d0 >= 0.009999999776482582D)
    076        {
    077            this.func_94103_c(d0);
    078        }
    079    }
    080
    081    protected void func_94103_c(double par1)
    082    {
    083        if (!this.worldObj.isRemote)
    084        {
    085            double d1 = Math.sqrt(par1);
    086
    087            if (d1 > 5.0D)
    088            {
    089                d1 = 5.0D;
    090            }
    091
    092            this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ, (float)(4.0D + this.rand.nextDouble() * 1.5D * d1), true);
    093            this.setDead();
    094        }
    095    }
    096
    097    /**
    098     * Called when the mob is falling. Calculates and applies fall damage.
    099     */
    100    protected void fall(float par1)
    101    {
    102        if (par1 >= 3.0F)
    103        {
    104            float f1 = par1 / 10.0F;
    105            this.func_94103_c((double)(f1 * f1));
    106        }
    107
    108        super.fall(par1);
    109    }
    110
    111    public void func_96095_a(int par1, int par2, int par3, boolean par4)
    112    {
    113        if (par4 && this.field_94106_a < 0)
    114        {
    115            this.func_94105_c();
    116        }
    117    }
    118
    119    @SideOnly(Side.CLIENT)
    120    public void handleHealthUpdate(byte par1)
    121    {
    122        if (par1 == 10)
    123        {
    124            this.func_94105_c();
    125        }
    126        else
    127        {
    128            super.handleHealthUpdate(par1);
    129        }
    130    }
    131
    132    public void func_94105_c()
    133    {
    134        this.field_94106_a = 80;
    135
    136        if (!this.worldObj.isRemote)
    137        {
    138            this.worldObj.setEntityState(this, (byte)10);
    139            this.worldObj.playSoundAtEntity(this, "random.fuse", 1.0F, 1.0F);
    140        }
    141    }
    142
    143    @SideOnly(Side.CLIENT)
    144    public int func_94104_d()
    145    {
    146        return this.field_94106_a;
    147    }
    148
    149    public boolean func_96096_ay()
    150    {
    151        return this.field_94106_a > -1;
    152    }
    153
    154    public float func_82146_a(Explosion par1Explosion, World par2World, int par3, int par4, int par5, Block par6Block)
    155    {
    156        return this.func_96096_ay() && (BlockRailBase.isRailBlock(par6Block.blockID) || BlockRailBase.isRailBlockAt(par2World, par3, par4 + 1, par5)) ? 0.0F : super.func_82146_a(par1Explosion, par2World, par3, par4, par5, par6Block);
    157    }
    158
    159    public boolean func_96091_a(Explosion par1Explosion, World par2World, int par3, int par4, int par5, int par6, float par7)
    160    {
    161        return this.func_96096_ay() && (BlockRailBase.isRailBlock(par6) || BlockRailBase.isRailBlockAt(par2World, par3, par4 + 1, par5)) ? false : super.func_96091_a(par1Explosion, par2World, par3, par4, par5, par6, par7);
    162    }
    163
    164    /**
    165     * (abstract) Protected helper method to read subclass entity data from NBT.
    166     */
    167    protected void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)
    168    {
    169        super.readEntityFromNBT(par1NBTTagCompound);
    170
    171        if (par1NBTTagCompound.hasKey("TNTFuse"))
    172        {
    173            this.field_94106_a = par1NBTTagCompound.getInteger("TNTFuse");
    174        }
    175    }
    176
    177    /**
    178     * (abstract) Protected helper method to write subclass entity data to NBT.
    179     */
    180    protected void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)
    181    {
    182        super.writeEntityToNBT(par1NBTTagCompound);
    183        par1NBTTagCompound.setInteger("TNTFuse", this.field_94106_a);
    184    }
    185}