Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.66 KB | None | 0 0
  1. package melonslise.runicinscription.common.entity;
  2.  
  3.  
  4. import java.util.List;
  5. import java.util.UUID;
  6.  
  7. import javax.annotation.Nullable;
  8.  
  9. import net.minecraft.entity.Entity;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.player.EntityPlayer;
  12. import net.minecraft.init.Blocks;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.util.EnumParticleTypes;
  15. import net.minecraft.util.math.AxisAlignedBB;
  16. import net.minecraft.util.math.RayTraceResult;
  17. import net.minecraft.util.math.Vec3d;
  18. import net.minecraft.world.World;
  19. import net.minecraft.world.WorldServer;
  20.  
  21. /**
  22.  * @Author Melonslise on minecraftforum.net
  23.  */
  24. public class EntityProjectile extends Entity // TODO Rename to magic projectile (?)
  25. {
  26.     private EntityLivingBase caster;
  27.     private String casterName; // TODO Remove
  28.     /** Speed is measured in blocks/tick or 20 blocks/second. */ // TODO Replace speed with motion (?)
  29.     private double speedX, speedY, speedZ;
  30.  
  31.     public EntityProjectile(World worldIn) // TODO SET SIZE
  32.     {
  33.         super(worldIn); // TODO add player's motion
  34.     }
  35.  
  36.     public EntityProjectile(World world, EntityLivingBase caster, double speed)
  37.     {
  38.         super(world);
  39.         this.caster = caster;
  40.         this.setPosition(caster.posX, caster.posY, caster.posZ);
  41.         this.setSpeedFromEntity(caster, speed);
  42.     }
  43.  
  44.     public EntityProjectile(World world, EntityLivingBase caster, double speedX, double speedY, double speedZ)
  45.     {
  46.         super(world);
  47.         this.caster = caster;
  48.         this.setPosition(caster.posX, caster.posY,caster.posZ);
  49.         this.setSpeed(speedX, speedY, speedZ); // TODO inacc
  50.     }
  51.  
  52.     /**
  53.      * Not sure what this is used for :confused:
  54.      */
  55.     @Override
  56.     protected void entityInit()
  57.     {
  58.     }
  59.  
  60.     /**
  61.      * Sets the projectile's position in the world. Does the same thing as {@link #setPosition(double, double, double)}, but does not update the bounding box.
  62.      */
  63.     public void setLocation(double coordinateX, double coordinateY, double coordinateZ)
  64.     {
  65.         this.posX = coordinateX;
  66.         this.posY = coordinateY;
  67.         this.posZ = coordinateZ;
  68.     }
  69.  
  70.     @Override
  71.     public void onUpdate() // TODO COMBINE CODE WITH COLLISION CHECK (?)
  72.     {
  73.         // Collision
  74.         super.onUpdate();
  75.         RayTraceResult result = this.checkCollision();
  76.         if (result != null)
  77.         {
  78.             if (result.typeOfHit == RayTraceResult.Type.BLOCK && this.worldObj.getBlockState(result.getBlockPos()).getBlock() == Blocks.PORTAL)
  79.             {
  80.                 this.setPortal(result.getBlockPos());
  81.             }
  82.             else
  83.             {
  84.                 //if(!net.minecraftforge.common.ForgeHooks.onThrowableImpact(this, raytraceresult)) // TODO OWN HOOK
  85.                 this.onImpact(result);
  86.             }
  87.         }
  88.         // Movement
  89.         this.setLocation(this.posX + this.speedX, this.posY + this.speedY, this.posZ + this.speedZ);
  90.         // Water collision
  91.         float multiplier = 1;
  92.         if (this.isInWater()) // TODO FIX + DEATH
  93.         {
  94.             for (int j = 0; j < 4; ++j)
  95.             {
  96.                 float f3 = 0.25F;
  97.                 this.worldObj.spawnParticle(EnumParticleTypes.WATER_BUBBLE, this.posX - this.speedX * 0.25D, this.posY - this.speedY * 0.25D, this.posZ - this.speedZ * 0.25D, this.speedX, this.speedY, this.speedZ, new int[0]);
  98.             }
  99.             multiplier = 0.8F;
  100.         }
  101.         this.speedX *= (double) multiplier;
  102.         this.speedY *= (double) multiplier;
  103.         this.speedZ *= (double) multiplier;
  104.         // TODO set up hasnogravity and gravity velocity
  105.         this.setPosition(this.posX, this.posY, this.posZ); // Not sure why this is needed, but I suspect it has something to do with it's aabb
  106.         // TODO Particle trail
  107.         this.spawnParticleTrail();
  108.     }
  109.  
  110.     protected RayTraceResult checkCollision()
  111.     {
  112.         if(this.shouldBurn())
  113.         {
  114.             this.setFire(1);
  115.         }
  116.         Vec3d vector1 = new Vec3d(this.posX, this.posY, this.posZ);
  117.         Vec3d vector2 = new Vec3d(this.posX + this.speedX, this.posY + this.speedY, this.posZ + this.speedZ);
  118.         RayTraceResult result1 = this.worldObj.rayTraceBlocks(vector1, vector2);
  119.         if (result1 != null)
  120.         {
  121.             vector2 = new Vec3d(result1.hitVec.xCoord, result1.hitVec.yCoord, result1.hitVec.zCoord);
  122.         }
  123.         Entity entity1 = null;
  124.         List<Entity> entities = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.speedX, this.speedY, this.speedZ).expandXyz(1.0D)); // TODO FIGURE OUT WHY EXPAND BY 1
  125.         double d0 = 0.0D;
  126.         for (int a = 0; a < entities.size(); ++a)
  127.         {
  128.             Entity entity2 = (Entity)entities.get(a);
  129.             if(entity2.canBeCollidedWith() && entity2 != this.caster) // Exclude entities here
  130.             {
  131.                 AxisAlignedBB aabb = entity2.getEntityBoundingBox().expandXyz(0.30000001192092896D); // Why expand?
  132.                 RayTraceResult result2 = aabb.calculateIntercept(vector1, vector2);
  133.                 if (result2 != null)
  134.                 {
  135.                     double d1 = vector1.squareDistanceTo(result2.hitVec);
  136.                     if (d1 < d0 || d0 == 0.0D)
  137.                     {
  138.                         entity1 = entity2;
  139.                         d0 = d1;
  140.                     }
  141.                 }
  142.             }
  143.         }
  144.         if (entity1 != null)
  145.         {
  146.             result1 = new RayTraceResult(entity1);
  147.         }
  148.         return result1;
  149.     }
  150.  
  151.     /**
  152.      * Called when the projectile collides with a block or entity.
  153.      */
  154.     protected void onImpact(RayTraceResult result)
  155.     {
  156.         this.setDead();
  157.         System.out.println("ded");
  158.     }
  159.  
  160.     /**
  161.      * Spawns particles around the projectile. Called from {@link #onUpdate()}.
  162.      */
  163.     protected void spawnParticleTrail()
  164.     {
  165.        
  166.     }
  167.  
  168.     /**
  169.      * Determines if the projectile will die after colliding with water.
  170.      */
  171.     public boolean diesInWater()
  172.     {
  173.         return false;
  174.     }
  175.  
  176.     /**
  177.      * Determines if the projectile should burn like a fireball.
  178.      * @return
  179.      */
  180.     public boolean shouldBurn()
  181.     {
  182.         return false;
  183.     }
  184.  
  185.     /**
  186.      * Sets the projectile's speed on the three axes.
  187.      */
  188.     public void setSpeed(double speedX, double speedY, double speedZ)
  189.     {
  190.         this.speedX = speedX;
  191.         this.speedY = speedY;
  192.         this.speedZ = speedZ;
  193.     }
  194.  
  195.     /**
  196.      * Sets the projectile's speed depending on the caster's facing.
  197.      */
  198.     public void setSpeedFromEntity(EntityLivingBase caster, double speed)
  199.     {
  200.         Vec3d vector = caster.getLookVec();
  201.         this.setSpeed(vector.xCoord * speed, vector.yCoord * speed, vector.zCoord * speed);
  202.     }
  203.  
  204.     /**
  205.      * Return's the projectile's three-dimensional vector. The basis is measured in block/tick or 20 blocks/second.
  206.      */
  207.     public Vec3d getSpeedVector()
  208.     {
  209.         Vec3d vector = new Vec3d(this.speedX, this.speedY, this.speedZ);
  210.         return vector;
  211.     }
  212.  
  213.     @Override
  214.     protected void readEntityFromNBT(NBTTagCompound nbt) // TODO Improve caster save/read
  215.     {
  216.         // Speed
  217.         this.speedX = nbt.getDouble("SpeedX");
  218.         this.speedY = nbt.getDouble("SpeedY");
  219.         this.speedZ = nbt.getDouble("SpeedZ");
  220.         // Caster
  221.         this.caster = null;
  222.         this.casterName = nbt.getString("ownerName");
  223.         if (this.casterName != null && this.casterName.isEmpty())
  224.         {
  225.             this.casterName = null;
  226.         }
  227.         this.caster = this.getCaster();
  228.     }
  229.  
  230.     @Override
  231.     protected void writeEntityToNBT(NBTTagCompound nbt)
  232.     {
  233.         // Speed
  234.         nbt.setDouble("SpeedX", this.speedX);
  235.         nbt.setDouble("SpeedY", this.speedY);
  236.         nbt.setDouble("SpeedZ", this.speedZ);
  237.         // Caster
  238.         if ((this.casterName == null || this.casterName.isEmpty()) && this.caster instanceof EntityPlayer)
  239.         {
  240.             this.casterName = this.caster.getName();
  241.         }
  242.         nbt.setString("ownerName", this.casterName == null ? "" : this.casterName);
  243.     }
  244.  
  245.     @Nullable
  246.     public EntityLivingBase getCaster()
  247.     {
  248.         if (this.caster == null && this.casterName != null && !this.casterName.isEmpty())
  249.         {
  250.             this.caster = this.worldObj.getPlayerEntityByName(this.casterName);
  251.  
  252.             if (this.caster == null && this.worldObj instanceof WorldServer)
  253.             {
  254.                 try
  255.                 {
  256.                     Entity entity = ((WorldServer)this.worldObj).getEntityFromUuid(UUID.fromString(this.casterName));
  257.                     if (entity instanceof EntityLivingBase)
  258.                     {
  259.                         this.caster = (EntityLivingBase)entity;
  260.                     }
  261.                 }
  262.                 catch (Throwable error)
  263.                 {
  264.                     this.caster = null;
  265.                 }
  266.             }
  267.         }
  268.         return this.caster;
  269.     }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement