Advertisement
robin4002

EntityTestArrow

Jan 19th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.15 KB | None | 0 0
  1. package tutoriel.common;
  2.  
  3. import java.util.List;
  4.  
  5. import com.google.common.io.ByteArrayDataInput;
  6. import com.google.common.io.ByteArrayDataOutput;
  7.  
  8. import net.minecraft.block.Block;
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.IProjectile;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.potion.Potion;
  17. import net.minecraft.potion.PotionEffect;
  18. import net.minecraft.util.AxisAlignedBB;
  19. import net.minecraft.util.MathHelper;
  20. import net.minecraft.util.MovingObjectPosition;
  21. import net.minecraft.util.Vec3;
  22. import net.minecraft.world.World;
  23. import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
  24. import cpw.mods.fml.relauncher.Side;
  25. import cpw.mods.fml.relauncher.SideOnly;
  26.  
  27. public class EntityTestArrow extends Entity implements IProjectile, IEntityAdditionalSpawnData
  28. {
  29.     private int xTile = -1;
  30.     private int yTile = -1;
  31.     private int zTile = -1;
  32.     private int inTile;
  33.     private int inData;
  34.     private boolean inGround;
  35.  
  36.     public int canBePickedUp;
  37.  
  38.     public int arrowShake;
  39.  
  40.     public Entity shootingEntity;
  41.     private int ticksInGround;
  42.     private int ticksInAir;
  43.     private double damage = 2.0D;
  44.  
  45.     private int knockbackStrength;
  46.  
  47.     public EntityTestArrow(World world)
  48.     {
  49.         super(world);
  50.         this.renderDistanceWeight = 10.0D;
  51.         this.setSize(0.5F, 0.5F);
  52.     }
  53.  
  54.     public EntityTestArrow(World world, double x, double y, double z)
  55.     {
  56.         super(world);
  57.         this.renderDistanceWeight = 10.0D;
  58.         this.setSize(0.5F, 0.5F);
  59.         this.setPosition(x, y, z);
  60.         this.yOffset = 0.0F;
  61.     }
  62.  
  63.     public EntityTestArrow(World world, EntityLivingBase living, EntityLivingBase par3EntityLivingBase, float y, float par5)
  64.     {
  65.         super(world);
  66.         this.renderDistanceWeight = 10.0D;
  67.         this.shootingEntity = living;
  68.  
  69.         if(living instanceof EntityPlayer)
  70.         {
  71.             this.canBePickedUp = 1;
  72.         }
  73.  
  74.         this.posY = living.posY + (double)living.getEyeHeight() - 0.10000000149011612D;
  75.         double d0 = par3EntityLivingBase.posX - living.posX;
  76.         double d1 = par3EntityLivingBase.boundingBox.minY + (double)(par3EntityLivingBase.height / 3.0F) - this.posY;
  77.         double d2 = par3EntityLivingBase.posZ - living.posZ;
  78.         double d3 = (double)MathHelper.sqrt_double(d0 * d0 + d2 * d2);
  79.  
  80.         if(d3 >= 1.0E-7D)
  81.         {
  82.             float f2 = (float)(Math.atan2(d2, d0) * 180.0D / Math.PI) - 90.0F;
  83.             float f3 = (float)(-(Math.atan2(d1, d3) * 180.0D / Math.PI));
  84.             double d4 = d0 / d3;
  85.             double d5 = d2 / d3;
  86.             this.setLocationAndAngles(living.posX + d4, this.posY, living.posZ + d5, f2, f3);
  87.             this.yOffset = 0.0F;
  88.             float f4 = (float)d3 * 0.2F;
  89.             this.setThrowableHeading(d0, d1 + (double)f4, d2, y, par5);
  90.         }
  91.     }
  92.  
  93.     public EntityTestArrow(World world, EntityLivingBase living, float par3)
  94.     {
  95.         super(world);
  96.         this.renderDistanceWeight = 10.0D;
  97.         this.shootingEntity = living;
  98.  
  99.         if(living instanceof EntityPlayer)
  100.         {
  101.             this.canBePickedUp = 1;
  102.         }
  103.  
  104.         this.setSize(0.5F, 0.5F);
  105.         this.setLocationAndAngles(living.posX, living.posY + (double)living.getEyeHeight(), living.posZ, living.rotationYaw, living.rotationPitch);
  106.         this.posX -= (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);
  107.         this.posY -= 0.10000000149011612D;
  108.         this.posZ -= (double)(MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F);
  109.         this.setPosition(this.posX, this.posY, this.posZ);
  110.         this.yOffset = 0.0F;
  111.         this.motionX = (double)(-MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI));
  112.         this.motionZ = (double)(MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI));
  113.         this.motionY = (double)(-MathHelper.sin(this.rotationPitch / 180.0F * (float)Math.PI));
  114.         this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, par3 * 1.5F, 1.0F);
  115.     }
  116.  
  117.     protected void entityInit()
  118.     {
  119.         this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
  120.     }
  121.  
  122.     public void setThrowableHeading(double par1, double par3, double par5, float par7, float par8)
  123.     {
  124.         float f2 = MathHelper.sqrt_double(par1 * par1 + par3 * par3 + par5 * par5);
  125.         par1 /= (double)f2;
  126.         par3 /= (double)f2;
  127.         par5 /= (double)f2;
  128.         par1 += this.rand.nextGaussian() * (double)(this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double)par8;
  129.         par3 += this.rand.nextGaussian() * (double)(this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double)par8;
  130.         par5 += this.rand.nextGaussian() * (double)(this.rand.nextBoolean() ? -1 : 1) * 0.007499999832361937D * (double)par8;
  131.         par1 *= (double)par7;
  132.         par3 *= (double)par7;
  133.         par5 *= (double)par7;
  134.         this.motionX = par1;
  135.         this.motionY = par3;
  136.         this.motionZ = par5;
  137.         float f3 = MathHelper.sqrt_double(par1 * par1 + par5 * par5);
  138.         this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(par1, par5) * 180.0D / Math.PI);
  139.         this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(par3, (double)f3) * 180.0D / Math.PI);
  140.         this.ticksInGround = 0;
  141.     }
  142.  
  143.     @SideOnly(Side.CLIENT)
  144.     public void setPositionAndRotation2(double par1, double par3, double par5, float par7, float par8, int par9)
  145.     {
  146.         this.setPosition(par1, par3, par5);
  147.         this.setRotation(par7, par8);
  148.     }
  149.  
  150.     @SideOnly(Side.CLIENT)
  151.     public void setVelocity(double par1, double par3, double par5)
  152.     {
  153.         this.motionX = par1;
  154.         this.motionY = par3;
  155.         this.motionZ = par5;
  156.  
  157.         if(this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
  158.         {
  159.             float f = MathHelper.sqrt_double(par1 * par1 + par5 * par5);
  160.             this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(par1, par5) * 180.0D / Math.PI);
  161.             this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(par3, (double)f) * 180.0D / Math.PI);
  162.             this.prevRotationPitch = this.rotationPitch;
  163.             this.prevRotationYaw = this.rotationYaw;
  164.             this.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);
  165.             this.ticksInGround = 0;
  166.         }
  167.     }
  168.  
  169.     public void onUpdate()
  170.     {
  171.         super.onUpdate();
  172.  
  173.         if(this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
  174.         {
  175.             float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
  176.             this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
  177.             this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(this.motionY, (double)f) * 180.0D / Math.PI);
  178.         }
  179.  
  180.         int i = this.worldObj.getBlockId(this.xTile, this.yTile, this.zTile);
  181.  
  182.         if(i > 0)
  183.         {
  184.             Block.blocksList[i].setBlockBoundsBasedOnState(this.worldObj, this.xTile, this.yTile, this.zTile);
  185.             AxisAlignedBB axisalignedbb = Block.blocksList[i].getCollisionBoundingBoxFromPool(this.worldObj, this.xTile, this.yTile, this.zTile);
  186.  
  187.             if(axisalignedbb != null && axisalignedbb.isVecInside(this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ)))
  188.             {
  189.                 this.inGround = true;
  190.             }
  191.         }
  192.  
  193.         if(this.arrowShake > 0)
  194.         {
  195.             --this.arrowShake;
  196.         }
  197.  
  198.         if(this.inGround)
  199.         {
  200.             int j = this.worldObj.getBlockId(this.xTile, this.yTile, this.zTile);
  201.             int k = this.worldObj.getBlockMetadata(this.xTile, this.yTile, this.zTile);
  202.  
  203.             if(j == this.inTile && k == this.inData)
  204.             {
  205.                 ++this.ticksInGround;
  206.  
  207.                 if(this.ticksInGround == 1200)
  208.                 {
  209.                     this.setDead();
  210.                 }
  211.             }
  212.             else
  213.             {
  214.                 this.inGround = false;
  215.                 this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
  216.                 this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
  217.                 this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
  218.                 this.ticksInGround = 0;
  219.                 this.ticksInAir = 0;
  220.             }
  221.         }
  222.         else
  223.         {
  224.             ++this.ticksInAir;
  225.             Vec3 vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
  226.             Vec3 vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  227.             MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks_do_do(vec3, vec31, false, true);
  228.             vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
  229.             vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  230.  
  231.             if(movingobjectposition != null)
  232.             {
  233.                 vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
  234.             }
  235.  
  236.             Entity entity = null;
  237.             List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
  238.             double d0 = 0.0D;
  239.             int l;
  240.             float f1;
  241.  
  242.             for(l = 0; l < list.size(); ++l)
  243.             {
  244.                 Entity entity1 = (Entity)list.get(l);
  245.  
  246.                 if(entity1.canBeCollidedWith() && (entity1 != this.shootingEntity || this.ticksInAir >= 5))
  247.                 {
  248.                     f1 = 0.3F;
  249.                     AxisAlignedBB axisalignedbb1 = entity1.boundingBox.expand((double)f1, (double)f1, (double)f1);
  250.                     MovingObjectPosition movingobjectposition1 = axisalignedbb1.calculateIntercept(vec3, vec31);
  251.  
  252.                     if(movingobjectposition1 != null)
  253.                     {
  254.                         double d1 = vec3.distanceTo(movingobjectposition1.hitVec);
  255.  
  256.                         if(d1 < d0 || d0 == 0.0D)
  257.                         {
  258.                             entity = entity1;
  259.                             d0 = d1;
  260.                         }
  261.                     }
  262.                 }
  263.             }
  264.  
  265.             if(entity != null)
  266.             {
  267.                 movingobjectposition = new MovingObjectPosition(entity);
  268.             }
  269.  
  270.             if(movingobjectposition != null && movingobjectposition.entityHit != null && movingobjectposition.entityHit instanceof EntityPlayer)
  271.             {
  272.                 EntityPlayer entityplayer = (EntityPlayer)movingobjectposition.entityHit;
  273.  
  274.                 if(entityplayer.capabilities.disableDamage || this.shootingEntity instanceof EntityPlayer && !((EntityPlayer)this.shootingEntity).canAttackPlayer(entityplayer))
  275.                 {
  276.                     movingobjectposition = null;
  277.                 }
  278.             }
  279.  
  280.             float f2;
  281.             float f3;
  282.  
  283.             if(movingobjectposition != null)
  284.             {
  285.                 if(movingobjectposition.entityHit != null)
  286.                 {
  287.                     f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  288.                     int i1 = MathHelper.ceiling_double_int((double)f2 * this.damage);
  289.  
  290.                     if(movingobjectposition.entityHit instanceof EntityLivingBase)
  291.                     {
  292.                         EntityLivingBase living = (EntityLivingBase)movingobjectposition.entityHit;
  293.                         living.addPotionEffect(new PotionEffect(Potion.heal.id, 1, 3));
  294.                     }
  295.                     this.motionX *= -0.10000000149011612D;
  296.                     this.motionY *= -0.10000000149011612D;
  297.                     this.motionZ *= -0.10000000149011612D;
  298.                     this.rotationYaw += 180.0F;
  299.                     this.prevRotationYaw += 180.0F;
  300.                     this.ticksInAir = 0;
  301.                 }
  302.                 else
  303.                 {
  304.                     this.xTile = movingobjectposition.blockX;
  305.                     this.yTile = movingobjectposition.blockY;
  306.                     this.zTile = movingobjectposition.blockZ;
  307.                     this.inTile = this.worldObj.getBlockId(this.xTile, this.yTile, this.zTile);
  308.                     this.inData = this.worldObj.getBlockMetadata(this.xTile, this.yTile, this.zTile);
  309.                     this.motionX = (double)((float)(movingobjectposition.hitVec.xCoord - this.posX));
  310.                     this.motionY = (double)((float)(movingobjectposition.hitVec.yCoord - this.posY));
  311.                     this.motionZ = (double)((float)(movingobjectposition.hitVec.zCoord - this.posZ));
  312.                     f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
  313.                     this.posX -= this.motionX / (double)f2 * 0.05000000074505806D;
  314.                     this.posY -= this.motionY / (double)f2 * 0.05000000074505806D;
  315.                     this.posZ -= this.motionZ / (double)f2 * 0.05000000074505806D;
  316.                     this.playSound("random.bowhit", 1.0F, 1.2F / (this.rand.nextFloat() * 0.2F + 0.9F));
  317.                     this.inGround = true;
  318.                     this.arrowShake = 7;
  319.  
  320.                     if(this.inTile != 0)
  321.                     {
  322.                         Block.blocksList[this.inTile].onEntityCollidedWithBlock(this.worldObj, this.xTile, this.yTile, this.zTile, this);
  323.                     }
  324.                 }
  325.             }
  326.  
  327.             this.posX += this.motionX;
  328.             this.posY += this.motionY;
  329.             this.posZ += this.motionZ;
  330.             f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
  331.             this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
  332.  
  333.             for(this.rotationPitch = (float)(Math.atan2(this.motionY, (double)f2) * 180.0D / Math.PI); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
  334.             {
  335.                 ;
  336.             }
  337.  
  338.             while(this.rotationPitch - this.prevRotationPitch >= 180.0F)
  339.             {
  340.                 this.prevRotationPitch += 360.0F;
  341.             }
  342.  
  343.             while(this.rotationYaw - this.prevRotationYaw < -180.0F)
  344.             {
  345.                 this.prevRotationYaw -= 360.0F;
  346.             }
  347.  
  348.             while(this.rotationYaw - this.prevRotationYaw >= 180.0F)
  349.             {
  350.                 this.prevRotationYaw += 360.0F;
  351.             }
  352.  
  353.             this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
  354.             this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
  355.             float f4 = 0.99F;
  356.             f1 = 0.05F;
  357.  
  358.             if(this.isInWater())
  359.             {
  360.                 for(int j1 = 0; j1 < 4; ++j1)
  361.                 {
  362.                     f3 = 0.25F;
  363.                     this.worldObj.spawnParticle("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);
  364.                 }
  365.  
  366.                 f4 = 0.8F;
  367.             }
  368.  
  369.             this.motionX *= (double)f4;
  370.             this.motionY *= (double)f4;
  371.             this.motionZ *= (double)f4;
  372.             this.motionY -= (double)f1;
  373.             this.setPosition(this.posX, this.posY, this.posZ);
  374.             this.doBlockCollisions();
  375.         }
  376.     }
  377.  
  378.     public void writeEntityToNBT(NBTTagCompound nbtTag)
  379.     {
  380.         nbtTag.setShort("xTile", (short)this.xTile);
  381.         nbtTag.setShort("yTile", (short)this.yTile);
  382.         nbtTag.setShort("zTile", (short)this.zTile);
  383.         nbtTag.setByte("inTile", (byte)this.inTile);
  384.         nbtTag.setByte("inData", (byte)this.inData);
  385.         nbtTag.setByte("shake", (byte)this.arrowShake);
  386.         nbtTag.setByte("inGround", (byte)(this.inGround ? 1 : 0));
  387.         nbtTag.setByte("pickup", (byte)this.canBePickedUp);
  388.         nbtTag.setDouble("damage", this.damage);
  389.     }
  390.  
  391.     public void readEntityFromNBT(NBTTagCompound nbtTag)
  392.     {
  393.         this.xTile = nbtTag.getShort("xTile");
  394.         this.yTile = nbtTag.getShort("yTile");
  395.         this.zTile = nbtTag.getShort("zTile");
  396.         this.inTile = nbtTag.getByte("inTile") & 255;
  397.         this.inData = nbtTag.getByte("inData") & 255;
  398.         this.arrowShake = nbtTag.getByte("shake") & 255;
  399.         this.inGround = nbtTag.getByte("inGround") == 1;
  400.  
  401.         if(nbtTag.hasKey("damage"))
  402.         {
  403.             this.damage = nbtTag.getDouble("damage");
  404.         }
  405.  
  406.         if(nbtTag.hasKey("pickup"))
  407.         {
  408.             this.canBePickedUp = nbtTag.getByte("pickup");
  409.         }
  410.         else if(nbtTag.hasKey("player"))
  411.         {
  412.             this.canBePickedUp = nbtTag.getBoolean("player") ? 1 : 0;
  413.         }
  414.     }
  415.  
  416.     public void onCollideWithPlayer(EntityPlayer par1EntityPlayer)
  417.     {
  418.         if(!this.worldObj.isRemote && this.inGround && this.arrowShake <= 0)
  419.         {
  420.             boolean flag = this.canBePickedUp == 1 || this.canBePickedUp == 2 && par1EntityPlayer.capabilities.isCreativeMode;
  421.  
  422.             if(this.canBePickedUp == 1 && !par1EntityPlayer.inventory.addItemStackToInventory(new ItemStack(Item.arrow, 1)))
  423.             {
  424.                 flag = false;
  425.             }
  426.  
  427.             if(flag)
  428.             {
  429.                 this.playSound("random.pop", 0.2F, ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
  430.                 par1EntityPlayer.onItemPickup(this, 1);
  431.                 this.setDead();
  432.             }
  433.         }
  434.     }
  435.    
  436.     protected boolean canTriggerWalking()
  437.     {
  438.         return false;
  439.     }
  440.  
  441.     @SideOnly(Side.CLIENT)
  442.     public float getShadowSize()
  443.     {
  444.         return 0.0F;
  445.     }
  446.  
  447.     public boolean canAttackWithItem()
  448.     {
  449.         return false;
  450.     }
  451.  
  452.     @Override
  453.     public void writeSpawnData(ByteArrayDataOutput data)
  454.     {
  455.         data.writeShort((short)this.xTile);
  456.         data.writeShort((short)this.yTile);
  457.         data.writeShort((short)this.zTile);
  458.         data.writeByte((byte)this.inTile);
  459.         data.writeByte((byte)this.inData);
  460.         data.writeByte((byte)this.arrowShake);
  461.         data.writeByte((byte)(this.inGround ? 1 : 0));
  462.     }
  463.  
  464.     @Override
  465.     public void readSpawnData(ByteArrayDataInput data)
  466.     {
  467.         this.xTile = data.readShort();
  468.         this.yTile = data.readShort();
  469.         this.zTile = data.readShort();
  470.         this.inTile = data.readByte() & 255;
  471.         this.inData = data.readByte() & 255;
  472.         this.arrowShake = data.readByte() & 255;
  473.         this.inGround = data.readByte() == 1;
  474.     }
  475. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement