| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import net.minecraft.world.entity.EntitySelector;
|
| 6 | import net.minecraft.world.entity.LivingEntity;
|
| 7 | import net.minecraft.world.entity.PathfinderMob;
|
| 8 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 9 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 10 | import net.minecraft.world.entity.ai.util.DefaultRandomPos;
|
| 11 | import net.minecraft.world.level.pathfinder.Path;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class AvoidEntityGoal<T extends LivingEntity> extends Goal {
|
| 16 | protected final PathfinderMob mob;
|
| 17 | private final double walkSpeedModifier;
|
| 18 | private final double sprintSpeedModifier;
|
| 19 | protected @Nullable T toAvoid;
|
| 20 | protected final float maxDist;
|
| 21 | protected @Nullable Path path;
|
| 22 | protected final PathNavigation pathNav;
|
| 23 | protected final Class<T> avoidClass;
|
| 24 | protected final Predicate<? super LivingEntity> avoidPredicate;
|
| 25 | protected final Predicate<? super LivingEntity> predicateOnAvoidEntity;
|
| 26 | private final TargetingConditions avoidEntityTargeting;
|
| 27 |
|
| 28 | public AvoidEntityGoal(
|
| 29 | final PathfinderMob mob, final Class<T> avoidClass, final float maxDist, final double walkSpeedModifier, final double sprintSpeedModifier
|
| 30 | ) {
|
| 31 | this(mob, avoidClass, t -> true, maxDist, walkSpeedModifier, sprintSpeedModifier, EntitySelector.NO_CREATIVE_OR_SPECTATOR);
|
| 32 | }
|
| 33 |
|
| 34 | public AvoidEntityGoal(
|
| 35 | final PathfinderMob mob,
|
| 36 | final Class<T> avoidClass,
|
| 37 | final Predicate<LivingEntity> avoidPredicate,
|
| 38 | final float maxDist,
|
| 39 | final double walkSpeedModifier,
|
| 40 | final double sprintSpeedModifier,
|
| 41 | final Predicate<? super LivingEntity> predicateOnAvoidEntity
|
| 42 | ) {
|
| 43 | this.mob = mob;
|
| 44 | this.avoidClass = avoidClass;
|
| 45 | this.avoidPredicate = avoidPredicate;
|
| 46 | this.maxDist = maxDist;
|
| 47 | this.walkSpeedModifier = walkSpeedModifier;
|
| 48 | this.sprintSpeedModifier = sprintSpeedModifier;
|
| 49 | this.predicateOnAvoidEntity = predicateOnAvoidEntity;
|
| 50 | this.pathNav = mob.getNavigation();
|
| 51 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 52 | this.avoidEntityTargeting = TargetingConditions.forCombat()
|
| 53 | .range(maxDist)
|
| 54 | .selector((target, level) -> predicateOnAvoidEntity.test(target) && avoidPredicate.test(target));
|
| 55 | }
|
| 56 |
|
| 57 | public AvoidEntityGoal(
|
| 58 | final PathfinderMob mob,
|
| 59 | final Class<T> avoidClass,
|
| 60 | final float maxDist,
|
| 61 | final double walkSpeedModifier,
|
| 62 | final double sprintSpeedModifier,
|
| 63 | final Predicate<? super LivingEntity> predicateOnAvoidEntity
|
| 64 | ) {
|
| 65 | this(mob, avoidClass, t -> true, maxDist, walkSpeedModifier, sprintSpeedModifier, predicateOnAvoidEntity);
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public boolean canUse() {
|
| 70 | this.toAvoid = getServerLevel(this.mob)
|
| 71 | .getNearestEntity(
|
| 72 | this.mob.level().getEntitiesOfClass(this.avoidClass, this.mob.getBoundingBox().inflate(this.maxDist, 3.0, this.maxDist), entity -> true),
|
| 73 | this.avoidEntityTargeting,
|
| 74 | this.mob,
|
| 75 | this.mob.getX(),
|
| 76 | this.mob.getY(),
|
| 77 | this.mob.getZ()
|
| 78 | );
|
| 79 | if (this.toAvoid == null) {
|
| 80 | return false;
|
| 81 | }
|
| 82 |
|
| 83 | Vec3 pos = DefaultRandomPos.getPosAway(this.mob, 16, 7, this.toAvoid.position());
|
| 84 | if (pos == null) {
|
| 85 | return false;
|
| 86 | }
|
| 87 |
|
| 88 | if (this.toAvoid.distanceToSqr(pos.x, pos.y, pos.z) < this.toAvoid.distanceToSqr(this.mob)) {
|
| 89 | return false;
|
| 90 | }
|
| 91 |
|
| 92 | this.path = this.pathNav.createPath(pos.x, pos.y, pos.z, 0);
|
| 93 | return this.path != null;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean canContinueToUse() {
|
| 98 | return !this.pathNav.isDone();
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public void start() {
|
| 103 | this.pathNav.moveTo(this.path, this.walkSpeedModifier);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public void stop() {
|
| 108 | this.toAvoid = null;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void tick() {
|
| 113 | if (this.mob.distanceToSqr(this.toAvoid) < 49.0) {
|
| 114 | this.mob.getNavigation().setSpeedModifier(this.sprintSpeedModifier);
|
| 115 | } else {
|
| 116 | this.mob.getNavigation().setSpeedModifier(this.walkSpeedModifier);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|