Guest User

Untitled

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