| 1 | package net.minecraft.entity.effect;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.block.material.Material;
|
| 5 | import net.minecraft.entity.Entity;
|
| 6 | import net.minecraft.init.Blocks;
|
| 7 | import net.minecraft.nbt.NBTTagCompound;
|
| 8 | import net.minecraft.util.AxisAlignedBB;
|
| 9 | import net.minecraft.util.BlockPos;
|
| 10 | import net.minecraft.world.EnumDifficulty;
|
| 11 | import net.minecraft.world.World;
|
| 12 |
|
| 13 | public class EntityLightningBolt extends EntityWeatherEffect
|
| 14 | {
|
| 15 | /**
|
| 16 | * Declares which state the lightning bolt is in. Whether it's in the air, hit the ground, etc.
|
| 17 | */
|
| 18 | private int lightningState;
|
| 19 |
|
| 20 | /**
|
| 21 | * A random long that is used to change the vertex of the lightning rendered in RenderLightningBolt
|
| 22 | */
|
| 23 | public long boltVertex;
|
| 24 |
|
| 25 | /**
|
| 26 | * Determines the time before the EntityLightningBolt is destroyed. It is a random integer decremented over time.
|
| 27 | */
|
| 28 | private int boltLivingTime;
|
| 29 |
|
| 30 | public EntityLightningBolt(World worldIn, double posX, double posY, double posZ)
|
| 31 | {
|
| 32 | super(worldIn);
|
| 33 | this.setLocationAndAngles(posX, posY, posZ, 0.0F, 0.0F);
|
| 34 | this.lightningState = 2;
|
| 35 | this.boltVertex = this.rand.nextLong();
|
| 36 | this.boltLivingTime = this.rand.nextInt(3) + 1;
|
| 37 | BlockPos blockpos = new BlockPos(this);
|
| 38 |
|
| 39 | if (!worldIn.isRemote && worldIn.getGameRules().getBoolean("doFireTick") && (worldIn.getDifficulty() == EnumDifficulty.NORMAL || worldIn.getDifficulty() == EnumDifficulty.HARD) && worldIn.isAreaLoaded(blockpos, 10))
|
| 40 | {
|
| 41 | if (worldIn.getBlockState(blockpos).getBlock().getMaterial() == Material.air && Blocks.fire.canPlaceBlockAt(worldIn, blockpos))
|
| 42 | {
|
| 43 | worldIn.setBlockState(blockpos, Blocks.fire.getDefaultState());
|
| 44 | }
|
| 45 |
|
| 46 | for (int i = 0; i < 4; ++i)
|
| 47 | {
|
| 48 | BlockPos blockpos1 = blockpos.add(this.rand.nextInt(3) - 1, this.rand.nextInt(3) - 1, this.rand.nextInt(3) - 1);
|
| 49 |
|
| 50 | if (worldIn.getBlockState(blockpos1).getBlock().getMaterial() == Material.air && Blocks.fire.canPlaceBlockAt(worldIn, blockpos1))
|
| 51 | {
|
| 52 | worldIn.setBlockState(blockpos1, Blocks.fire.getDefaultState());
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | /**
|
| 59 | * Called to update the entity's position/logic.
|
| 60 | */
|
| 61 | public void onUpdate()
|
| 62 | {
|
| 63 | super.onUpdate();
|
| 64 |
|
| 65 | if (this.lightningState == 2)
|
| 66 | {
|
| 67 | this.worldObj.playSoundEffect(this.posX, this.posY, this.posZ, "ambient.weather.thunder", 10000.0F, 0.8F + this.rand.nextFloat() * 0.2F);
|
| 68 | this.worldObj.playSoundEffect(this.posX, this.posY, this.posZ, "random.explode", 2.0F, 0.5F + this.rand.nextFloat() * 0.2F);
|
| 69 | }
|
| 70 |
|
| 71 | --this.lightningState;
|
| 72 |
|
| 73 | if (this.lightningState < 0)
|
| 74 | {
|
| 75 | if (this.boltLivingTime == 0)
|
| 76 | {
|
| 77 | this.setDead();
|
| 78 | }
|
| 79 | else if (this.lightningState < -this.rand.nextInt(10))
|
| 80 | {
|
| 81 | --this.boltLivingTime;
|
| 82 | this.lightningState = 1;
|
| 83 | this.boltVertex = this.rand.nextLong();
|
| 84 | BlockPos blockpos = new BlockPos(this);
|
| 85 |
|
| 86 | if (!this.worldObj.isRemote && this.worldObj.getGameRules().getBoolean("doFireTick") && this.worldObj.isAreaLoaded(blockpos, 10) && this.worldObj.getBlockState(blockpos).getBlock().getMaterial() == Material.air && Blocks.fire.canPlaceBlockAt(this.worldObj, blockpos))
|
| 87 | {
|
| 88 | this.worldObj.setBlockState(blockpos, Blocks.fire.getDefaultState());
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | if (this.lightningState >= 0)
|
| 94 | {
|
| 95 | if (this.worldObj.isRemote)
|
| 96 | {
|
| 97 | this.worldObj.setLastLightningBolt(2);
|
| 98 | }
|
| 99 | else
|
| 100 | {
|
| 101 | double d0 = 3.0D;
|
| 102 | List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, new AxisAlignedBB(this.posX - d0, this.posY - d0, this.posZ - d0, this.posX + d0, this.posY + 6.0D + d0, this.posZ + d0));
|
| 103 |
|
| 104 | for (int i = 0; i < list.size(); ++i)
|
| 105 | {
|
| 106 | Entity entity = (Entity)list.get(i);
|
| 107 | entity.onStruckByLightning(this);
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | protected void entityInit()
|
| 114 | {
|
| 115 | }
|
| 116 |
|
| 117 | /**
|
| 118 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 119 | */
|
| 120 | protected void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 121 | {
|
| 122 | }
|
| 123 |
|
| 124 | /**
|
| 125 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 126 | */
|
| 127 | protected void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 128 | {
|
| 129 | }
|
| 130 | }
|
| 131 |
|