Guest User

Untitled

a guest
Jul 28th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 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.init.Blocks;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.network.datasync.DataParameter;
  13. import net.minecraft.network.datasync.DataSerializers;
  14. import net.minecraft.network.datasync.EntityDataManager;
  15. import net.minecraft.util.EnumParticleTypes;
  16. import net.minecraft.util.math.AxisAlignedBB;
  17. import net.minecraft.util.math.RayTraceResult;
  18. import net.minecraft.util.math.Vec3d;
  19. import net.minecraft.world.World;
  20.  
  21. /**
  22.  * @Author Melonslise
  23.  */
  24. public abstract class EntityProjectile extends Entity // TODO Rename to magic projectile (?)
  25. {
  26.     private static final DataParameter<Integer> OWNER = EntityDataManager.<Integer>createKey(EntityProjectile.class, DataSerializers.VARINT);
  27.     /** UUID of the projectile's caster */
  28.     private UUID ownerUUID;
  29.     /** Speed is measured in blocks/tick or 20 blocks/second. */
  30.     /** Used for removing the entity if it has been alive for too long. */
  31.     private int ticksAlive; // TODO die after ticks
  32.  
  33.     public EntityProjectile(World worldIn) // TODO SET SIZE
  34.     {
  35.         super(worldIn); // TODO add player's motion
  36.     }
  37.  
  38.     public EntityProjectile(World world, EntityLivingBase caster, double speed)
  39.     {
  40.         super(world);
  41.         this.ownerUUID = caster.getUniqueID();
  42.         this.getDataManager().register(OWNER, caster.getEntityId());
  43.         this.setPosition(caster.posX, caster.posY, caster.posZ);
  44.         this.setSpeedFromEntity(caster, speed);
  45.     }
  46.  
  47.     /**
  48.      * Not sure what this is used for :confused:
  49.      */
  50.     @Override
  51.     protected abstract void entityInit();
  52.  
  53.     /**
  54.      * 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.
  55.      */
  56.     public void setLocation(double coordinateX, double coordinateY, double coordinateZ)
  57.     {
  58.         this.posX = coordinateX;
  59.         this.posY = coordinateY;
  60.         this.posZ = coordinateZ;
  61.     }
  62.  
  63.     @Override
  64.     public void onUpdate() // TODO COMBINE CODE WITH COLLISION CHECK (?)
  65.     {
  66.         super.onUpdate();
  67.  
  68.         // Effects
  69.         if(this.shouldBurn())
  70.         {
  71.             this.setFire(1);
  72.         }
  73.  
  74.         // Collision
  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.  
  89.         // Movement
  90.         this.setLocation(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  91.  
  92.         // Water collision
  93.         float multiplier = 1;
  94.         float subtrahend = 0;
  95.         if (this.isInWater())
  96.         {
  97.             if(this.diesInWater())
  98.             {
  99.                 this.setDead(); // Longer death + sound
  100.             }
  101.             else
  102.             {
  103.                 for (int j = 0; j < 4; ++j)
  104.                 {
  105.                     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]);
  106.                 }
  107.                 multiplier = 0.9F;
  108.                 subtrahend = 0.2F;
  109.             }
  110.         }
  111.  
  112.         this.motionX *= (double) multiplier; // TODO Apply actual physics formulae (probably mg-pgV)
  113.         this.motionZ *= (double) multiplier;
  114.         this.motionY -= (double) subtrahend;
  115.         // TODO set up hasnogravity and gravity velocity
  116.         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
  117.  
  118.         // Particle trail
  119.         this.spawnParticleTrail();
  120.     }
  121.  
  122.     protected RayTraceResult checkCollision()
  123.     {
  124.         Vec3d vector1 = new Vec3d(this.posX, this.posY, this.posZ);
  125.         Vec3d vector2 = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
  126.         RayTraceResult result1 = this.worldObj.rayTraceBlocks(vector1, vector2);
  127.         if (result1 != null)
  128.         {
  129.             vector2 = new Vec3d(result1.hitVec.xCoord, result1.hitVec.yCoord, result1.hitVec.zCoord);
  130.         }
  131.  
  132.         Entity entity1 = null;
  133.         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
  134.         double d0 = 0.0D;
  135.         for (int a = 0; a < entities.size(); ++a)
  136.         {
  137.             Entity entity2 = (Entity)entities.get(a);
  138.             if(entity2.canBeCollidedWith() && !entity2.equals(this.getCaster())) // Exclude entities here
  139.             {
  140.                 AxisAlignedBB aabb = entity2.getEntityBoundingBox().expandXyz(0.30000001192092896D); // Why expand?
  141.                 RayTraceResult result2 = aabb.calculateIntercept(vector1, vector2);
  142.                 if (result2 != null)
  143.                 {
  144.                     double d1 = vector1.squareDistanceTo(result2.hitVec);
  145.                     if (d1 < d0 || d0 == 0.0D)
  146.                     {
  147.                         entity1 = entity2;
  148.                         d0 = d1;
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.         if (entity1 != null)
  154.         {
  155.             result1 = new RayTraceResult(entity1);
  156.         }
  157.         return result1;
  158.     }
  159.  
  160.     /**
  161.      * Called when the projectile collides with a block or entity.
  162.      */
  163.     protected abstract void onImpact(RayTraceResult result);
  164.  
  165.     /**
  166.      * Spawns particles around the projectile. Called from {@link #onUpdate()}.
  167.      */
  168.     protected abstract void spawnParticleTrail();
  169.  
  170.     /**
  171.      * Determines if the projectile will die after colliding with water.
  172.      */
  173.     public abstract boolean diesInWater();
  174.  
  175.     /**
  176.      * Determines if the projectile should burn like a fireball.
  177.      */
  178.     public abstract boolean shouldBurn();
  179.  
  180.     /**
  181.      * Sets the projectile's speed on the three axes.
  182.      */
  183.     public void setSpeed(double speedX, double speedY, double speedZ)
  184.     {
  185.         this.motionX = speedX;
  186.         this.motionY = speedY;
  187.         this.motionZ = speedZ;
  188.     }
  189.  
  190.     /**
  191.      * Sets the projectile's speed depending on the caster's facing.
  192.      */
  193.     public void setSpeedFromEntity(EntityLivingBase caster, double speed)
  194.     {
  195.         Vec3d vector = caster.getLookVec();
  196.         this.setSpeed(vector.xCoord * speed, vector.yCoord * speed, vector.zCoord * speed);
  197.     }
  198.  
  199.     @Override
  200.     protected void readEntityFromNBT(NBTTagCompound nbt)
  201.     {
  202.         this.ownerUUID = nbt.getUniqueId("ownerUUID");
  203.     }
  204.  
  205.     @Override
  206.     protected void writeEntityToNBT(NBTTagCompound nbt) // TODO
  207.     {
  208.         nbt.setUniqueId("ownerUUID", this.ownerUUID);
  209.     }
  210.  
  211.     @Nullable
  212.     public EntityLivingBase getCaster() // TODO
  213.     {
  214.         if(!this.worldObj.isRemote)
  215.         {
  216.             return this.worldObj.getPlayerEntityByUUID(this.ownerUUID);
  217.         }
  218.         else
  219.         {
  220.             return (EntityLivingBase) this.worldObj.getEntityByID(this.getDataManager().get(OWNER));
  221.         }
  222.     }
  223.  
  224.     /** *** OLD ***
  225.     @Nullable
  226.     public EntityLivingBase getCaster()
  227.     {
  228.         if (this.caster == null && this.casterName != null && !this.casterName.isEmpty())
  229.         {
  230.             this.caster = this.worldObj.getPlayerEntityByName(this.casterName);
  231.  
  232.             if (this.caster == null && this.worldObj instanceof WorldServer)
  233.             {
  234.                 try
  235.                 {
  236.                     Entity entity = ((WorldServer)this.worldObj).getEntityFromUuid(UUID.fromString(this.casterName));
  237.                     if (entity instanceof EntityLivingBase)
  238.                     {
  239.                         this.caster = (EntityLivingBase)entity;
  240.                     }
  241.                 }
  242.                 catch (Throwable error)
  243.                 {
  244.                     this.caster = null;
  245.                 }
  246.             }
  247.         }
  248.         return this.caster;
  249.     }
  250.     */
  251. }
Advertisement
Add Comment
Please, Sign In to add comment