Guest User

Untitled

a guest
May 20th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. package net.minecraft.entity.ai;
  2.  
  3. import java.util.EnumSet;
  4. import java.util.function.Predicate;
  5. import javax.annotation.Nullable;
  6.  
  7. import com.sonniccub.racismmod.events.PlayerAssignEvent;
  8.  
  9. import net.minecraft.entity.EntityPredicate;
  10. import net.minecraft.entity.LivingEntity;
  11. import net.minecraft.entity.MobEntity;
  12. import net.minecraft.entity.ai.goal.Goal;
  13. import net.minecraft.entity.ai.goal.TargetGoal;
  14. import net.minecraft.entity.player.PlayerEntity;
  15. import net.minecraft.entity.player.ServerPlayerEntity;
  16. import net.minecraft.util.math.AxisAlignedBB;
  17.  
  18. public class NearestAttackableTargetCopGoal<T extends LivingEntity> extends TargetGoal {
  19. protected final Class<T> targetClass;
  20. protected final int targetChance;
  21. protected LivingEntity nearestTarget;
  22. /** This filter is applied to the Entity search. Only matching entities will be targeted. */
  23. protected EntityPredicate targetEntitySelector;
  24.  
  25. public NearestAttackableTargetCopGoal(MobEntity goalOwnerIn, Class<T> targetClassIn, boolean checkSight) {
  26. this(goalOwnerIn, targetClassIn, checkSight, false);
  27. }
  28.  
  29. public NearestAttackableTargetCopGoal(MobEntity goalOwnerIn, Class<T> targetClassIn, boolean checkSight, boolean nearbyOnlyIn) {
  30. this(goalOwnerIn, targetClassIn, 10, checkSight, nearbyOnlyIn, (Predicate<LivingEntity>)null);
  31. }
  32.  
  33. public NearestAttackableTargetCopGoal(MobEntity goalOwnerIn, Class<T> targetClassIn, int targetChanceIn, boolean checkSight, boolean nearbyOnlyIn, @Nullable Predicate<LivingEntity> targetPredicate) {
  34. super(goalOwnerIn, checkSight, nearbyOnlyIn);
  35. this.targetClass = targetClassIn;
  36. this.targetChance = targetChanceIn;
  37. this.setMutexFlags(EnumSet.of(Goal.Flag.TARGET));
  38. this.targetEntitySelector = (new EntityPredicate()).setDistance(this.getTargetDistance()).setCustomPredicate(targetPredicate);
  39. }
  40.  
  41. /**
  42. * Returns whether execution should begin. You can also read and cache any state necessary for execution in this
  43. * method as well.
  44. */
  45. public boolean shouldExecute() {
  46. if (this.targetChance > 0 && this.goalOwner.getRNG().nextInt(this.targetChance) != 0) {
  47. return false;
  48. } else {
  49. this.findNearestTarget();
  50. return this.nearestTarget != null;
  51. }
  52. }
  53.  
  54. protected AxisAlignedBB getTargetableArea(double targetDistance) {
  55. return this.goalOwner.getBoundingBox().grow(targetDistance, 4.0D, targetDistance);
  56. }
  57.  
  58. protected void findNearestTarget() {
  59. if (this.targetClass != PlayerEntity.class && this.targetClass != ServerPlayerEntity.class) {
  60. this.nearestTarget = this.goalOwner.world.<T>func_225318_b(this.targetClass, this.targetEntitySelector, this.goalOwner, this.goalOwner.getPosX(), this.goalOwner.getPosYEye(), this.goalOwner.getPosZ(), this.getTargetableArea(this.getTargetDistance()));
  61. } else {
  62. this.nearestTarget = this.goalOwner.world.getClosestPlayer(this.targetEntitySelector, this.goalOwner, this.goalOwner.getPosX(), this.goalOwner.getPosYEye(), this.goalOwner.getPosZ());
  63. if (PlayerAssignEvent.PlayerAssignIds.get(PlayerEntity.getUUID(((PlayerEntity) nearestTarget).getGameProfile())) == false) {
  64. this.nearestTarget = null;
  65. }
  66. else {
  67. this.nearestTarget = this.goalOwner.world.getClosestPlayer(this.targetEntitySelector, this.goalOwner, this.goalOwner.getPosX(), this.goalOwner.getPosYEye(), this.goalOwner.getPosZ());
  68. }
  69. }
  70. }
  71.  
  72.  
  73.  
  74. /**
  75. * Execute a one shot task or start executing a continuous task
  76. */
  77. public void startExecuting() {
  78. this.goalOwner.setAttackTarget(this.nearestTarget);
  79. super.startExecuting();
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment