001 package net.minecraft.src; 002 003 import cpw.mods.fml.common.Side; 004 import cpw.mods.fml.common.asm.SideOnly; 005 import java.util.List; 006 007 public class EntityFishHook extends Entity 008 { 009 /** The tile this entity is on, X position */ 010 private int xTile; 011 012 /** The tile this entity is on, Y position */ 013 private int yTile; 014 015 /** The tile this entity is on, Z position */ 016 private int zTile; 017 private int inTile; 018 private boolean inGround; 019 public int shake; 020 public EntityPlayer angler; 021 private int ticksInGround; 022 private int ticksInAir; 023 024 /** the number of ticks remaining until this fish can no longer be caught */ 025 private int ticksCatchable; 026 027 /** 028 * The entity that the fishing rod is connected to, if any. When you right click on the fishing rod and the hook 029 * falls on to an entity, this it that entity. 030 */ 031 public Entity bobber; 032 private int fishPosRotationIncrements; 033 private double fishX; 034 private double fishY; 035 private double fishZ; 036 private double fishYaw; 037 private double fishPitch; 038 @SideOnly(Side.CLIENT) 039 private double velocityX; 040 @SideOnly(Side.CLIENT) 041 private double velocityY; 042 @SideOnly(Side.CLIENT) 043 private double velocityZ; 044 045 public EntityFishHook(World par1World) 046 { 047 super(par1World); 048 this.xTile = -1; 049 this.yTile = -1; 050 this.zTile = -1; 051 this.inTile = 0; 052 this.inGround = false; 053 this.shake = 0; 054 this.ticksInAir = 0; 055 this.ticksCatchable = 0; 056 this.bobber = null; 057 this.setSize(0.25F, 0.25F); 058 this.ignoreFrustumCheck = true; 059 } 060 061 @SideOnly(Side.CLIENT) 062 public EntityFishHook(World par1World, double par2, double par4, double par6, EntityPlayer par8EntityPlayer) 063 { 064 this(par1World); 065 this.setPosition(par2, par4, par6); 066 this.ignoreFrustumCheck = true; 067 this.angler = par8EntityPlayer; 068 par8EntityPlayer.fishEntity = this; 069 } 070 071 public EntityFishHook(World par1World, EntityPlayer par2EntityPlayer) 072 { 073 super(par1World); 074 this.xTile = -1; 075 this.yTile = -1; 076 this.zTile = -1; 077 this.inTile = 0; 078 this.inGround = false; 079 this.shake = 0; 080 this.ticksInAir = 0; 081 this.ticksCatchable = 0; 082 this.bobber = null; 083 this.ignoreFrustumCheck = true; 084 this.angler = par2EntityPlayer; 085 this.angler.fishEntity = this; 086 this.setSize(0.25F, 0.25F); 087 this.setLocationAndAngles(par2EntityPlayer.posX, par2EntityPlayer.posY + 1.62D - (double)par2EntityPlayer.yOffset, par2EntityPlayer.posZ, par2EntityPlayer.rotationYaw, par2EntityPlayer.rotationPitch); 088 this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F); 089 this.posY -= 0.10000000149011612D; 090 this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F); 091 this.setPosition(this.posX, this.posY, this.posZ); 092 this.yOffset = 0.0F; 093 float var3 = 0.4F; 094 this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * var3); 095 this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * var3); 096 this.motionY = (double)(-MathHelper.sin(this.rotationPitch / 180.0F * (float)Math.PI) * var3); 097 this.calculateVelocity(this.motionX, this.motionY, this.motionZ, 1.5F, 1.0F); 098 } 099 100 protected void entityInit() {} 101 102 @SideOnly(Side.CLIENT) 103 104 /** 105 * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge 106 * length * 64 * renderDistanceWeight Args: distance 107 */ 108 public boolean isInRangeToRenderDist(double par1) 109 { 110 double var3 = this.boundingBox.getAverageEdgeLength() * 4.0D; 111 var3 *= 64.0D; 112 return par1 < var3 * var3; 113 } 114 115 public void calculateVelocity(double par1, double par3, double par5, float par7, float par8) 116 { 117 float var9 = MathHelper.sqrt_double(par1 * par1 + par3 * par3 + par5 * par5); 118 par1 /= (double)var9; 119 par3 /= (double)var9; 120 par5 /= (double)var9; 121 par1 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8; 122 par3 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8; 123 par5 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8; 124 par1 *= (double)par7; 125 par3 *= (double)par7; 126 par5 *= (double)par7; 127 this.motionX = par1; 128 this.motionY = par3; 129 this.motionZ = par5; 130 float var10 = MathHelper.sqrt_double(par1 * par1 + par5 * par5); 131 this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(par1, par5) * 180.0D / Math.PI); 132 this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(par3, (double)var10) * 180.0D / Math.PI); 133 this.ticksInGround = 0; 134 } 135 136 @SideOnly(Side.CLIENT) 137 138 /** 139 * Sets the position and rotation. Only difference from the other one is no bounding on the rotation. Args: posX, 140 * posY, posZ, yaw, pitch 141 */ 142 public void setPositionAndRotation2(double par1, double par3, double par5, float par7, float par8, int par9) 143 { 144 this.fishX = par1; 145 this.fishY = par3; 146 this.fishZ = par5; 147 this.fishYaw = (double)par7; 148 this.fishPitch = (double)par8; 149 this.fishPosRotationIncrements = par9; 150 this.motionX = this.velocityX; 151 this.motionY = this.velocityY; 152 this.motionZ = this.velocityZ; 153 } 154 155 @SideOnly(Side.CLIENT) 156 157 /** 158 * Sets the velocity to the args. Args: x, y, z 159 */ 160 public void setVelocity(double par1, double par3, double par5) 161 { 162 this.velocityX = this.motionX = par1; 163 this.velocityY = this.motionY = par3; 164 this.velocityZ = this.motionZ = par5; 165 } 166 167 /** 168 * Called to update the entity's position/logic. 169 */ 170 public void onUpdate() 171 { 172 super.onUpdate(); 173 174 if (this.fishPosRotationIncrements > 0) 175 { 176 double var21 = this.posX + (this.fishX - this.posX) / (double)this.fishPosRotationIncrements; 177 double var22 = this.posY + (this.fishY - this.posY) / (double)this.fishPosRotationIncrements; 178 double var23 = this.posZ + (this.fishZ - this.posZ) / (double)this.fishPosRotationIncrements; 179 double var7 = MathHelper.wrapAngleTo180_double(this.fishYaw - (double)this.rotationYaw); 180 this.rotationYaw = (float)((double)this.rotationYaw + var7 / (double)this.fishPosRotationIncrements); 181 this.rotationPitch = (float)((double)this.rotationPitch + (this.fishPitch - (double)this.rotationPitch) / (double)this.fishPosRotationIncrements); 182 --this.fishPosRotationIncrements; 183 this.setPosition(var21, var22, var23); 184 this.setRotation(this.rotationYaw, this.rotationPitch); 185 } 186 else 187 { 188 if (!this.worldObj.isRemote) 189 { 190 ItemStack var1 = this.angler.getCurrentEquippedItem(); 191 192 if (this.angler.isDead || !this.angler.isEntityAlive() || var1 == null || var1.getItem() != Item.fishingRod || this.getDistanceSqToEntity(this.angler) > 1024.0D) 193 { 194 this.setDead(); 195 this.angler.fishEntity = null; 196 return; 197 } 198 199 if (this.bobber != null) 200 { 201 if (!this.bobber.isDead) 202 { 203 this.posX = this.bobber.posX; 204 this.posY = this.bobber.boundingBox.minY + (double)this.bobber.height * 0.8D; 205 this.posZ = this.bobber.posZ; 206 return; 207 } 208 209 this.bobber = null; 210 } 211 } 212 213 if (this.shake > 0) 214 { 215 --this.shake; 216 } 217 218 if (this.inGround) 219 { 220 int var19 = this.worldObj.getBlockId(this.xTile, this.yTile, this.zTile); 221 222 if (var19 == this.inTile) 223 { 224 ++this.ticksInGround; 225 226 if (this.ticksInGround == 1200) 227 { 228 this.setDead(); 229 } 230 231 return; 232 } 233 234 this.inGround = false; 235 this.motionX *= (double)(this.rand.nextFloat() * 0.2F); 236 this.motionY *= (double)(this.rand.nextFloat() * 0.2F); 237 this.motionZ *= (double)(this.rand.nextFloat() * 0.2F); 238 this.ticksInGround = 0; 239 this.ticksInAir = 0; 240 } 241 else 242 { 243 ++this.ticksInAir; 244 } 245 246 Vec3 var20 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ); 247 Vec3 var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); 248 MovingObjectPosition var3 = this.worldObj.rayTraceBlocks(var20, var2); 249 var20 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ); 250 var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); 251 252 if (var3 != null) 253 { 254 var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(var3.hitVec.xCoord, var3.hitVec.yCoord, var3.hitVec.zCoord); 255 } 256 257 Entity var4 = null; 258 List var5 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D)); 259 double var6 = 0.0D; 260 double var13; 261 262 for (int var8 = 0; var8 < var5.size(); ++var8) 263 { 264 Entity var9 = (Entity)var5.get(var8); 265 266 if (var9.canBeCollidedWith() && (var9 != this.angler || this.ticksInAir >= 5)) 267 { 268 float var10 = 0.3F; 269 AxisAlignedBB var11 = var9.boundingBox.expand((double)var10, (double)var10, (double)var10); 270 MovingObjectPosition var12 = var11.calculateIntercept(var20, var2); 271 272 if (var12 != null) 273 { 274 var13 = var20.distanceTo(var12.hitVec); 275 276 if (var13 < var6 || var6 == 0.0D) 277 { 278 var4 = var9; 279 var6 = var13; 280 } 281 } 282 } 283 } 284 285 if (var4 != null) 286 { 287 var3 = new MovingObjectPosition(var4); 288 } 289 290 if (var3 != null) 291 { 292 if (var3.entityHit != null) 293 { 294 if (var3.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, this.angler), 0)) 295 { 296 this.bobber = var3.entityHit; 297 } 298 } 299 else 300 { 301 this.inGround = true; 302 } 303 } 304 305 if (!this.inGround) 306 { 307 this.moveEntity(this.motionX, this.motionY, this.motionZ); 308 float var24 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ); 309 this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI); 310 311 for (this.rotationPitch = (float)(Math.atan2(this.motionY, (double)var24) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F) 312 { 313 ; 314 } 315 316 while (this.rotationPitch - this.prevRotationPitch >= 180.0F) 317 { 318 this.prevRotationPitch += 360.0F; 319 } 320 321 while (this.rotationYaw - this.prevRotationYaw < -180.0F) 322 { 323 this.prevRotationYaw -= 360.0F; 324 } 325 326 while (this.rotationYaw - this.prevRotationYaw >= 180.0F) 327 { 328 this.prevRotationYaw += 360.0F; 329 } 330 331 this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F; 332 this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F; 333 float var25 = 0.92F; 334 335 if (this.onGround || this.isCollidedHorizontally) 336 { 337 var25 = 0.5F; 338 } 339 340 byte var27 = 5; 341 double var26 = 0.0D; 342 343 for (int var29 = 0; var29 < var27; ++var29) 344 { 345 double var14 = this.boundingBox.minY + (this.boundingBox.maxY - this.boundingBox.minY) * (double)(var29 + 0) / (double)var27 - 0.125D + 0.125D; 346 double var16 = this.boundingBox.minY + (this.boundingBox.maxY - this.boundingBox.minY) * (double)(var29 + 1) / (double)var27 - 0.125D + 0.125D; 347 AxisAlignedBB var18 = AxisAlignedBB.getAABBPool().addOrModifyAABBInPool(this.boundingBox.minX, var14, this.boundingBox.minZ, this.boundingBox.maxX, var16, this.boundingBox.maxZ); 348 349 if (this.worldObj.isAABBInMaterial(var18, Material.water)) 350 { 351 var26 += 1.0D / (double)var27; 352 } 353 } 354 355 if (var26 > 0.0D) 356 { 357 if (this.ticksCatchable > 0) 358 { 359 --this.ticksCatchable; 360 } 361 else 362 { 363 short var28 = 500; 364 365 if (this.worldObj.canLightningStrikeAt(MathHelper.floor_double(this.posX), MathHelper.floor_double(this.posY) + 1, MathHelper.floor_double(this.posZ))) 366 { 367 var28 = 300; 368 } 369 370 if (this.rand.nextInt(var28) == 0) 371 { 372 this.ticksCatchable = this.rand.nextInt(30) + 10; 373 this.motionY -= 0.20000000298023224D; 374 this.func_85030_a("random.splash", 0.25F, 1.0F + (this.rand.nextFloat() - this.rand.nextFloat()) * 0.4F); 375 float var30 = (float)MathHelper.floor_double(this.boundingBox.minY); 376 int var15; 377 float var17; 378 float var31; 379 380 for (var15 = 0; (float)var15 < 1.0F + this.width * 20.0F; ++var15) 381 { 382 var31 = (this.rand.nextFloat() * 2.0F - 1.0F) * this.width; 383 var17 = (this.rand.nextFloat() * 2.0F - 1.0F) * this.width; 384 this.worldObj.spawnParticle("bubble", this.posX + (double)var31, (double)(var30 + 1.0F), this.posZ + (double)var17, this.motionX, this.motionY - (double)(this.rand.nextFloat() * 0.2F), this.motionZ); 385 } 386 387 for (var15 = 0; (float)var15 < 1.0F + this.width * 20.0F; ++var15) 388 { 389 var31 = (this.rand.nextFloat() * 2.0F - 1.0F) * this.width; 390 var17 = (this.rand.nextFloat() * 2.0F - 1.0F) * this.width; 391 this.worldObj.spawnParticle("splash", this.posX + (double)var31, (double)(var30 + 1.0F), this.posZ + (double)var17, this.motionX, this.motionY, this.motionZ); 392 } 393 } 394 } 395 } 396 397 if (this.ticksCatchable > 0) 398 { 399 this.motionY -= (double)(this.rand.nextFloat() * this.rand.nextFloat() * this.rand.nextFloat()) * 0.2D; 400 } 401 402 var13 = var26 * 2.0D - 1.0D; 403 this.motionY += 0.03999999910593033D * var13; 404 405 if (var26 > 0.0D) 406 { 407 var25 = (float)((double)var25 * 0.9D); 408 this.motionY *= 0.8D; 409 } 410 411 this.motionX *= (double)var25; 412 this.motionY *= (double)var25; 413 this.motionZ *= (double)var25; 414 this.setPosition(this.posX, this.posY, this.posZ); 415 } 416 } 417 } 418 419 /** 420 * (abstract) Protected helper method to write subclass entity data to NBT. 421 */ 422 public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) 423 { 424 par1NBTTagCompound.setShort("xTile", (short)this.xTile); 425 par1NBTTagCompound.setShort("yTile", (short)this.yTile); 426 par1NBTTagCompound.setShort("zTile", (short)this.zTile); 427 par1NBTTagCompound.setByte("inTile", (byte)this.inTile); 428 par1NBTTagCompound.setByte("shake", (byte)this.shake); 429 par1NBTTagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0)); 430 } 431 432 /** 433 * (abstract) Protected helper method to read subclass entity data from NBT. 434 */ 435 public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) 436 { 437 this.xTile = par1NBTTagCompound.getShort("xTile"); 438 this.yTile = par1NBTTagCompound.getShort("yTile"); 439 this.zTile = par1NBTTagCompound.getShort("zTile"); 440 this.inTile = par1NBTTagCompound.getByte("inTile") & 255; 441 this.shake = par1NBTTagCompound.getByte("shake") & 255; 442 this.inGround = par1NBTTagCompound.getByte("inGround") == 1; 443 } 444 445 @SideOnly(Side.CLIENT) 446 public float getShadowSize() 447 { 448 return 0.0F; 449 } 450 451 public int catchFish() 452 { 453 if (this.worldObj.isRemote) 454 { 455 return 0; 456 } 457 else 458 { 459 byte var1 = 0; 460 461 if (this.bobber != null) 462 { 463 double var2 = this.angler.posX - this.posX; 464 double var4 = this.angler.posY - this.posY; 465 double var6 = this.angler.posZ - this.posZ; 466 double var8 = (double)MathHelper.sqrt_double(var2 * var2 + var4 * var4 + var6 * var6); 467 double var10 = 0.1D; 468 this.bobber.motionX += var2 * var10; 469 this.bobber.motionY += var4 * var10 + (double)MathHelper.sqrt_double(var8) * 0.08D; 470 this.bobber.motionZ += var6 * var10; 471 var1 = 3; 472 } 473 else if (this.ticksCatchable > 0) 474 { 475 EntityItem var13 = new EntityItem(this.worldObj, this.posX, this.posY, this.posZ, new ItemStack(Item.fishRaw)); 476 double var3 = this.angler.posX - this.posX; 477 double var5 = this.angler.posY - this.posY; 478 double var7 = this.angler.posZ - this.posZ; 479 double var9 = (double)MathHelper.sqrt_double(var3 * var3 + var5 * var5 + var7 * var7); 480 double var11 = 0.1D; 481 var13.motionX = var3 * var11; 482 var13.motionY = var5 * var11 + (double)MathHelper.sqrt_double(var9) * 0.08D; 483 var13.motionZ = var7 * var11; 484 this.worldObj.spawnEntityInWorld(var13); 485 this.angler.addStat(StatList.fishCaughtStat, 1); 486 this.angler.worldObj.spawnEntityInWorld(new EntityXPOrb(this.angler.worldObj, this.angler.posX, this.angler.posY + 0.5D, this.angler.posZ + 0.5D, this.rand.nextInt(3) + 1)); 487 var1 = 1; 488 } 489 490 if (this.inGround) 491 { 492 var1 = 2; 493 } 494 495 this.setDead(); 496 this.angler.fishEntity = null; 497 return var1; 498 } 499 } 500 501 /** 502 * Will get destroyed next tick. 503 */ 504 public void setDead() 505 { 506 super.setDead(); 507 508 if (this.angler != null) 509 { 510 this.angler.fishEntity = null; 511 } 512 } 513 }