Guest User

Untitled

a guest
May 3rd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.23 KB | None | 0 0
  1. package org.freeforums.geforce.SnipingMod.entitys;
  2.  
  3. import io.netty.buffer.ByteBuf;
  4.  
  5. import java.util.List;
  6.  
  7. import javax.vecmath.Vector3f;
  8.  
  9. import net.minecraft.block.Block;
  10. import net.minecraft.block.material.Material;
  11. import net.minecraft.entity.Entity;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.entity.player.EntityPlayerMP;
  15. import net.minecraft.init.Blocks;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.scoreboard.Team;
  19. import net.minecraft.util.DamageSource;
  20. import net.minecraft.util.EntityDamageSourceIndirect;
  21. import net.minecraft.util.MathHelper;
  22. import net.minecraft.util.MovingObjectPosition;
  23. import net.minecraft.util.Vec3;
  24. import net.minecraft.world.World;
  25. import cpw.mods.fml.common.FMLCommonHandler;
  26. import cpw.mods.fml.common.network.ByteBufUtils;
  27. import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
  28. import cpw.mods.fml.relauncher.Side;
  29.  
  30. public class EntitySniperBullet extends Entity
  31. {
  32.     private static int bulletLife = 600; //Kill bullets after 30 seconds
  33.     public Entity owner;
  34.     private int ticksInAir;
  35.     public float damage;
  36.     public boolean shotgun = false;
  37.  
  38.     public EntitySniperBullet(World world)
  39.     {
  40.         super(world);
  41.         ticksInAir = 0;
  42.         setSize(0.5F, 0.5F);
  43.     }
  44.    
  45.     /** Private partial constructor to avoid repeated code */
  46.     private EntitySniperBullet(World world, EntityLivingBase shooter, float gunDamage)
  47.     {
  48.         this(world);
  49.         owner = shooter;
  50.         damage = gunDamage;
  51.     }
  52.  
  53.     /** Method called by ItemGun for creating bullets from a hand held weapon */
  54.     public EntitySniperBullet(World world, EntityLivingBase shooter, float spread, float gunDamage, float speed, boolean shot)
  55.     {
  56.         this(world, Vec3.createVectorHelper(shooter.posX, shooter.posY + shooter.getEyeHeight(), shooter.posZ), shooter.rotationYaw, shooter.rotationPitch, shooter, spread, gunDamage, speed);
  57.         shotgun = shot;
  58.     }
  59.  
  60.     /** Machinegun / AAGun bullet constructor */
  61.     public EntitySniperBullet(World world, Vec3 origin, float yaw, float pitch, EntityLivingBase shooter, float spread, float gunDamage)
  62.     {
  63.         this(world, origin, yaw, pitch, shooter, spread, gunDamage, 3.0F);
  64.     }
  65.  
  66.     /** More generalised bullet constructor */
  67.     public EntitySniperBullet(World world, Vec3 origin, float yaw, float pitch, EntityLivingBase shooter, float spread, float gunDamage, float speed)
  68.     {
  69.         this(world, shooter, gunDamage);
  70.         damage = gunDamage;
  71.         setLocationAndAngles(origin.xCoord, origin.yCoord, origin.zCoord, yaw, pitch);
  72.         float offset = 0F;//(1F - FlansModClient.zoomProgress) * 0.16F;
  73.         posX -= MathHelper.cos((rotationYaw / 180F) * 3.141593F) * offset;
  74.         posY -= 0F;//0.10000000149011612D * (1F - FlansModClient.zoomProgress);
  75.         posZ -= MathHelper.sin((rotationYaw / 180F) * 3.141593F) * offset;
  76.         setPosition(posX, posY, posZ);
  77.         yOffset = 0.0F;
  78.         motionX = -MathHelper.sin((rotationYaw / 180F) * 3.141593F) * MathHelper.cos((rotationPitch / 180F) * 3.141593F);
  79.         motionZ = MathHelper.cos((rotationYaw / 180F) * 3.141593F) * MathHelper.cos((rotationPitch / 180F) * 3.141593F);
  80.         motionY = -MathHelper.sin((rotationPitch / 180F) * 3.141593F);
  81.         setArrowHeading(motionX, motionY, motionZ, spread / 2F, speed);
  82.     }
  83.    
  84.     /**  */
  85.     public EntitySniperBullet(World world, Vector3f origin, Vector3f direction, EntityLivingBase shooter, float spread, float gunDamage, float speed)
  86.     {
  87.         this(world, shooter, gunDamage);
  88.         damage = gunDamage;
  89.         setPosition(origin.x, origin.y, origin.z);
  90.         motionX = direction.x;
  91.         motionY = direction.y;
  92.         motionZ = direction.z;
  93.         setArrowHeading(motionX, motionY, motionZ, spread, speed);
  94.     }
  95.  
  96.     /** Bomb constructor. Inherits the motion and rotation of the plane */
  97.     public EntitySniperBullet(World world, Vec3 origin, float yaw, float pitch, double motX, double motY, double motZ, EntityLivingBase shooter, float gunDamage)
  98.     {
  99.         super(world);
  100.         ticksInAir = 0;
  101.         owner = shooter;
  102.         damage = gunDamage;
  103.         setSize(0.5F, 0.5F);
  104.         setLocationAndAngles(origin.xCoord, origin.yCoord, origin.zCoord, yaw, pitch);
  105.         setPosition(posX, posY, posZ);
  106.         yOffset = 0.0F;
  107.         motionX = motX;
  108.         motionY = motY;
  109.         motionZ = motZ;
  110.     }
  111.  
  112.    
  113.     protected void entityInit()
  114.     {
  115.     }
  116.  
  117.     public void setArrowHeading(double d, double d1, double d2, float spread, float speed)
  118.     {
  119.         float f2 = MathHelper.sqrt_double(d * d + d1 * d1 + d2 * d2);
  120.         d /= f2;
  121.         d1 /= f2;
  122.         d2 /= f2;
  123.         d += rand.nextGaussian() * 0.005D * spread;
  124.         d1 += rand.nextGaussian() * 0.005D * spread;
  125.         d2 += rand.nextGaussian() * 0.005D * spread;
  126.         d *= speed;
  127.         d1 *= speed;
  128.         d2 *= speed;
  129.         motionX = d;
  130.         motionY = d1;
  131.         motionZ = d2;
  132.         float f3 = MathHelper.sqrt_double(d * d + d2 * d2);
  133.         prevRotationYaw = rotationYaw = (float) ((Math.atan2(d, d2) * 180D) / 3.1415927410125732D);
  134.         prevRotationPitch = rotationPitch = (float) ((Math.atan2(d1, f3) * 180D) / 3.1415927410125732D);
  135.     }
  136.  
  137.    
  138.     public void setVelocity(double d, double d1, double d2)
  139.     {
  140.         motionX = d;
  141.         motionY = d1;
  142.         motionZ = d2;
  143.         if (prevRotationPitch == 0.0F && prevRotationYaw == 0.0F)
  144.         {
  145.             float f = MathHelper.sqrt_double(d * d + d2 * d2);
  146.             prevRotationYaw = rotationYaw = (float) ((Math.atan2(d, d2) * 180D) / 3.1415927410125732D);
  147.             prevRotationPitch = rotationPitch = (float) ((Math.atan2(d1, f) * 180D) / 3.1415927410125732D);
  148.             setLocationAndAngles(posX, posY, posZ, rotationYaw, rotationPitch);
  149.         }
  150.     }
  151.  
  152.    
  153.     public void onUpdate()
  154.     {
  155.         super.onUpdate();
  156.        
  157.         if(ticksExisted > bulletLife)
  158.         {
  159.             setDead();
  160.         }
  161.        
  162.         //Iterate over all EntityDriveables
  163.         for(int i = 0; i < worldObj.loadedEntityList.size(); i++)
  164.         {
  165.             Object obj = worldObj.loadedEntityList.get(i);
  166.         }
  167.        
  168.         //Ray trace the bullet by comparing its next position to its current position
  169.         Vec3 posVec = Vec3.createVectorHelper(posX, posY, posZ);
  170.         Vec3 nextPosVec = Vec3.createVectorHelper(posX + motionX, posY + motionY, posZ + motionZ);
  171.         MovingObjectPosition hit = worldObj.func_147447_a(posVec, nextPosVec, false, true, true);
  172.        
  173.         //Reset the position vectors since the ray tracer messes them up
  174.         posVec = Vec3.createVectorHelper(posX, posY, posZ);
  175.         nextPosVec = Vec3.createVectorHelper(posX + motionX, posY + motionY, posZ + motionZ);
  176.        
  177.         //If there is something in the way of the bullet's motion, put the bullet at that position next tick
  178.         if(hit != null)
  179.         {
  180.             nextPosVec = Vec3.createVectorHelper(hit.hitVec.xCoord, hit.hitVec.yCoord, hit.hitVec.zCoord);
  181.         }
  182.         //If we hit something
  183.         if (!isDead && hit != null)
  184.         {
  185.            
  186.             //If the hit wasn't an entity hit, then it must've been a block hit
  187.             if(hit.entityHit == null)
  188.             {
  189.                 int xTile = hit.blockX;
  190.                 int yTile = hit.blockY;
  191.                 int zTile = hit.blockZ;
  192.                 Block block = worldObj.getBlock(xTile, yTile, zTile);
  193.                 Material mat = block.getMaterial();
  194.                 //If the bullet breaks glass, and can do so according to FlansMod, do so.
  195.             }
  196.         }
  197.        
  198.        
  199.         //If the bullet was not stopped by a block
  200.         if(!isDead)
  201.         {
  202.             //Iterate over entities close to the bullet to see if any of them have been hit and hit them
  203.             List list = worldObj.getEntitiesWithinAABBExcludingEntity(this, boundingBox.addCoord(motionX, motionY, motionZ).expand(0.5F, 0.5F, 0.5F));
  204.             for (int l = 0; l < list.size(); l++)
  205.             {
  206.                 Entity checkEntity = (Entity) list.get(l);
  207.                
  208.                 //Stop the bullet hitting stuff that can't be collided with or the person shooting immediately after firing it
  209.                 if (!checkEntity.canBeCollidedWith() || checkEntity == owner) //TODO !checkEntity.canBeCollidedWith() ||
  210.                 {
  211.                     System.out.println("Can not be collided with!");
  212.                     continue;
  213.                 }
  214.                 //Calculate the hit damage
  215.                 float hitDamage = damage;
  216.                 //Create a damage source object
  217.                 //DamageSource damagesource = owner == null ? DamageSource.generic : getBulletDamage();
  218.                 DamageSource damagesource = DamageSource.generic;
  219.                
  220.                 //Attack the entity!
  221.                 if(checkEntity.attackEntityFrom(damagesource, hitDamage))
  222.                 {
  223.                     System.out.println("Attacking entity!");
  224.                     //If the attack was allowed and the entity is alive, we should remove their immortality cooldown so we can shoot them again. Without this, any rapid fire gun become useless
  225.                     if (checkEntity instanceof EntityLivingBase)
  226.                     {
  227.                         ((EntityLivingBase) checkEntity).arrowHitTimer++;
  228.                         ((EntityLivingBase) checkEntity).hurtResistantTime = ((EntityLivingBase) checkEntity).maxHurtResistantTime / 2;
  229.                     }
  230.                     //Yuck.
  231.                     //PacketDispatcher.sendPacketToAllAround(posX, posY, posZ, 50, dimension, PacketPlaySound.buildSoundPacket(posX, posY, posZ, type.hitSound, true));
  232.                 }
  233.                 //Unless the bullet penetrates, kill it
  234.                 if(checkEntity != null && checkEntity instanceof EntityLivingBase)
  235.                 {
  236.                     setPosition(checkEntity.posX, checkEntity.posY, checkEntity.posZ);
  237.                     setDead();
  238.                     break;
  239.                 }
  240.             }
  241.         }
  242.    
  243.         //Apply motion
  244.         posX += motionX;
  245.         posY += motionY;
  246.         posZ += motionZ;
  247.        
  248.         //Recalculate the angles from the new motion
  249.         float motionXZ = MathHelper.sqrt_double(motionX * motionX + motionZ * motionZ);
  250.         rotationYaw = (float) ((Math.atan2(motionX, motionZ) * 180D) / 3.1415927410125732D);
  251.         rotationPitch = (float) ((Math.atan2(motionY, motionXZ) * 180D) / 3.1415927410125732D);
  252.         //Reset the range of the angles
  253.         for (; rotationPitch - prevRotationPitch < -180F; prevRotationPitch -= 360F){}
  254.         for (; rotationPitch - prevRotationPitch >= 180F; prevRotationPitch += 360F){}
  255.         for (; rotationYaw - prevRotationYaw < -180F; prevRotationYaw -= 360F){}
  256.         for (; rotationYaw - prevRotationYaw >= 180F; prevRotationYaw += 360F){}
  257.         rotationPitch = prevRotationPitch + (rotationPitch - prevRotationPitch) * 0.2F;
  258.         rotationYaw = prevRotationYaw + (rotationYaw - prevRotationYaw) * 0.2F;
  259.        
  260.         //Movement dampening variables
  261.         float drag = 0.99F;
  262.         float gravity = 0.02F;
  263.         //If the bullet is in water, spawn particles and increase the drag
  264.         if (isInWater())
  265.         {
  266.             for(int i = 0; i < 4; i++)
  267.             {
  268.                 float bubbleMotion = 0.25F;
  269.                 worldObj.spawnParticle("bubble", posX - motionX * bubbleMotion, posY - motionY * bubbleMotion, posZ - motionZ * bubbleMotion, motionX, motionY, motionZ);
  270.             }
  271.             drag = 0.8F;
  272.         }
  273.         motionX *= drag;
  274.         motionY *= drag;
  275.         motionZ *= drag;
  276.         motionY -= gravity * 0.05F; //TODO
  277.         setPosition(posX, posY, posZ);
  278.        
  279.        
  280.     }
  281.  
  282.     public void writeEntityToNBT(NBTTagCompound tag)
  283.     {
  284.         if (owner == null)
  285.             tag.setString("owner", "null");
  286.         else
  287.             tag.setString("owner", owner.getCommandSenderName());
  288.     }
  289.  
  290.    
  291.     public void readEntityFromNBT(NBTTagCompound tag)
  292.     {
  293.         String ownerName = tag.getString("owner");
  294.        
  295.        
  296.        
  297.         if (ownerName != null && !ownerName.equals("null")){
  298.             owner = FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().getPlayerForUsername(ownerName);
  299.         }
  300.     }
  301.  
  302.     public float getShadowSize()
  303.     {
  304.         return 0.1F;
  305.     }
  306.        
  307.     public int getBrightnessForRender(float par1)
  308.     {
  309.    
  310.         int i = MathHelper.floor_double(this.posX);
  311.         int j = MathHelper.floor_double(this.posZ);
  312.        
  313.         if (this.worldObj.blockExists(i, 0, j))
  314.         {
  315.             double d0 = (this.boundingBox.maxY - this.boundingBox.minY) * 0.66D;
  316.             int k = MathHelper.floor_double(this.posY - (double)this.yOffset + d0);
  317.             return this.worldObj.getLightBrightnessForSkyBlocks(i, k, j, 0);
  318.         }
  319.         else
  320.         {
  321.             return 0;
  322.         }
  323.     }
  324.    
  325. }
Advertisement
Add Comment
Please, Sign In to add comment