Advertisement
Guest User

Code for shooting AI

a guest
Jul 15th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.59 KB | None | 0 0
  1.  
  2.             /**
  3.              * Imports that are required
  4.              */
  5.  
  6. import net.minecraft.util.math.Vec3d;
  7. import net.minecraft.entity.projectile.EntityLargeFireball;
  8. import net.minecraft.entity.ai.EntityAIFindEntityNearestPlayer;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.network.datasync.DataParameter;
  11. import net.minecraft.network.datasync.DataSerializers;
  12. import net.minecraft.network.datasync.EntityDataManager;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.entity.ai.EntityAIWatchClosest;
  15.  
  16.  
  17.  
  18.  
  19.  
  20.             /**
  21.              * AI tasks
  22.              */
  23.  
  24. this.tasks.addTask(4, new EntityCustom.AIFireballAttack(this));
  25. this.targetTasks.addTask(1, new EntityAIFindEntityNearestPlayer(this));
  26.  
  27.  
  28.             /**
  29.              * IMPORTANT: For some odd reason it uses same sound effect as Ghast when it's shotting  but I don't see any relation to it here
  30.              * unless the projectile will have it coded than, also Paste code below in space Between AILookAround code and AI Tasks
  31.              */
  32.  
  33.  
  34.  
  35.  
  36.             /**
  37.              * This is here to determine if mob is in Idle or Attacking State
  38.              */
  39.  
  40. private static final DataParameter<Boolean> ATTACKING = EntityDataManager.<Boolean>createKey(EntityCustom.class, DataSerializers.BOOLEAN);
  41.  
  42.             /**
  43.              * Strenght of projectile that is shooted
  44.              */
  45.     private int explosionStrength = 3;
  46.  
  47.  
  48.             /**
  49.              * Stuff to determine mob state
  50.              */
  51.      @SideOnly(Side.CLIENT)
  52.     public boolean isAttacking()
  53.     {
  54.         return ((Boolean)this.dataManager.get(ATTACKING)).booleanValue();
  55.     }
  56.  
  57.     public void setAttacking(boolean attacking)
  58.     {
  59.         this.dataManager.set(ATTACKING, Boolean.valueOf(attacking));
  60.     }
  61.  
  62.             /**
  63.              * Defines the variable that will contain strength of our projectile
  64.              */
  65.  
  66.     public int getFireballStrength()
  67.     {
  68.         return this.explosionStrength;
  69.     }
  70.    
  71.  
  72.             /**
  73.              * Sets Mob state from Attack to Idle when mob is Initialized
  74.              */
  75.      protected void entityInit()
  76.     {
  77.         super.entityInit();
  78.         this.dataManager.register(ATTACKING, Boolean.valueOf(false));
  79.     }
  80.  
  81.     /**
  82.      * (abstract) Protected helper method to write subclass entity data to NBT.
  83.      */
  84.     public void writeEntityToNBT(NBTTagCompound compound)
  85.     {
  86.         super.writeEntityToNBT(compound);
  87.         compound.setInteger("ExplosionPower", this.explosionStrength);
  88.     }
  89.  
  90.     /**
  91.      * (abstract) Protected helper method to read subclass entity data from NBT.
  92.      */
  93.     public void readEntityFromNBT(NBTTagCompound compound)
  94.     {
  95.         super.readEntityFromNBT(compound);
  96.  
  97.         if (compound.hasKey("ExplosionPower", 99))
  98.         {
  99.             this.explosionStrength = compound.getInteger("ExplosionPower");
  100.         }
  101.     }
  102.  
  103.  
  104.             /**
  105.              * The part that changes our mob into flying Airship with cannon
  106.              * As usual EntityCustom is our Entity class name, boo stands for bariable
  107.              */
  108.         static class AIFireballAttack extends EntityAIBase
  109.         {
  110.             private final EntityCustom parentEntity;
  111.             public int attackTimer;
  112.        
  113.             public AIFireballAttack(EntityCustom boo)
  114.             {
  115.                 this.parentEntity = boo;
  116.             }
  117.  
  118.             /**
  119.              * Returns whether the EntityAIBase should begin execution.
  120.              */
  121.             public boolean shouldExecute()
  122.             {
  123.                 return this.parentEntity.getAttackTarget() != null;
  124.             }
  125.  
  126.             /**
  127.              * Execute a one shot task or start executing a continuous task
  128.              */
  129.             public void startExecuting()
  130.             {
  131.                 this.attackTimer = 0;
  132.             }
  133.  
  134.             /**
  135.              * Reset the task's internal state. Called when this task is interrupted by another one
  136.              */
  137.             public void resetTask()
  138.             {
  139.                 this.parentEntity.setAttacking(false);
  140.             }
  141.  
  142.             /**
  143.              * Keep ticking a continuous task that has already been started
  144.              */
  145.             public void updateTask()
  146.             {
  147.                 EntityLivingBase entitylivingbase = this.parentEntity.getAttackTarget();
  148.                 double d0 = 64.0D;
  149.  
  150.                 if (entitylivingbase.getDistanceSq(this.parentEntity) < 4096.0D && this.parentEntity.canEntityBeSeen(entitylivingbase))
  151.                 {
  152.                     World world = this.parentEntity.world;
  153.                     ++this.attackTimer;
  154.  
  155.                     if (this.attackTimer == 10)
  156.                     {
  157.                         world.playEvent((EntityPlayer)null, 1015, new BlockPos(this.parentEntity), 0);
  158.                     }
  159.                    
  160.  
  161.  
  162.                     /**
  163.                      * This part defines what projectile is shooted and from what X/Y/Z location of our mob Bounding Box
  164.                      * based on Eye's height "You can change projectile from Large Fireball to something else"
  165.                      */
  166.                     if (this.attackTimer == 20)
  167.                     {
  168.                         double d1 = 4.0D;
  169.                         Vec3d vec3d = this.parentEntity.getLook(2.0F);
  170.                         double d2 = entitylivingbase.posX - (this.parentEntity.posX + vec3d.x * 4.0D);
  171.                         double d3 = entitylivingbase.getEntityBoundingBox().minY + (double)(entitylivingbase.height / 2.0F) - (0.5D + this.parentEntity.posY + (double)(this.parentEntity.height / 2.0F));
  172.                         double d4 = entitylivingbase.posZ - (this.parentEntity.posZ + vec3d.z * 4.0D);
  173.                         world.playEvent((EntityPlayer)null, 1016, new BlockPos(this.parentEntity), 0);
  174.                         EntityLargeFireball entitylargefireball = new EntityLargeFireball(world, this.parentEntity, d2, d3, d4);
  175.                         entitylargefireball.explosionPower = this.parentEntity.getFireballStrength();
  176.                         entitylargefireball.posX = this.parentEntity.posX + vec3d.x * 4.0D;
  177.                         entitylargefireball.posY = this.parentEntity.posY + (double)(this.parentEntity.height / 2.0F) + 0.5D;
  178.                         entitylargefireball.posZ = this.parentEntity.posZ + vec3d.z * 4.0D;
  179.                         world.spawnEntity(entitylargefireball);
  180.                         this.attackTimer = -40;
  181.                     }
  182.                 }
  183.                 else if (this.attackTimer > 0)
  184.                 {
  185.                     --this.attackTimer;
  186.                 }
  187.  
  188.                 this.parentEntity.setAttacking(this.attackTimer > 10);
  189.             }
  190.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement