package game.entity.projectile; import java.util.List; import game.block.Block; import game.enchantment.EnchantmentHelper; import game.entity.DamageSource; import game.entity.Entity; import game.entity.EntityType; import game.entity.item.EntityItem; import game.entity.item.EntityXp; import game.entity.npc.EntityNPC; import game.entity.types.IObjectData; import game.init.BlockRegistry; import game.init.Blocks; import game.init.Config; import game.init.Items; import game.init.SoundEvent; import game.item.ItemStack; import game.nbt.NBTTagCompound; import game.renderer.particle.ParticleType; import game.util.ExtMath; import game.world.BlockPos; import game.world.BoundingBox; import game.world.HitPosition; import game.world.Vec3; import game.world.World; import game.world.WorldServer; import game.worldgen.LootConstants; public class EntityHook extends Entity implements IObjectData { private int xTile; private int yTile; private int zTile; private Block inTile; private boolean inGround; public int shake; public EntityNPC angler; private int ticksInGround; private int ticksInAir; private int ticksCatchable; private int ticksCaughtDelay; private int ticksCatchableDelay; private float fishApproachAngle; public Entity caughtEntity; private int fishPosRotationIncrements; private double fishX; private double fishY; private double fishZ; private double fishYaw; private double fishPitch; private double clientMotionX; private double clientMotionY; private double clientMotionZ; public EntityHook(World worldIn) { super(worldIn); this.xTile = -1; this.yTile = -1; this.zTile = -1; this.setSize(0.25F, 0.25F); this.noFrustumCheck = true; } public EntityHook(World worldIn, double x, double y, double z, int data) //, EntityNPC anglerIn) { super(worldIn); this.xTile = -1; this.yTile = -1; this.zTile = -1; this.setSize(0.25F, 0.25F); this.noFrustumCheck = true; // this(worldIn); this.setPosition(x, y, z); // this.noFrustumCheck = true; // this.angler = anglerIn; // if(anglerIn != null) { // anglerIn.fishEntity = this; // } // } // // public EntityFishHook(World worldIn, double x, double y, double z, int data) // { // this(worldIn, x, y, z, null); Entity entity1 = worldIn.getEntityByID(data); if (entity1 != null && entity1.isPlayer()) { this.angler = (EntityNPC)entity1; this.angler.fishEntity = this; } else { this.setDead(); } } public EntityHook(World worldIn, EntityNPC fishingPlayer) { super(worldIn); this.xTile = -1; this.yTile = -1; this.zTile = -1; this.noFrustumCheck = true; this.angler = fishingPlayer; this.angler.fishEntity = this; this.setSize(0.25F, 0.25F); this.setLocationAndAngles(fishingPlayer.posX, fishingPlayer.posY + (double)fishingPlayer.getEyeHeight(), fishingPlayer.posZ, fishingPlayer.rotYaw, fishingPlayer.rotPitch); this.posX -= (double)(ExtMath.cos(this.rotYaw / 180.0F * (float)Math.PI) * 0.16F); this.posY -= 0.10000000149011612D; this.posZ -= (double)(ExtMath.sin(this.rotYaw / 180.0F * (float)Math.PI) * 0.16F); this.setPosition(this.posX, this.posY, this.posZ); float f = 0.4F; this.motionX = (double)(-ExtMath.sin(this.rotYaw / 180.0F * (float)Math.PI) * ExtMath.cos(this.rotPitch / 180.0F * (float)Math.PI) * f); this.motionZ = (double)(ExtMath.cos(this.rotYaw / 180.0F * (float)Math.PI) * ExtMath.cos(this.rotPitch / 180.0F * (float)Math.PI) * f); this.motionY = (double)(-ExtMath.sin(this.rotPitch / 180.0F * (float)Math.PI) * f); this.handleHookCasting(this.motionX, this.motionY, this.motionZ, 1.5F, 1.0F); } protected void entityInit() { } public boolean writeToNBTOptional(NBTTagCompound tagCompund) { return false; } /** * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge * length * 64 * renderDistanceWeight Args: distance */ public boolean isInRangeToRenderDist(double distance) { double d0 = this.getEntityBoundingBox().getAverageEdgeLength() * 4.0D; if (Double.isNaN(d0)) { d0 = 4.0D; } d0 = d0 * 64.0D; return distance < d0 * d0; } public void handleHookCasting(double p_146035_1_, double p_146035_3_, double p_146035_5_, float p_146035_7_, float p_146035_8_) { float f = ExtMath.sqrtd(p_146035_1_ * p_146035_1_ + p_146035_3_ * p_146035_3_ + p_146035_5_ * p_146035_5_); p_146035_1_ = p_146035_1_ / (double)f; p_146035_3_ = p_146035_3_ / (double)f; p_146035_5_ = p_146035_5_ / (double)f; p_146035_1_ = p_146035_1_ + this.rand.gaussian() * 0.007499999832361937D * (double)p_146035_8_; p_146035_3_ = p_146035_3_ + this.rand.gaussian() * 0.007499999832361937D * (double)p_146035_8_; p_146035_5_ = p_146035_5_ + this.rand.gaussian() * 0.007499999832361937D * (double)p_146035_8_; p_146035_1_ = p_146035_1_ * (double)p_146035_7_; p_146035_3_ = p_146035_3_ * (double)p_146035_7_; p_146035_5_ = p_146035_5_ * (double)p_146035_7_; this.motionX = p_146035_1_; this.motionY = p_146035_3_; this.motionZ = p_146035_5_; float f1 = ExtMath.sqrtd(p_146035_1_ * p_146035_1_ + p_146035_5_ * p_146035_5_); this.prevYaw = this.rotYaw = (float)(ExtMath.atan2(p_146035_1_, p_146035_5_) * 180.0D / Math.PI); this.prevPitch = this.rotPitch = (float)(ExtMath.atan2(p_146035_3_, (double)f1) * 180.0D / Math.PI); this.ticksInGround = 0; } public void setPositionAndRotation2(double x, double y, double z, float yaw, float pitch, int posRotationIncrements, boolean p_180426_10_) { this.fishX = x; this.fishY = y; this.fishZ = z; this.fishYaw = (double)yaw; this.fishPitch = (double)pitch; this.fishPosRotationIncrements = posRotationIncrements; this.motionX = this.clientMotionX; this.motionY = this.clientMotionY; this.motionZ = this.clientMotionZ; } /** * Sets the velocity to the args. Args: x, y, z */ public void setVelocity(double x, double y, double z) { this.clientMotionX = this.motionX = x; this.clientMotionY = this.motionY = y; this.clientMotionZ = this.motionZ = z; } /** * Called to update the entity's position/logic. */ public void onUpdate() { super.onUpdate(); if(this.angler == null) { this.setDead(); return; } if (this.fishPosRotationIncrements > 0) { double d7 = this.posX + (this.fishX - this.posX) / (double)this.fishPosRotationIncrements; double d8 = this.posY + (this.fishY - this.posY) / (double)this.fishPosRotationIncrements; double d9 = this.posZ + (this.fishZ - this.posZ) / (double)this.fishPosRotationIncrements; double d1 = ExtMath.wrapd(this.fishYaw - (double)this.rotYaw); this.rotYaw = (float)((double)this.rotYaw + d1 / (double)this.fishPosRotationIncrements); this.rotPitch = (float)((double)this.rotPitch + (this.fishPitch - (double)this.rotPitch) / (double)this.fishPosRotationIncrements); --this.fishPosRotationIncrements; this.setPosition(d7, d8, d9); this.setRotation(this.rotYaw, this.rotPitch); } else { if (!this.worldObj.client) { ItemStack itemstack = this.angler.getCurrentEquippedItem(); if (this.angler.dead || !this.angler.isEntityAlive() || itemstack == null || itemstack.getItem() != Items.fishing_rod || this.getDistanceSqToEntity(this.angler) > 1024.0D) { this.setDead(); this.angler.fishEntity = null; return; } if (this.caughtEntity != null) { if (!this.caughtEntity.dead) { this.posX = this.caughtEntity.posX; double d17 = (double)this.caughtEntity.height; this.posY = this.caughtEntity.getEntityBoundingBox().minY + d17 * 0.8D; this.posZ = this.caughtEntity.posZ; return; } this.caughtEntity = null; } } if (this.shake > 0) { --this.shake; } if (this.inGround) { if (this.worldObj.getState(new BlockPos(this.xTile, this.yTile, this.zTile)).getBlock() == this.inTile) { ++this.ticksInGround; if (this.ticksInGround == 1200) { this.setDead(); } return; } this.inGround = false; this.motionX *= (double)(this.rand.floatv() * 0.2F); this.motionY *= (double)(this.rand.floatv() * 0.2F); this.motionZ *= (double)(this.rand.floatv() * 0.2F); this.ticksInGround = 0; this.ticksInAir = 0; } else { ++this.ticksInAir; } Vec3 vec31 = new Vec3(this.posX, this.posY, this.posZ); Vec3 vec3 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); HitPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec31, vec3); vec31 = new Vec3(this.posX, this.posY, this.posZ); vec3 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); if (movingobjectposition != null) { vec3 = new Vec3(movingobjectposition.vec.xCoord, movingobjectposition.vec.yCoord, movingobjectposition.vec.zCoord); } Entity entity = null; List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D)); double d0 = 0.0D; for (int i = 0; i < list.size(); ++i) { Entity entity1 = (Entity)list.get(i); if (entity1.canBeCollidedWith() && (entity1 != this.angler || this.ticksInAir >= 5)) { float f = 0.3F; BoundingBox axisalignedbb = entity1.getEntityBoundingBox().expand((double)f, (double)f, (double)f); HitPosition movingobjectposition1 = axisalignedbb.calculateIntercept(vec31, vec3); if (movingobjectposition1 != null) { double d2 = vec31.squareDistanceTo(movingobjectposition1.vec); if (d2 < d0 || d0 == 0.0D) { entity = entity1; d0 = d2; } } } } if (entity != null) { movingobjectposition = new HitPosition(entity); } if (movingobjectposition != null) { if (movingobjectposition.entity != null) { if (this.worldObj.client || !Config.knockHook || movingobjectposition.entity.attackEntityFrom( DamageSource.causeThrownDamage(this, this.angler), 0) || !Config.hookCheckDamage) { if(this.worldObj.client || Config.hookEntity) // && (!(movingobjectposition.entity.isPlayer()) // || !((EntityNPC)movingobjectposition.entity).creative))) this.caughtEntity = movingobjectposition.entity; } } else { this.inGround = true; } } if (!this.inGround) { this.moveEntity(this.motionX, this.motionY, this.motionZ); float f5 = ExtMath.sqrtd(this.motionX * this.motionX + this.motionZ * this.motionZ); this.rotYaw = (float)(ExtMath.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI); for (this.rotPitch = (float)(ExtMath.atan2(this.motionY, (double)f5) * 180.0D / Math.PI); this.rotPitch - this.prevPitch < -180.0F; this.prevPitch -= 360.0F) { ; } while (this.rotPitch - this.prevPitch >= 180.0F) { this.prevPitch += 360.0F; } while (this.rotYaw - this.prevYaw < -180.0F) { this.prevYaw -= 360.0F; } while (this.rotYaw - this.prevYaw >= 180.0F) { this.prevYaw += 360.0F; } this.rotPitch = this.prevPitch + (this.rotPitch - this.prevPitch) * 0.2F; this.rotYaw = this.prevYaw + (this.rotYaw - this.prevYaw) * 0.2F; float f6 = 0.92F; if (this.onGround || this.collidedHorizontally) { f6 = 0.5F; } int j = 5; double d10 = 0.0D; for (int k = 0; k < j; ++k) { BoundingBox axisalignedbb1 = this.getEntityBoundingBox(); double d3 = axisalignedbb1.maxY - axisalignedbb1.minY; double d4 = axisalignedbb1.minY + d3 * (double)k / (double)j; double d5 = axisalignedbb1.minY + d3 * (double)(k + 1) / (double)j; BoundingBox axisalignedbb2 = new BoundingBox(axisalignedbb1.minX, d4, axisalignedbb1.minZ, axisalignedbb1.maxX, d5, axisalignedbb1.maxZ); if (this.worldObj.isAABBInLiquid(axisalignedbb2)) { d10 += 1.0D / (double)j; } } if (!this.worldObj.client && d10 > 0.0D) { WorldServer worldserver = (WorldServer)this.worldObj; int l = 1; BlockPos blockpos = (new BlockPos(this)).up(); if (this.rand.floatv() < 0.25F && this.worldObj.isRainingAt(blockpos, true)) { l = 2; } if (this.rand.floatv() < 0.5F && !this.worldObj.canSeeSky(blockpos)) { --l; } if (this.ticksCatchable > 0) { --this.ticksCatchable; if (this.ticksCatchable <= 0) { this.ticksCaughtDelay = 0; this.ticksCatchableDelay = 0; } } else if (this.ticksCatchableDelay > 0) { this.ticksCatchableDelay -= l; if (this.ticksCatchableDelay <= 0) { this.motionY -= 0.20000000298023224D; this.playSound(SoundEvent.SPLASH, 0.25F); float f8 = (float)ExtMath.floord(this.getEntityBoundingBox().minY); worldserver.spawnParticle(ParticleType.WATER_BUBBLE, this.posX, (double)(f8 + 1.0F), this.posZ, (int)(1.0F + this.width * 20.0F), (double)this.width, 0.0D, (double)this.width, 0.20000000298023224D); worldserver.spawnParticle(ParticleType.WATER_WAKE, this.posX, (double)(f8 + 1.0F), this.posZ, (int)(1.0F + this.width * 20.0F), (double)this.width, 0.0D, (double)this.width, 0.20000000298023224D); this.ticksCatchable = this.rand.range(10, 30); } else { this.fishApproachAngle = (float)((double)this.fishApproachAngle + this.rand.gaussian() * 4.0D); float f7 = this.fishApproachAngle * 0.017453292F; float f10 = ExtMath.sin(f7); float f11 = ExtMath.cos(f7); double d13 = this.posX + (double)(f10 * (float)this.ticksCatchableDelay * 0.1F); double d15 = (double)((float)ExtMath.floord(this.getEntityBoundingBox().minY) + 1.0F); double d16 = this.posZ + (double)(f11 * (float)this.ticksCatchableDelay * 0.1F); Block block1 = worldserver.getState(new BlockPos((int)d13, (int)d15 - 1, (int)d16)).getBlock(); if (block1 == Blocks.water || block1 == Blocks.flowing_water) { if (this.rand.floatv() < 0.15F) { worldserver.spawnParticle(ParticleType.WATER_BUBBLE, d13, d15 - 0.10000000149011612D, d16, 1, (double)f10, 0.1D, (double)f11, 0.0D); } float f3 = f10 * 0.04F; float f4 = f11 * 0.04F; worldserver.spawnParticle(ParticleType.WATER_WAKE, d13, d15, d16, 0, (double)f4, 0.01D, (double)(-f3), 1.0D); worldserver.spawnParticle(ParticleType.WATER_WAKE, d13, d15, d16, 0, (double)(-f4), 0.01D, (double)f3, 1.0D); } } } else if (this.ticksCaughtDelay > 0) { this.ticksCaughtDelay -= l; float f1 = 0.15F; if (this.ticksCaughtDelay < 20) { f1 = (float)((double)f1 + (double)(20 - this.ticksCaughtDelay) * 0.05D); } else if (this.ticksCaughtDelay < 40) { f1 = (float)((double)f1 + (double)(40 - this.ticksCaughtDelay) * 0.02D); } else if (this.ticksCaughtDelay < 60) { f1 = (float)((double)f1 + (double)(60 - this.ticksCaughtDelay) * 0.01D); } if (this.rand.floatv() < f1) { float f9 = this.rand.frange(0.0F, 360.0F) * 0.017453292F; float f2 = this.rand.frange(25.0F, 60.0F); double d12 = this.posX + (double)(ExtMath.sin(f9) * f2 * 0.1F); double d14 = (double)((float)ExtMath.floord(this.getEntityBoundingBox().minY) + 1.0F); double d6 = this.posZ + (double)(ExtMath.cos(f9) * f2 * 0.1F); Block block = worldserver.getState(new BlockPos((int)d12, (int)d14 - 1, (int)d6)).getBlock(); if (block == Blocks.water || block == Blocks.flowing_water) { worldserver.spawnParticle(ParticleType.WATER_SPLASH, d12, d14, d6, this.rand.range(2, 3), 0.10000000149011612D, 0.0D, 0.10000000149011612D, 0.0D); } } if (this.ticksCaughtDelay <= 0) { this.fishApproachAngle = this.rand.frange(0.0F, 360.0F); this.ticksCatchableDelay = this.rand.range(20, 80); } } else { this.ticksCaughtDelay = this.rand.range(100, 900); this.ticksCaughtDelay -= EnchantmentHelper.getLureModifier(this.angler) * 20 * 5; } if (this.ticksCatchable > 0) { this.motionY -= (double)(this.rand.floatv() * this.rand.floatv() * this.rand.floatv()) * 0.2D; } } double d11 = d10 * 2.0D - 1.0D; this.motionY += 0.03999999910593033D * d11; if (d10 > 0.0D) { f6 = (float)((double)f6 * 0.9D); this.motionY *= 0.8D; } this.motionX *= (double)f6; this.motionY *= (double)f6; this.motionZ *= (double)f6; this.setPosition(this.posX, this.posY, this.posZ); } } } /** * (abstract) Protected helper method to write subclass entity data to NBT. */ public void writeEntityToNBT(NBTTagCompound tagCompound) { tagCompound.setShort("xTile", (short)this.xTile); tagCompound.setShort("yTile", (short)this.yTile); tagCompound.setShort("zTile", (short)this.zTile); String resourcelocation = BlockRegistry.REGISTRY.getNameForObject(this.inTile); tagCompound.setString("inTile", resourcelocation == null ? "" : resourcelocation.toString()); tagCompound.setByte("shake", (byte)this.shake); tagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0)); } /** * (abstract) Protected helper method to read subclass entity data from NBT. */ public void readEntityFromNBT(NBTTagCompound tagCompund) { this.xTile = tagCompund.getShort("xTile"); this.yTile = tagCompund.getShort("yTile"); this.zTile = tagCompund.getShort("zTile"); if (tagCompund.hasKey("inTile", 8)) { this.inTile = BlockRegistry.getByIdFallback(tagCompund.getString("inTile")); } else { this.inTile = BlockRegistry.getBlockById(tagCompund.getByte("inTile") & 255); } this.shake = tagCompund.getByte("shake") & 255; this.inGround = tagCompund.getByte("inGround") == 1; } public int handleHookRetraction() { if (this.worldObj.client) { return 0; } else { int i = 0; if (this.caughtEntity != null) { double d0 = this.angler.posX - this.posX; double d2 = this.angler.posY - this.posY; double d4 = this.angler.posZ - this.posZ; double d6 = (double)ExtMath.sqrtd(d0 * d0 + d2 * d2 + d4 * d4); double d8 = 0.1D; this.caughtEntity.motionX += d0 * d8; this.caughtEntity.motionY += d2 * d8 + (double)ExtMath.sqrtd(d6) * 0.08D; this.caughtEntity.motionZ += d4 * d8; i = 3; } else if (this.ticksCatchable > 0) { EntityItem entityitem = new EntityItem(this.worldObj, this.posX, this.posY, this.posZ, this.getFishingResult()); double d1 = this.angler.posX - this.posX; double d3 = this.angler.posY - this.posY; double d5 = this.angler.posZ - this.posZ; double d7 = (double)ExtMath.sqrtd(d1 * d1 + d3 * d3 + d5 * d5); double d9 = 0.1D; entityitem.motionX = d1 * d9; entityitem.motionY = d3 * d9 + (double)ExtMath.sqrtd(d7) * 0.08D; entityitem.motionZ = d5 * d9; this.worldObj.spawnEntityInWorld(entityitem); if(Config.fishingXP) this.angler.worldObj.spawnEntityInWorld(new EntityXp(this.angler.worldObj, this.angler.posX, this.angler.posY + 0.5D, this.angler.posZ + 0.5D, this.rand.roll(6))); i = 1; } if (this.inGround) { i = 2; } this.setDead(); this.angler.fishEntity = null; return i; } } private ItemStack getFishingResult() { float f = this.worldObj.rand.floatv(); int i = EnchantmentHelper.getLuckOfSeaModifier(this.angler); int j = EnchantmentHelper.getLureModifier(this.angler); float f1 = 0.1F - (float)i * 0.025F - (float)j * 0.01F; float f2 = 0.05F + (float)i * 0.01F - (float)j * 0.01F; f1 = ExtMath.clampf(f1, 0.0F, 1.0F); f2 = ExtMath.clampf(f2, 0.0F, 1.0F); if (f < f1) { // this.angler.triggerAchievement(StatRegistry.junkFishedStat); return ((RngFishable)LootConstants.FISHING_JUNK.pick(this.rand)).getItemStack(this.rand); } else { f = f - f1; if (f < f2) { // this.angler.triggerAchievement(StatRegistry.treasureFishedStat); return ((RngFishable)LootConstants.FISHING_TREASURE.pick(this.rand)).getItemStack(this.rand); } else { float f3 = f - f2; // this.angler.triggerAchievement(StatRegistry.fishCaughtStat); return ((RngFishable)LootConstants.FISH_TYPES.pick(this.rand)).getItemStack(this.rand); } } } /** * Will get destroyed next tick. */ public void setDead() { super.setDead(); if (this.angler != null) { this.angler.fishEntity = null; } } public int getTrackingRange() { return 64; } public int getUpdateFrequency() { return 5; } public boolean isSendingVeloUpdates() { return true; } public int getPacketData() { return this.angler != null ? this.angler.getId() : this.getId(); } public EntityType getType() { return EntityType.PROJECTILE; } // public boolean hasSpawnVelocity() { // return false; // } }