001package net.minecraft.entity.effect;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.List;
006import net.minecraft.block.Block;
007import net.minecraft.entity.Entity;
008import net.minecraft.nbt.NBTTagCompound;
009import net.minecraft.util.AxisAlignedBB;
010import net.minecraft.util.MathHelper;
011import net.minecraft.util.Vec3;
012import net.minecraft.world.World;
013
014public class EntityLightningBolt extends EntityWeatherEffect
015{
016    /**
017     * Declares which state the lightning bolt is in. Whether it's in the air, hit the ground, etc.
018     */
019    private int lightningState;
020
021    /**
022     * A random long that is used to change the vertex of the lightning rendered in RenderLightningBolt
023     */
024    public long boltVertex = 0L;
025
026    /**
027     * Determines the time before the EntityLightningBolt is destroyed. It is a random integer decremented over time.
028     */
029    private int boltLivingTime;
030
031    public EntityLightningBolt(World par1World, double par2, double par4, double par6)
032    {
033        super(par1World);
034        this.setLocationAndAngles(par2, par4, par6, 0.0F, 0.0F);
035        this.lightningState = 2;
036        this.boltVertex = this.rand.nextLong();
037        this.boltLivingTime = this.rand.nextInt(3) + 1;
038
039        if (!par1World.isRemote && par1World.difficultySetting >= 2 && par1World.doChunksNearChunkExist(MathHelper.floor_double(par2), MathHelper.floor_double(par4), MathHelper.floor_double(par6), 10))
040        {
041            int i = MathHelper.floor_double(par2);
042            int j = MathHelper.floor_double(par4);
043            int k = MathHelper.floor_double(par6);
044
045            if (par1World.getBlockId(i, j, k) == 0 && Block.fire.canPlaceBlockAt(par1World, i, j, k))
046            {
047                par1World.func_94575_c(i, j, k, Block.fire.blockID);
048            }
049
050            for (i = 0; i < 4; ++i)
051            {
052                j = MathHelper.floor_double(par2) + this.rand.nextInt(3) - 1;
053                k = MathHelper.floor_double(par4) + this.rand.nextInt(3) - 1;
054                int l = MathHelper.floor_double(par6) + this.rand.nextInt(3) - 1;
055
056                if (par1World.getBlockId(j, k, l) == 0 && Block.fire.canPlaceBlockAt(par1World, j, k, l))
057                {
058                    par1World.func_94575_c(j, k, l, Block.fire.blockID);
059                }
060            }
061        }
062    }
063
064    /**
065     * Called to update the entity's position/logic.
066     */
067    public void onUpdate()
068    {
069        super.onUpdate();
070
071        if (this.lightningState == 2)
072        {
073            this.worldObj.playSoundEffect(this.posX, this.posY, this.posZ, "ambient.weather.thunder", 10000.0F, 0.8F + this.rand.nextFloat() * 0.2F);
074            this.worldObj.playSoundEffect(this.posX, this.posY, this.posZ, "random.explode", 2.0F, 0.5F + this.rand.nextFloat() * 0.2F);
075        }
076
077        --this.lightningState;
078
079        if (this.lightningState < 0)
080        {
081            if (this.boltLivingTime == 0)
082            {
083                this.setDead();
084            }
085            else if (this.lightningState < -this.rand.nextInt(10))
086            {
087                --this.boltLivingTime;
088                this.lightningState = 1;
089                this.boltVertex = this.rand.nextLong();
090
091                if (!this.worldObj.isRemote && this.worldObj.doChunksNearChunkExist(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY), MathHelper.floor_double(this.posZ), 10))
092                {
093                    int i = MathHelper.floor_double(this.posX);
094                    int j = MathHelper.floor_double(this.posY);
095                    int k = MathHelper.floor_double(this.posZ);
096
097                    if (this.worldObj.getBlockId(i, j, k) == 0 && Block.fire.canPlaceBlockAt(this.worldObj, i, j, k))
098                    {
099                        this.worldObj.func_94575_c(i, j, k, Block.fire.blockID);
100                    }
101                }
102            }
103        }
104
105        if (this.lightningState >= 0)
106        {
107            if (this.worldObj.isRemote)
108            {
109                this.worldObj.lastLightningBolt = 2;
110            }
111            else
112            {
113                double d0 = 3.0D;
114                List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, AxisAlignedBB.getAABBPool().getAABB(this.posX - d0, this.posY - d0, this.posZ - d0, this.posX + d0, this.posY + 6.0D + d0, this.posZ + d0));
115
116                for (int l = 0; l < list.size(); ++l)
117                {
118                    Entity entity = (Entity)list.get(l);
119                    entity.onStruckByLightning(this);
120                }
121            }
122        }
123    }
124
125    protected void entityInit() {}
126
127    /**
128     * (abstract) Protected helper method to read subclass entity data from NBT.
129     */
130    protected void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) {}
131
132    /**
133     * (abstract) Protected helper method to write subclass entity data to NBT.
134     */
135    protected void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) {}
136
137    @SideOnly(Side.CLIENT)
138
139    /**
140     * Checks using a Vec3d to determine if this entity is within range of that vector to be rendered. Args: vec3D
141     */
142    public boolean isInRangeToRenderVec3D(Vec3 par1Vec3)
143    {
144        return this.lightningState >= 0;
145    }
146}