package minefantasy.entity; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import java.util.List; import minefantasy.system.data_minefantasy; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.IProjectile; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.DamageSource; import net.minecraft.util.EnumMovingObjectType; import net.minecraft.util.MathHelper; import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.Vec3; import net.minecraft.world.World; public abstract class EntityThrowableBounce extends Entity implements IProjectile { private int xTile = -1; private int yTile = -1; public int ticksRemaining = getFuseTime(); private int zTile = -1; private int inTile = 0; protected boolean inGround = false; public int throwableShake = 0; /** * Is the entity that throws this 'thing' (snowball, ender pearl, eye of ender or potion) */ private EntityLivingBase thrower; private String throwerName = null; private int ticksInGround; private int ticksInAir = 0; public EntityThrowableBounce(World par1World) { super(par1World); this.setSize(0.25F, 0.25F); } protected void entityInit() {} @SideOnly(Side.CLIENT) /** * 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 par1) { double var3 = this.boundingBox.getAverageEdgeLength() * 4.0D; var3 *= 64.0D; return par1 < var3 * var3; } public EntityThrowableBounce(World par1World, EntityLivingBase par2EntityLivingBase) { super(par1World); this.thrower = par2EntityLivingBase; this.setSize(0.25F, 0.25F); this.setLocationAndAngles(par2EntityLivingBase.posX, par2EntityLivingBase.posY + (double)par2EntityLivingBase.getEyeHeight(), par2EntityLivingBase.posZ, par2EntityLivingBase.rotationYaw, par2EntityLivingBase.rotationPitch); this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F); this.posY -= 0.10000000149011612D; this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F); this.setPosition(this.posX, this.posY, this.posZ); this.yOffset = 0.0F; float var3 = 0.4F; this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * var3); this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * var3); this.motionY = (double)(-MathHelper.sin((this.rotationPitch + this.func_70183_g()) / 180.0F * (float)Math.PI) * var3); this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, getThrownPower(), 1.0F); } /** * Gets the power of the entity when thrown * @return Default 1.5 */ private float getThrownPower() { return 1.5F; } public EntityThrowableBounce(World par1World, double par2, double par4, double par6) { super(par1World); this.ticksInGround = 0; this.setSize(0.25F, 0.25F); this.setPosition(par2, par4, par6); this.yOffset = 0.0F; } protected float func_70183_g() { return 0.0F; } /** * Similar to setArrowHeading, it's point the throwable entity to a x, y, z direction. */ public void setThrowableHeading(double par1, double par3, double par5, float par7, float par8) { float var9 = MathHelper.sqrt_double(par1 * par1 + par3 * par3 + par5 * par5); par1 /= (double)var9; par3 /= (double)var9; par5 /= (double)var9; par1 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8; par3 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8; par5 += this.rand.nextGaussian() * 0.007499999832361937D * (double)par8; par1 *= (double)par7; par3 *= (double)par7; par5 *= (double)par7; this.motionX = par1; this.motionY = par3; this.motionZ = par5; float var10 = MathHelper.sqrt_double(par1 * par1 + par5 * par5); this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(par1, par5) * 180.0D / Math.PI); this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(par3, (double)var10) * 180.0D / Math.PI); this.ticksInGround = 0; } @SideOnly(Side.CLIENT) /** * Sets the velocity to the args. Args: x, y, z */ public void setVelocity(double par1, double par3, double par5) { this.motionX = par1; this.motionY = par3; this.motionZ = par5; if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F) { float var7 = MathHelper.sqrt_double(par1 * par1 + par5 * par5); this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(par1, par5) * 180.0D / Math.PI); this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(par3, (double)var7) * 180.0D / Math.PI); } } /** * Called to update the entity's position/logic. */ public void onUpdate() { float drag = 0.95F; motionX *= drag; motionY *= drag; motionZ *= drag; this.lastTickPosX = this.posX; this.lastTickPosY = this.posY; this.lastTickPosZ = this.posZ; super.onUpdate(); if (this.throwableShake > 0) { --this.throwableShake; } if(motionY < 0.01 && motionY > -0.01 && this.onGround) { float drag2 = 0.8F; motionX *= drag2; motionY *= drag2; motionZ *= drag2; } if (this.inGround) { int var1 = this.worldObj.getBlockId(this.xTile, this.yTile, this.zTile); if (var1 == this.inTile) { ++this.ticksInGround; if (this.ticksInGround == 1200) { this.setDead(); } return; } this.inGround = false; this.motionX *= (double)(this.rand.nextFloat() * 0.2F); this.motionY *= (double)(this.rand.nextFloat() * 0.2F); this.motionZ *= (double)(this.rand.nextFloat() * 0.2F); this.ticksInGround = 0; this.ticksInAir = 0; } else { ++this.ticksInAir; } Vec3 var16 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ); Vec3 var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); MovingObjectPosition var3 = this.worldObj.clip(var16, var2); var16 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ); var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); if (var3 != null) { var2 = this.worldObj.getWorldVec3Pool().getVecFromPool(var3.hitVec.xCoord, var3.hitVec.yCoord, var3.hitVec.zCoord); } if (!this.worldObj.isRemote) { Entity var4 = null; List var5 = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D)); double var6 = 0.0D; EntityLivingBase var8 = this.getThrower(); for (int var9 = 0; var9 < var5.size(); ++var9) { Entity var10 = (Entity)var5.get(var9); if (var10.canBeCollidedWith() && (var10 != var8 || this.ticksInAir >= 5)) { float var11 = 0.3F; AxisAlignedBB var12 = var10.boundingBox.expand((double)var11, (double)var11, (double)var11); MovingObjectPosition var13 = var12.calculateIntercept(var16, var2); if (var13 != null) { double var14 = var16.distanceTo(var13.hitVec); if (var14 < var6 || var6 == 0.0D) { var4 = var10; var6 = var14; } } } } if (var4 != null) { var3 = new MovingObjectPosition(var4); } } if (var3 != null) { boolean bounce = true; if (var3.typeOfHit == EnumMovingObjectType.TILE) { int id = this.worldObj.getBlockId(var3.blockX, var3.blockY, var3.blockZ); if(id != 0) { Block block = Block.blocksList[id]; if(block != null) { if(!block.blockMaterial.isSolid()) { System.out.println("NonSolid: " + block.blockMaterial.toString()); bounce = false; } if(shouldBreakBlock(block)) { worldObj.setBlockToAir(var3.blockX, var3.blockY, var3.blockZ); String snd = "dig." + block.stepSound; if(block.blockMaterial == Material.glass) { snd = "random.glass"; } this.playSound(snd, 1.0F, 1.0F); bounce = false; this.worldObj.spawnParticle("tilecrack_" + id + "_" + this.worldObj.getBlockMetadata(var3.blockX, var3.blockY, var3.blockZ), var3.blockX + 0.5D, var3.blockY + 0.5D, var3.blockZ + 0.5D, 0D, 0D, 0D); } } } } if (var3.typeOfHit == EnumMovingObjectType.TILE && this.worldObj.getBlockId(var3.blockX, var3.blockY, var3.blockZ) == Block.portal.blockID) { this.setInPortal(); } else if(bounce) { this.bounce(var3); } } this.moveEntity(motionX, motionY, motionZ); float var17 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ); this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI); for (this.rotationPitch = (float)(Math.atan2(this.motionY, (double)var17) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F) { ; } while (this.rotationPitch - this.prevRotationPitch >= 180.0F) { this.prevRotationPitch += 360.0F; } while (this.rotationYaw - this.prevRotationYaw < -180.0F) { this.prevRotationYaw -= 360.0F; } while (this.rotationYaw - this.prevRotationYaw >= 180.0F) { this.prevRotationYaw += 360.0F; } this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F; this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F; float var18 = 0.99F; float var19 = this.getGravityVelocity(); if (this.isInWater()) { for (int var7 = 0; var7 < 4; ++var7) { float var20 = 0.25F; this.worldObj.spawnParticle("bubble", this.posX - this.motionX * (double)var20, this.posY - this.motionY * (double)var20, this.posZ - this.motionZ * (double)var20, this.motionX, this.motionY, this.motionZ); } var18 = 0.8F; } this.motionX *= (double)var18; this.motionY *= (double)var18; this.motionZ *= (double)var18; this.motionY -= (double)var19; this.setPosition(this.posX, this.posY, this.posZ); } /** * Gets the amount of gravity to apply to the thrown entity with each tick. */ protected float getGravityVelocity() { return 0.5F; } /** * Called when this EntityThrowable hits a block or entity. */ protected void bounce(MovingObjectPosition var1) { float resistance = getResistance(); if(resistance > 0.2F) worldObj.playSoundAtEntity(this, data_minefantasy.sound("Weapon.bombBounce"), resistance+0.2F, 0.75F); if(var1.sideHit == 0 && motionY > 0)//BOTTOM { motionY *= -resistance; if(motionY < 0.01 && motionY > -0.01 && this.onGround) { motionX = motionY = motionZ = 0; } } if(var1.sideHit == 1 && motionY < 0)//TOP { motionY *= -resistance; if(motionY < 0.01 && motionY > -0.01 && this.onGround) { motionX = motionY = motionZ = 0; } } if(var1.sideHit == 2 && motionZ > 0)//EAST { motionZ *= -resistance; } if(var1.sideHit == 3 && motionZ < 0)//WEST { motionZ *= -resistance; } if(var1.sideHit == 4 && motionX > 0)//NORTH { motionX *= -resistance; } if(var1.sideHit == 5 && motionX < 0)//SOUTH { motionX *= -resistance; } if(var1.entityHit != null) { this.motionX *= -0.10000000149011612D; this.motionY *= -0.10000000149011612D; this.motionZ *= -0.10000000149011612D; if(this.thrower != null) { var1.entityHit.attackEntityFrom(DamageSource.causeThrownDamage(this, thrower), 1); } } } /** * Gets the amount of energy reduced on bouncing * @return the resulting energy as a decimal */ public abstract float getResistance(); /** * (abstract) Protected helper method to write subclass entity data to NBT. */ public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) { par1NBTTagCompound.setShort("xTile", (short)this.xTile); par1NBTTagCompound.setInteger("fuse", ticksRemaining); par1NBTTagCompound.setShort("yTile", (short)this.yTile); par1NBTTagCompound.setShort("zTile", (short)this.zTile); par1NBTTagCompound.setByte("inTile", (byte)this.inTile); par1NBTTagCompound.setByte("shake", (byte)this.throwableShake); par1NBTTagCompound.setByte("inGround", (byte)(this.inGround ? 1 : 0)); if ((this.throwerName == null || this.throwerName.length() == 0) && this.thrower != null && this.thrower instanceof EntityPlayer) { this.throwerName = this.thrower.getEntityName(); } par1NBTTagCompound.setString("ownerName", this.throwerName == null ? "" : this.throwerName); } /** * (abstract) Protected helper method to read subclass entity data from NBT. */ public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) { this.xTile = par1NBTTagCompound.getShort("xTile"); this.ticksRemaining = par1NBTTagCompound.getInteger("fuse"); this.yTile = par1NBTTagCompound.getShort("yTile"); this.zTile = par1NBTTagCompound.getShort("zTile"); this.inTile = par1NBTTagCompound.getByte("inTile") & 255; this.throwableShake = par1NBTTagCompound.getByte("shake") & 255; this.inGround = par1NBTTagCompound.getByte("inGround") == 1; this.throwerName = par1NBTTagCompound.getString("ownerName"); if (this.throwerName != null && this.throwerName.length() == 0) { this.throwerName = null; } } @SideOnly(Side.CLIENT) public float getShadowSize() { return 0.0F; } public EntityLivingBase getThrower() { if (this.thrower == null && this.throwerName != null && this.throwerName.length() > 0) { this.thrower = this.worldObj.getPlayerEntityByName(this.throwerName); } return this.thrower; } public abstract int getFuseTime(); public abstract void explode(MovingObjectPosition pos); public boolean shouldBreakBlock(Block block) { return false; } }