001    package net.minecraft.entity.effect;
002    
003    import cpw.mods.fml.relauncher.Side;
004    import cpw.mods.fml.relauncher.SideOnly;
005    import java.util.List;
006    import net.minecraft.block.Block;
007    import net.minecraft.entity.Entity;
008    import net.minecraft.nbt.NBTTagCompound;
009    import net.minecraft.util.AxisAlignedBB;
010    import net.minecraft.util.MathHelper;
011    import net.minecraft.util.Vec3;
012    import net.minecraft.world.World;
013    
014    public 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 var8 = MathHelper.floor_double(par2);
042                int var9 = MathHelper.floor_double(par4);
043                int var10 = MathHelper.floor_double(par6);
044    
045                if (par1World.getBlockId(var8, var9, var10) == 0 && Block.fire.canPlaceBlockAt(par1World, var8, var9, var10))
046                {
047                    par1World.setBlockWithNotify(var8, var9, var10, Block.fire.blockID);
048                }
049    
050                for (var8 = 0; var8 < 4; ++var8)
051                {
052                    var9 = MathHelper.floor_double(par2) + this.rand.nextInt(3) - 1;
053                    var10 = MathHelper.floor_double(par4) + this.rand.nextInt(3) - 1;
054                    int var11 = MathHelper.floor_double(par6) + this.rand.nextInt(3) - 1;
055    
056                    if (par1World.getBlockId(var9, var10, var11) == 0 && Block.fire.canPlaceBlockAt(par1World, var9, var10, var11))
057                    {
058                        par1World.setBlockWithNotify(var9, var10, var11, 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 var1 = MathHelper.floor_double(this.posX);
094                        int var2 = MathHelper.floor_double(this.posY);
095                        int var3 = MathHelper.floor_double(this.posZ);
096    
097                        if (this.worldObj.getBlockId(var1, var2, var3) == 0 && Block.fire.canPlaceBlockAt(this.worldObj, var1, var2, var3))
098                        {
099                            this.worldObj.setBlockWithNotify(var1, var2, var3, 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 var6 = 3.0D;
114                    List var7 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, AxisAlignedBB.getAABBPool().addOrModifyAABBInPool(this.posX - var6, this.posY - var6, this.posZ - var6, this.posX + var6, this.posY + 6.0D + var6, this.posZ + var6));
115    
116                    for (int var4 = 0; var4 < var7.size(); ++var4)
117                    {
118                        Entity var5 = (Entity)var7.get(var4);
119                        var5.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    }