| 1 | package net.minecraft.entity.projectile;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.block.Block;
|
| 5 | import net.minecraft.entity.Entity;
|
| 6 | import net.minecraft.entity.EntityLivingBase;
|
| 7 | import net.minecraft.nbt.NBTTagCompound;
|
| 8 | import net.minecraft.nbt.NBTTagList;
|
| 9 | import net.minecraft.util.AxisAlignedBB;
|
| 10 | import net.minecraft.util.BlockPos;
|
| 11 | import net.minecraft.util.DamageSource;
|
| 12 | import net.minecraft.util.EnumParticleTypes;
|
| 13 | import net.minecraft.util.MathHelper;
|
| 14 | import net.minecraft.util.MovingObjectPosition;
|
| 15 | import net.minecraft.util.ResourceLocation;
|
| 16 | import net.minecraft.util.Vec3;
|
| 17 | import net.minecraft.world.World;
|
| 18 |
|
| 19 | public abstract class EntityFireball extends Entity
|
| 20 | {
|
| 21 | private int xTile = -1;
|
| 22 | private int yTile = -1;
|
| 23 | private int zTile = -1;
|
| 24 | private Block inTile;
|
| 25 | private boolean inGround;
|
| 26 | public EntityLivingBase shootingEntity;
|
| 27 | private int ticksAlive;
|
| 28 | private int ticksInAir;
|
| 29 | public double accelerationX;
|
| 30 | public double accelerationY;
|
| 31 | public double accelerationZ;
|
| 32 |
|
| 33 | public EntityFireball(World worldIn)
|
| 34 | {
|
| 35 | super(worldIn);
|
| 36 | this.setSize(1.0F, 1.0F);
|
| 37 | }
|
| 38 |
|
| 39 | protected void entityInit()
|
| 40 | {
|
| 41 | }
|
| 42 |
|
| 43 | /**
|
| 44 | * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
|
| 45 | * length * 64 * renderDistanceWeight Args: distance
|
| 46 | */
|
| 47 | public boolean isInRangeToRenderDist(double distance)
|
| 48 | {
|
| 49 | double d0 = this.getEntityBoundingBox().getAverageEdgeLength() * 4.0D;
|
| 50 |
|
| 51 | if (Double.isNaN(d0))
|
| 52 | {
|
| 53 | d0 = 4.0D;
|
| 54 | }
|
| 55 |
|
| 56 | d0 = d0 * 64.0D;
|
| 57 | return distance < d0 * d0;
|
| 58 | }
|
| 59 |
|
| 60 | public EntityFireball(World worldIn, double x, double y, double z, double accelX, double accelY, double accelZ)
|
| 61 | {
|
| 62 | super(worldIn);
|
| 63 | this.setSize(1.0F, 1.0F);
|
| 64 | this.setLocationAndAngles(x, y, z, this.rotationYaw, this.rotationPitch);
|
| 65 | this.setPosition(x, y, z);
|
| 66 | double d0 = (double)MathHelper.sqrt_double(accelX * accelX + accelY * accelY + accelZ * accelZ);
|
| 67 | this.accelerationX = accelX / d0 * 0.1D;
|
| 68 | this.accelerationY = accelY / d0 * 0.1D;
|
| 69 | this.accelerationZ = accelZ / d0 * 0.1D;
|
| 70 | }
|
| 71 |
|
| 72 | public EntityFireball(World worldIn, EntityLivingBase shooter, double accelX, double accelY, double accelZ)
|
| 73 | {
|
| 74 | super(worldIn);
|
| 75 | this.shootingEntity = shooter;
|
| 76 | this.setSize(1.0F, 1.0F);
|
| 77 | this.setLocationAndAngles(shooter.posX, shooter.posY, shooter.posZ, shooter.rotationYaw, shooter.rotationPitch);
|
| 78 | this.setPosition(this.posX, this.posY, this.posZ);
|
| 79 | this.motionX = this.motionY = this.motionZ = 0.0D;
|
| 80 | accelX = accelX + this.rand.nextGaussian() * 0.4D;
|
| 81 | accelY = accelY + this.rand.nextGaussian() * 0.4D;
|
| 82 | accelZ = accelZ + this.rand.nextGaussian() * 0.4D;
|
| 83 | double d0 = (double)MathHelper.sqrt_double(accelX * accelX + accelY * accelY + accelZ * accelZ);
|
| 84 | this.accelerationX = accelX / d0 * 0.1D;
|
| 85 | this.accelerationY = accelY / d0 * 0.1D;
|
| 86 | this.accelerationZ = accelZ / d0 * 0.1D;
|
| 87 | }
|
| 88 |
|
| 89 | /**
|
| 90 | * Called to update the entity's position/logic.
|
| 91 | */
|
| 92 | public void onUpdate()
|
| 93 | {
|
| 94 | if (this.worldObj.isRemote || (this.shootingEntity == null || !this.shootingEntity.isDead) && this.worldObj.isBlockLoaded(new BlockPos(this)))
|
| 95 | {
|
| 96 | super.onUpdate();
|
| 97 | this.setFire(1);
|
| 98 |
|
| 99 | if (this.inGround)
|
| 100 | {
|
| 101 | if (this.worldObj.getBlockState(new BlockPos(this.xTile, this.yTile, this.zTile)).getBlock() == this.inTile)
|
| 102 | {
|
| 103 | ++this.ticksAlive;
|
| 104 |
|
| 105 | if (this.ticksAlive == 600)
|
| 106 | {
|
| 107 | this.setDead();
|
| 108 | }
|
| 109 |
|
| 110 | return;
|
| 111 | }
|
| 112 |
|
| 113 | this.inGround = false;
|
| 114 | this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
|
| 115 | this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
|
| 116 | this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
|
| 117 | this.ticksAlive = 0;
|
| 118 | this.ticksInAir = 0;
|
| 119 | }
|
| 120 | else
|
| 121 | {
|
| 122 | ++this.ticksInAir;
|
| 123 | }
|
| 124 |
|
| 125 | Vec3 vec3 = new Vec3(this.posX, this.posY, this.posZ);
|
| 126 | Vec3 vec31 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
|
| 127 | MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec3, vec31);
|
| 128 | vec3 = new Vec3(this.posX, this.posY, this.posZ);
|
| 129 | vec31 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
|
| 130 |
|
| 131 | if (movingobjectposition != null)
|
| 132 | {
|
| 133 | vec31 = new Vec3(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
|
| 134 | }
|
| 135 |
|
| 136 | Entity entity = null;
|
| 137 | List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
|
| 138 | double d0 = 0.0D;
|
| 139 |
|
| 140 | for (int i = 0; i < list.size(); ++i)
|
| 141 | {
|
| 142 | Entity entity1 = (Entity)list.get(i);
|
| 143 |
|
| 144 | if (entity1.canBeCollidedWith() && (!entity1.isEntityEqual(this.shootingEntity) || this.ticksInAir >= 25))
|
| 145 | {
|
| 146 | float f = 0.3F;
|
| 147 | AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expand((double)f, (double)f, (double)f);
|
| 148 | MovingObjectPosition movingobjectposition1 = axisalignedbb.calculateIntercept(vec3, vec31);
|
| 149 |
|
| 150 | if (movingobjectposition1 != null)
|
| 151 | {
|
| 152 | double d1 = vec3.squareDistanceTo(movingobjectposition1.hitVec);
|
| 153 |
|
| 154 | if (d1 < d0 || d0 == 0.0D)
|
| 155 | {
|
| 156 | entity = entity1;
|
| 157 | d0 = d1;
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | if (entity != null)
|
| 164 | {
|
| 165 | movingobjectposition = new MovingObjectPosition(entity);
|
| 166 | }
|
| 167 |
|
| 168 | if (movingobjectposition != null)
|
| 169 | {
|
| 170 | this.onImpact(movingobjectposition);
|
| 171 | }
|
| 172 |
|
| 173 | this.posX += this.motionX;
|
| 174 | this.posY += this.motionY;
|
| 175 | this.posZ += this.motionZ;
|
| 176 | float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
|
| 177 | this.rotationYaw = (float)(MathHelper.atan2(this.motionZ, this.motionX) * 180.0D / Math.PI) + 90.0F;
|
| 178 |
|
| 179 | for (this.rotationPitch = (float)(MathHelper.atan2((double)f1, this.motionY) * 180.0D / Math.PI) - 90.0F; this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
|
| 180 | {
|
| 181 | ;
|
| 182 | }
|
| 183 |
|
| 184 | while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
|
| 185 | {
|
| 186 | this.prevRotationPitch += 360.0F;
|
| 187 | }
|
| 188 |
|
| 189 | while (this.rotationYaw - this.prevRotationYaw < -180.0F)
|
| 190 | {
|
| 191 | this.prevRotationYaw -= 360.0F;
|
| 192 | }
|
| 193 |
|
| 194 | while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
|
| 195 | {
|
| 196 | this.prevRotationYaw += 360.0F;
|
| 197 | }
|
| 198 |
|
| 199 | this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
|
| 200 | this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
|
| 201 | float f2 = this.getMotionFactor();
|
| 202 |
|
| 203 | if (this.isInWater())
|
| 204 | {
|
| 205 | for (int j = 0; j < 4; ++j)
|
| 206 | {
|
| 207 | float f3 = 0.25F;
|
| 208 | this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.motionX * (double)f3, this.posY - this.motionY * (double)f3, this.posZ - this.motionZ * (double)f3, this.motionX, this.motionY, this.motionZ, new int[0]);
|
| 209 | }
|
| 210 |
|
| 211 | f2 = 0.8F;
|
| 212 | }
|
| 213 |
|
| 214 | this.motionX += this.accelerationX;
|
| 215 | this.motionY += this.accelerationY;
|
| 216 | this.motionZ += this.accelerationZ;
|
| 217 | this.motionX *= (double)f2;
|
| 218 | this.motionY *= (double)f2;
|
| 219 | this.motionZ *= (double)f2;
|
| 220 | this.worldObj.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, this.posX, this.posY + 0.5D, this.posZ, 0.0D, 0.0D, 0.0D, new int[0]);
|
| 221 | this.setPosition(this.posX, this.posY, this.posZ);
|
| 222 | }
|
| 223 | else
|
| 224 | {
|
| 225 | this.setDead();
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | /**
|
| 230 | * Return the motion factor for this projectile. The factor is multiplied by the original motion.
|
| 231 | */
|
| 232 | protected float getMotionFactor()
|
| 233 | {
|
| 234 | return 0.95F;
|
| 235 | }
|
| 236 |
|
| 237 | /**
|
| 238 | * Called when this EntityFireball hits a block or entity.
|
| 239 | */
|
| 240 | protected abstract void onImpact(MovingObjectPosition movingObject);
|
| 241 |
|
| 242 | /**
|
| 243 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 244 | */
|
| 245 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 246 | {
|
| 247 | tagCompound.setShort("xTile", (short)this.xTile);
|
| 248 | tagCompound.setShort("yTile", (short)this.yTile);
|
| 249 | tagCompound.setShort("zTile", (short)this.zTile);
|
| 250 | ResourceLocation resourcelocation = (ResourceLocation)Block.blockRegistry.getNameForObject(this.inTile);
|
| 251 | tagCompound.setString("inTile", resourcelocation == null ? "" : resourcelocation.toString());
|
| 252 | tagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0));
|
| 253 | tagCompound.setTag("direction", this.newDoubleNBTList(new double[] {this.motionX, this.motionY, this.motionZ}));
|
| 254 | }
|
| 255 |
|
| 256 | /**
|
| 257 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 258 | */
|
| 259 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 260 | {
|
| 261 | this.xTile = tagCompund.getShort("xTile");
|
| 262 | this.yTile = tagCompund.getShort("yTile");
|
| 263 | this.zTile = tagCompund.getShort("zTile");
|
| 264 |
|
| 265 | if (tagCompund.hasKey("inTile", 8))
|
| 266 | {
|
| 267 | this.inTile = Block.getBlockFromName(tagCompund.getString("inTile"));
|
| 268 | }
|
| 269 | else
|
| 270 | {
|
| 271 | this.inTile = Block.getBlockById(tagCompund.getByte("inTile") & 255);
|
| 272 | }
|
| 273 |
|
| 274 | this.inGround = tagCompund.getByte("inGround") == 1;
|
| 275 |
|
| 276 | if (tagCompund.hasKey("direction", 9))
|
| 277 | {
|
| 278 | NBTTagList nbttaglist = tagCompund.getTagList("direction", 6);
|
| 279 | this.motionX = nbttaglist.getDoubleAt(0);
|
| 280 | this.motionY = nbttaglist.getDoubleAt(1);
|
| 281 | this.motionZ = nbttaglist.getDoubleAt(2);
|
| 282 | }
|
| 283 | else
|
| 284 | {
|
| 285 | this.setDead();
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | /**
|
| 290 | * Returns true if other Entities should be prevented from moving through this Entity.
|
| 291 | */
|
| 292 | public boolean canBeCollidedWith()
|
| 293 | {
|
| 294 | return true;
|
| 295 | }
|
| 296 |
|
| 297 | public float getCollisionBorderSize()
|
| 298 | {
|
| 299 | return 1.0F;
|
| 300 | }
|
| 301 |
|
| 302 | /**
|
| 303 | * Called when the entity is attacked.
|
| 304 | */
|
| 305 | public boolean attackEntityFrom(DamageSource source, float amount)
|
| 306 | {
|
| 307 | if (this.isEntityInvulnerable(source))
|
| 308 | {
|
| 309 | return false;
|
| 310 | }
|
| 311 | else
|
| 312 | {
|
| 313 | this.setBeenAttacked();
|
| 314 |
|
| 315 | if (source.getEntity() != null)
|
| 316 | {
|
| 317 | Vec3 vec3 = source.getEntity().getLookVec();
|
| 318 |
|
| 319 | if (vec3 != null)
|
| 320 | {
|
| 321 | this.motionX = vec3.xCoord;
|
| 322 | this.motionY = vec3.yCoord;
|
| 323 | this.motionZ = vec3.zCoord;
|
| 324 | this.accelerationX = this.motionX * 0.1D;
|
| 325 | this.accelerationY = this.motionY * 0.1D;
|
| 326 | this.accelerationZ = this.motionZ * 0.1D;
|
| 327 | }
|
| 328 |
|
| 329 | if (source.getEntity() instanceof EntityLivingBase)
|
| 330 | {
|
| 331 | this.shootingEntity = (EntityLivingBase)source.getEntity();
|
| 332 | }
|
| 333 |
|
| 334 | return true;
|
| 335 | }
|
| 336 | else
|
| 337 | {
|
| 338 | return false;
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 |
|
| 343 | /**
|
| 344 | * Gets how bright this entity is.
|
| 345 | */
|
| 346 | public float getBrightness(float partialTicks)
|
| 347 | {
|
| 348 | return 1.0F;
|
| 349 | }
|
| 350 |
|
| 351 | public int getBrightnessForRender(float partialTicks)
|
| 352 | {
|
| 353 | return 15728880;
|
| 354 | }
|
| 355 | }
|
| 356 |
|