Advertisement
Guest User

EntityAIAttackRangedBow2

a guest
Jul 2nd, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. package com.ChaoticSoul.MoreArmor.entity;
  2.  
  3. import net.minecraft.entity.EntityLiving;
  4. import net.minecraft.entity.EntityLivingBase;
  5. import net.minecraft.entity.IRangedAttackMob;
  6. import net.minecraft.entity.ai.EntityAIBase;
  7. import net.minecraft.entity.monster.EntityMob;
  8. import net.minecraft.init.Items;
  9. import net.minecraft.item.ItemBow;
  10. import net.minecraft.util.EnumHand;
  11. import net.minecraft.world.World;
  12.  
  13. public class EntityAIAttackRangedBow2<T extends EntityLivingBase & IRangedAttackMob> extends EntityAIBase
  14. {
  15. private final T entity;
  16. private final double moveSpeedAmp;
  17. private int attackCooldown;
  18. private final float maxAttackDistance;
  19. private int attackTime = -1;
  20. private int seeTime;
  21. private boolean strafingClockwise;
  22. private boolean strafingBackwards;
  23. private int strafingTime = -1;
  24.  
  25. public EntityAIAttackRangedBow2(T p_i47515_1_, double p_i47515_2_, int p_i47515_4_, float p_i47515_5_)
  26. {
  27. this.entity = p_i47515_1_;
  28. this.moveSpeedAmp = p_i47515_2_;
  29. this.attackCooldown = p_i47515_4_;
  30. this.maxAttackDistance = p_i47515_5_ * p_i47515_5_;
  31. this.setMutexBits(3);
  32. }
  33.  
  34. public void setAttackCooldown(int p_189428_1_)
  35. {
  36. this.attackCooldown = p_189428_1_;
  37. }
  38.  
  39. /**
  40. * Returns whether the EntityAIBase should begin execution.
  41. */
  42. public boolean shouldExecute()
  43. {
  44. return ((EntityLiving) this.entity).getAttackTarget() == null ? false : this.isBowInMainhand();
  45. }
  46.  
  47. protected boolean isBowInMainhand()
  48. {
  49. return !this.entity.getHeldItemMainhand().isEmpty() && this.entity.getHeldItemMainhand().getItem() == Items.BOW;
  50. }
  51.  
  52. /**
  53. * Returns whether an in-progress EntityAIBase should continue executing
  54. */
  55. public boolean shouldContinueExecuting()
  56. {
  57. return (this.shouldExecute() || !((EntityLiving) this.entity).getNavigator().noPath()) && this.isBowInMainhand();
  58. }
  59.  
  60. /**
  61. * Execute a one shot task or start executing a continuous task
  62. */
  63. public void startExecuting()
  64. {
  65. super.startExecuting();
  66. ((IRangedAttackMob)this.entity).setSwingingArms(true);
  67. }
  68.  
  69. /**
  70. * Reset the task's internal state. Called when this task is interrupted by another one
  71. */
  72. public void resetTask()
  73. {
  74. super.resetTask();
  75. ((IRangedAttackMob)this.entity).setSwingingArms(false);
  76. this.seeTime = 0;
  77. this.attackTime = -1;
  78. this.entity.resetActiveHand();
  79. }
  80.  
  81. /**
  82. * Keep ticking a continuous task that has already been started
  83. */
  84. public void updateTask()
  85. {
  86. EntityLivingBase entitylivingbase = ((EntityLiving) this.entity).getAttackTarget();
  87.  
  88. if (entitylivingbase != null)
  89. {
  90. double d0 = this.entity.getDistanceSq(entitylivingbase.posX, entitylivingbase.getEntityBoundingBox().minY, entitylivingbase.posZ);
  91. boolean flag = ((EntityLiving) this.entity).getEntitySenses().canSee(entitylivingbase);
  92. boolean flag1 = this.seeTime > 0;
  93.  
  94. if (flag != flag1)
  95. {
  96. this.seeTime = 0;
  97. }
  98.  
  99. if (flag)
  100. {
  101. ++this.seeTime;
  102. }
  103. else
  104. {
  105. --this.seeTime;
  106. }
  107.  
  108. if (d0 <= (double)this.maxAttackDistance && this.seeTime >= 20)
  109. {
  110. ((EntityLiving) this.entity).getNavigator().clearPath();
  111. ++this.strafingTime;
  112. }
  113. else
  114. {
  115. ((EntityLiving) this.entity).getNavigator().tryMoveToEntityLiving(entitylivingbase, this.moveSpeedAmp);
  116. this.strafingTime = -1;
  117. }
  118.  
  119. if (this.strafingTime >= 20)
  120. {
  121. if ((double)this.entity.getRNG().nextFloat() < 0.3D)
  122. {
  123. this.strafingClockwise = !this.strafingClockwise;
  124. }
  125.  
  126. if ((double)this.entity.getRNG().nextFloat() < 0.3D)
  127. {
  128. this.strafingBackwards = !this.strafingBackwards;
  129. }
  130.  
  131. this.strafingTime = 0;
  132. }
  133.  
  134. if (this.strafingTime > -1)
  135. {
  136. if (d0 > (double)(this.maxAttackDistance * 0.75F))
  137. {
  138. this.strafingBackwards = false;
  139. }
  140. else if (d0 < (double)(this.maxAttackDistance * 0.25F))
  141. {
  142. this.strafingBackwards = true;
  143. }
  144.  
  145. ((EntityLiving) this.entity).getMoveHelper().strafe(this.strafingBackwards ? -0.5F : 0.5F, this.strafingClockwise ? 0.5F : -0.5F);
  146. ((EntityLiving) this.entity).faceEntity(entitylivingbase, 30.0F, 30.0F);
  147. }
  148. else
  149. {
  150. ((EntityLiving) this.entity).getLookHelper().setLookPositionWithEntity(entitylivingbase, 30.0F, 30.0F);
  151. }
  152.  
  153. if (this.entity.isHandActive())
  154. {
  155. if (!flag && this.seeTime < -60)
  156. {
  157. this.entity.resetActiveHand();
  158. }
  159. else if (flag)
  160. {
  161. int i = this.entity.getItemInUseMaxCount();
  162.  
  163. if (i >= 20)
  164. {
  165. this.entity.resetActiveHand();
  166. ((IRangedAttackMob)this.entity).attackEntityWithRangedAttack(entitylivingbase, ItemBow.getArrowVelocity(i));
  167. this.attackTime = this.attackCooldown;
  168. }
  169. }
  170. }
  171. else if (--this.attackTime <= 0 && this.seeTime >= -60)
  172. {
  173. this.entity.setActiveHand(EnumHand.MAIN_HAND);
  174. }
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement