|
| 1 | +package org.vivecraft.entities; |
| 2 | + |
| 3 | +import java.util.function.Predicate; |
| 4 | + |
| 5 | +import net.minecraft.world.entity.Entity; |
| 6 | +import net.minecraft.world.entity.LivingEntity; |
| 7 | +import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal; |
| 8 | +import net.minecraft.world.entity.ai.targeting.TargetingConditions; |
| 9 | +import net.minecraft.world.entity.monster.EnderMan; |
| 10 | +import net.minecraft.world.entity.player.Player; |
| 11 | + |
| 12 | +public class CustomEndermanLookForPlayerGoal extends NearestAttackableTargetGoal<Player> { |
| 13 | + private final EnderMan enderman; |
| 14 | + private Player pendingTarget; |
| 15 | + private int aggroTime; |
| 16 | + private int teleportTime; |
| 17 | + private final TargetingConditions startAggroTargetConditions; |
| 18 | + private final TargetingConditions continueAggroTargetConditions = TargetingConditions.forCombat().ignoreLineOfSight(); |
| 19 | + private final Predicate<LivingEntity> isAngerInducing; |
| 20 | + |
| 21 | + public CustomEndermanLookForPlayerGoal(EnderMan entityenderman, Predicate<LivingEntity> p) { |
| 22 | + super(entityenderman, Player.class, 10, false, false, p); |
| 23 | + this.enderman = entityenderman; |
| 24 | + this.isAngerInducing = (entityliving) -> { |
| 25 | + return (EndermanUtils.isLookingAtMe((Player)entityliving, this.enderman) || entityenderman.isAngryAt((LivingEntity) entityliving)) && !entityenderman.hasIndirectPassenger((Entity) entityliving); |
| 26 | + }; |
| 27 | + this.startAggroTargetConditions = TargetingConditions.forCombat().range(this.getFollowDistance()).selector(this.isAngerInducing); |
| 28 | + } |
| 29 | + |
| 30 | + public boolean canUse() { |
| 31 | + this.pendingTarget = this.enderman.level().getNearestPlayer(this.startAggroTargetConditions, this.enderman); |
| 32 | + return this.pendingTarget != null; |
| 33 | + } |
| 34 | + |
| 35 | + public void start() { |
| 36 | + this.aggroTime = this.adjustedTickDelay(5); |
| 37 | + this.teleportTime = 0; |
| 38 | + this.enderman.setBeingStaredAt(); |
| 39 | + } |
| 40 | + |
| 41 | + public void stop() { |
| 42 | + this.pendingTarget = null; |
| 43 | + super.stop(); |
| 44 | + } |
| 45 | + |
| 46 | + public boolean canContinueToUse() { |
| 47 | + if (this.pendingTarget != null) { |
| 48 | + if (!this.isAngerInducing.test(this.pendingTarget)) { |
| 49 | + return false; |
| 50 | + } else { |
| 51 | + this.enderman.lookAt(this.pendingTarget, 10.0F, 10.0F); |
| 52 | + return true; |
| 53 | + } |
| 54 | + } else { |
| 55 | + if (this.target != null) { |
| 56 | + if (this.enderman.hasIndirectPassenger(this.target)) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + if (this.continueAggroTargetConditions.test(this.enderman, this.target)) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return super.canContinueToUse(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void tick() { |
| 70 | + if (this.enderman.getTarget() == null) { |
| 71 | + super.setTarget((LivingEntity)null); |
| 72 | + } |
| 73 | + |
| 74 | + if (this.pendingTarget != null) { |
| 75 | + if (--this.aggroTime <= 0) { |
| 76 | + this.target = this.pendingTarget; |
| 77 | + this.pendingTarget = null; |
| 78 | + super.start(); |
| 79 | + } |
| 80 | + } else { |
| 81 | + if (this.target != null && !this.enderman.isPassenger()) { |
| 82 | + if (EndermanUtils.isLookingAtMe((Player)this.target, this.enderman)) { |
| 83 | + if (this.target.distanceToSqr(this.enderman) < 16.0) { |
| 84 | + this.enderman.teleport(); |
| 85 | + } |
| 86 | + |
| 87 | + this.teleportTime = 0; |
| 88 | + } else if (this.target.distanceToSqr(this.enderman) > 256.0 && this.teleportTime++ >= this.adjustedTickDelay(30) && this.enderman.teleportTowards(this.target)) { |
| 89 | + this.teleportTime = 0; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + super.tick(); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | + |
0 commit comments