Guest User

Untitled

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