Guest User

EntityBulletBase

a guest
Jun 20th, 2015
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.60 KB | None | 0 0
  1. package com.svk.industrialGuns.entity;
  2.  
  3. import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
  4. import cpw.mods.fml.relauncher.Side;
  5. import cpw.mods.fml.relauncher.SideOnly;
  6.  
  7. import java.util.List;
  8.  
  9. import net.minecraft.block.Block;
  10. import net.minecraft.entity.Entity;
  11. import net.minecraft.entity.EntityLivingBase;
  12. import net.minecraft.entity.IProjectile;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.init.Blocks;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.util.AxisAlignedBB;
  17. import net.minecraft.util.MathHelper;
  18. import net.minecraft.util.MovingObjectPosition;
  19. import net.minecraft.util.Vec3;
  20. import net.minecraft.world.World;
  21.  
  22. public abstract class EntityBulletBase extends Entity implements IProjectile, IEntityAdditionalSpawnData
  23. {
  24.     private int field_145788_c = -1;
  25.     private int field_145786_d = -1;
  26.     private int field_145787_e = -1;
  27.     private Block field_145785_f;
  28.     protected boolean inGround;
  29.     public int throwableShake;
  30.     /** The entity that threw this throwable item. */
  31.     private EntityLivingBase thrower;
  32.     private String throwerName;
  33.     private int ticksInGround;
  34.     private int ticksInAir;
  35.     private static final String __OBFID = "CL_00001723";
  36.     protected boolean render;
  37.     public float addSize = 0;
  38.  
  39.     public EntityBulletBase(World p_i1776_1_)
  40.     {
  41.         super(p_i1776_1_);
  42.         this.setSize(0.25F, 0.25F);
  43.     }
  44.    
  45.     /**
  46.      * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
  47.      * length * 64 * renderDistanceWeight Args: distance
  48.      */
  49.     @SideOnly(Side.CLIENT)
  50.     public boolean isInRangeToRenderDist(double p_70112_1_)
  51.     {
  52.         if(render)
  53.         {
  54.             double d1 = this.boundingBox.getAverageEdgeLength();
  55.             d1 *= 64.0D * 256;
  56.             return p_70112_1_ < d1 * d1;
  57.         }
  58.         else
  59.         {
  60.             return false;
  61.         }
  62.     }
  63.    
  64.     public EntityBulletBase(World p_i1777_1_, EntityLivingBase p_i1777_2_)
  65.     {
  66.         super(p_i1777_1_);
  67.         this.thrower = p_i1777_2_;
  68.         this.setSize(0.25F, 0.25F);
  69.         this.setLocationAndAngles(p_i1777_2_.posX, p_i1777_2_.posY + (double)p_i1777_2_.getEyeHeight(), p_i1777_2_.posZ, p_i1777_2_.rotationYaw, p_i1777_2_.rotationPitch);
  70.         this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);
  71.         this.posY -= 0.10000000149011612D;
  72.         this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);
  73.         this.setPosition(this.posX, this.posY, this.posZ);
  74.         this.yOffset = 0.0F;
  75.         float f = 0.4F;
  76.         this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * f);
  77.         this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI) * f);
  78.         this.motionY = (double)(-MathHelper.sin((this.rotationPitch + this.func_70183_g()) / 180.0F * (float)Math.PI) * f);
  79.         this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, this.func_70182_d(), 1.0F);
  80.     }
  81.  
  82.     public EntityBulletBase(World p_i1778_1_, double p_i1778_2_, double p_i1778_4_, double p_i1778_6_)
  83.     {
  84.         super(p_i1778_1_);
  85.         this.ticksInGround = 0;
  86.         this.setSize(0.25F, 0.25F);
  87.         this.setPosition(p_i1778_2_, p_i1778_4_, p_i1778_6_);
  88.         this.yOffset = 0.0F;
  89.     }
  90.  
  91.     protected float func_70182_d()
  92.     {
  93.         return 1.5F;
  94.     }
  95.  
  96.     protected float func_70183_g()
  97.     {
  98.         return 0.0F;
  99.     }
  100.  
  101.     /**
  102.      * Similar to setArrowHeading, it's point the throwable entity to a x, y, z direction.
  103.      */
  104.     public void setThrowableHeading(double p_70186_1_, double p_70186_3_, double p_70186_5_, float p_70186_7_, float p_70186_8_)
  105.     {
  106.         float f2 = MathHelper.sqrt_double(p_70186_1_ * p_70186_1_ + p_70186_3_ * p_70186_3_ + p_70186_5_ * p_70186_5_);
  107.         p_70186_1_ /= (double)f2;
  108.         p_70186_3_ /= (double)f2;
  109.         p_70186_5_ /= (double)f2;
  110.         p_70186_1_ += this.rand.nextGaussian() * 0.007499999832361937D * (double)p_70186_8_;
  111.         p_70186_3_ += this.rand.nextGaussian() * 0.007499999832361937D * (double)p_70186_8_;
  112.         p_70186_5_ += this.rand.nextGaussian() * 0.007499999832361937D * (double)p_70186_8_;
  113.         p_70186_1_ *= (double)p_70186_7_;
  114.         p_70186_3_ *= (double)p_70186_7_;
  115.         p_70186_5_ *= (double)p_70186_7_;
  116.         this.motionX = p_70186_1_;
  117.         this.motionY = p_70186_3_;
  118.         this.motionZ = p_70186_5_;
  119.         float f3 = MathHelper.sqrt_double(p_70186_1_ * p_70186_1_ + p_70186_5_ * p_70186_5_);
  120.         this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(p_70186_1_, p_70186_5_) * 180.0D / Math.PI);
  121.         this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(p_70186_3_, (double)f3) * 180.0D / Math.PI);
  122.         this.ticksInGround = 0;
  123.     }
  124.  
  125.     /**
  126.      * Sets the velocity to the args. Args: x, y, z
  127.      */
  128.     @SideOnly(Side.CLIENT)
  129.     public void setVelocity(double p_70016_1_, double p_70016_3_, double p_70016_5_)
  130.     {
  131.         this.motionX = p_70016_1_;
  132.         this.motionY = p_70016_3_;
  133.         this.motionZ = p_70016_5_;
  134.  
  135.         if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
  136.         {
  137.             float f = MathHelper.sqrt_double(p_70016_1_ * p_70016_1_ + p_70016_5_ * p_70016_5_);
  138.             this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(p_70016_1_, p_70016_5_) * 180.0D / Math.PI);
  139.             this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(p_70016_3_, (double)f) * 180.0D / Math.PI);
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Called to update the entity's position/logic.
  145.      */
  146.     public void onUpdate()
  147.     {      
  148.         if(this.posY > 255 || this.posY < 1)
  149.         {
  150.             this.setDead();
  151.         }
  152.        
  153.         this.lastTickPosX = this.posX;
  154.         this.lastTickPosY = this.posY;
  155.         this.lastTickPosZ = this.posZ;
  156.         super.onUpdate();
  157.  
  158.         if (this.throwableShake > 0)
  159.         {
  160.             --this.throwableShake;
  161.         }
  162.  
  163.         if (this.inGround)
  164.         {
  165.             if (this.worldObj.getBlock(this.field_145788_c, this.field_145786_d, this.field_145787_e) == this.field_145785_f)
  166.             {
  167.                 ++this.ticksInGround;
  168.  
  169.                 if (this.ticksInGround == 1200)
  170.                 {
  171.                     this.setDead();
  172.                 }
  173.  
  174.                 return;
  175.             }
  176.  
  177.             this.inGround = false;
  178.             this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
  179.             this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
  180.             this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
  181.             this.ticksInGround = 0;
  182.             this.ticksInAir = 0;
  183.         }
  184.         else
  185.         {
  186.             ++this.ticksInAir;
  187.         }
  188.  
  189.         Vec3 vec3 = Vec3.createVectorHelper(this.posX, this.posY, this.posZ);
  190.         Vec3 vec31 = Vec3.createVectorHelper(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  191.         MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec3, vec31);
  192.         vec3 = Vec3.createVectorHelper(this.posX, this.posY, this.posZ);
  193.         vec31 = Vec3.createVectorHelper(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  194.  
  195.         if (movingobjectposition != null)
  196.         {
  197.             vec31 = Vec3.createVectorHelper(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
  198.         }
  199.  
  200.             Entity entity = null;
  201.             List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(2.0D, 2.0D, 2.0D));
  202.             double d0 = 0.0D;
  203.             EntityLivingBase entitylivingbase = this.getThrower();
  204.  
  205.             for (int j = 0; j < list.size(); ++j)
  206.             {
  207.                 Entity entity1 = (Entity)list.get(j);
  208.  
  209.                 if (entity1.canBeCollidedWith() && (entity1 != entitylivingbase || this.ticksInAir >= 5))
  210.                 {
  211.                     float f = 0.3F + addSize*2;
  212.                     AxisAlignedBB axisalignedbb = entity1.boundingBox.expand((double)f, (double)f, (double)f);
  213.                     MovingObjectPosition movingobjectposition1 = axisalignedbb.calculateIntercept(vec3, vec31);
  214.  
  215.                     if (movingobjectposition1 != null)
  216.                     {
  217.                         double d1 = vec3.distanceTo(movingobjectposition1.hitVec);
  218.  
  219.                         if (d1 < d0 || d0 == 0.0D)
  220.                         {
  221.                             entity = entity1;
  222.                             d0 = d1;
  223.                         }
  224.                     }
  225.                 }
  226.             }
  227.  
  228.             if (entity != null)
  229.             {
  230.                 movingobjectposition = new MovingObjectPosition(entity);
  231.             }
  232.  
  233.         if (movingobjectposition != null)
  234.         {
  235.             if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK && this.worldObj.getBlock(movingobjectposition.blockX, movingobjectposition.blockY, movingobjectposition.blockZ) == Blocks.portal)
  236.             {
  237.                 this.setInPortal();
  238.             }
  239.             else
  240.             {
  241.                 this.onImpact(movingobjectposition);
  242.             }
  243.         }
  244.  
  245.         this.posX += this.motionX;
  246.         this.posY += this.motionY;
  247.         this.posZ += this.motionZ;
  248.         float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
  249.         this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
  250.  
  251.         for (this.rotationPitch = (float)(Math.atan2(this.motionY, (double)f1) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
  252.         {
  253.             ;
  254.         }
  255.  
  256.         while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
  257.         {
  258.             this.prevRotationPitch += 360.0F;
  259.         }
  260.  
  261.         while (this.rotationYaw - this.prevRotationYaw < -180.0F)
  262.         {
  263.             this.prevRotationYaw -= 360.0F;
  264.         }
  265.  
  266.         while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
  267.         {
  268.             this.prevRotationYaw += 360.0F;
  269.         }
  270.  
  271.         this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
  272.         this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
  273.         float f2 = 0.99F;
  274.         float f3 = this.getGravityVelocity();
  275.  
  276.         if (this.isInWater())
  277.         {
  278.             for (int i = 0; i < 4; ++i)
  279.             {
  280.                 float f4 = 0.25F;
  281.                 this.worldObj.spawnParticle("bubble", this.posX - this.motionX * (double)f4, this.posY - this.motionY * (double)f4, this.posZ - this.motionZ * (double)f4, this.motionX, this.motionY, this.motionZ);
  282.             }
  283.  
  284.             f2 = 0.8F;
  285.         }
  286.  
  287.         this.motionX *= (double)f2;
  288.         this.motionY *= (double)f2;
  289.         this.motionZ *= (double)f2;
  290.         this.motionY -= (double)f3;
  291.         this.setPosition(this.posX, this.posY, this.posZ);
  292.     }
  293.  
  294.     /**
  295.      * Gets the amount of gravity to apply to the thrown entity with each tick.
  296.      */
  297.     protected float getGravityVelocity()
  298.     {
  299.         return 0.03F;
  300.     }
  301.  
  302.     /**
  303.      * Called when this EntityThrowable hits a block or entity.
  304.      */
  305.     protected abstract void onImpact(MovingObjectPosition p_70184_1_);
  306.  
  307.     /**
  308.      * (abstract) Protected helper method to write subclass entity data to NBT.
  309.      */
  310.     public void writeEntityToNBT(NBTTagCompound p_70014_1_)
  311.     {
  312.         p_70014_1_.setShort("xTile", (short)this.field_145788_c);
  313.         p_70014_1_.setShort("yTile", (short)this.field_145786_d);
  314.         p_70014_1_.setShort("zTile", (short)this.field_145787_e);
  315.         p_70014_1_.setByte("inTile", (byte)Block.getIdFromBlock(this.field_145785_f));
  316.         p_70014_1_.setByte("shake", (byte)this.throwableShake);
  317.         p_70014_1_.setByte("inGround", (byte)(this.inGround ? 1 : 0));
  318.  
  319.         if ((this.throwerName == null || this.throwerName.length() == 0) && this.thrower != null && this.thrower instanceof EntityPlayer)
  320.         {
  321.             this.throwerName = this.thrower.getCommandSenderName();
  322.         }
  323.  
  324.         p_70014_1_.setString("ownerName", this.throwerName == null ? "" : this.throwerName);
  325.     }
  326.  
  327.     /**
  328.      * (abstract) Protected helper method to read subclass entity data from NBT.
  329.      */
  330.     public void readEntityFromNBT(NBTTagCompound p_70037_1_)
  331.     {
  332.         this.field_145788_c = p_70037_1_.getShort("xTile");
  333.         this.field_145786_d = p_70037_1_.getShort("yTile");
  334.         this.field_145787_e = p_70037_1_.getShort("zTile");
  335.         this.field_145785_f = Block.getBlockById(p_70037_1_.getByte("inTile") & 255);
  336.         this.throwableShake = p_70037_1_.getByte("shake") & 255;
  337.         this.inGround = p_70037_1_.getByte("inGround") == 1;
  338.         this.throwerName = p_70037_1_.getString("ownerName");
  339.  
  340.         if (this.throwerName != null && this.throwerName.length() == 0)
  341.         {
  342.             this.throwerName = null;
  343.         }
  344.     }
  345.  
  346.     @SideOnly(Side.CLIENT)
  347.     public float getShadowSize()
  348.     {
  349.         return 0.0F;
  350.     }
  351.  
  352.     public EntityLivingBase getThrower()
  353.     {
  354.         if (this.thrower == null && this.throwerName != null && this.throwerName.length() > 0)
  355.         {
  356.             this.thrower = this.worldObj.getPlayerEntityByName(this.throwerName);
  357.         }
  358.  
  359.         return this.thrower;
  360.     }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment