| 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.LivingEntity;
|
| 6 | import net.minecraft.world.entity.Mob;
|
| 7 | import net.minecraft.world.entity.PathfinderMob;
|
| 8 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 9 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class TemptGoal extends Goal {
|
| 16 | private static final TargetingConditions TEMPT_TARGETING = TargetingConditions.forNonCombat().ignoreLineOfSight();
|
| 17 | private static final double DEFAULT_STOP_DISTANCE = 2.5;
|
| 18 | private final TargetingConditions targetingConditions;
|
| 19 | protected final Mob mob;
|
| 20 | protected final double speedModifier;
|
| 21 | private double px;
|
| 22 | private double py;
|
| 23 | private double pz;
|
| 24 | private double pRotX;
|
| 25 | private double pRotY;
|
| 26 | protected @Nullable Player player;
|
| 27 | private int calmDown;
|
| 28 | private boolean isRunning;
|
| 29 | private final Predicate<ItemStack> items;
|
| 30 | private final boolean canScare;
|
| 31 | private final double stopDistance;
|
| 32 |
|
| 33 | public TemptGoal(final PathfinderMob mob, final double speedModifier, final Predicate<ItemStack> items, final boolean canScare) {
|
| 34 | this((Mob)mob, speedModifier, items, canScare, 2.5);
|
| 35 | }
|
| 36 |
|
| 37 | public TemptGoal(final PathfinderMob mob, final double speedModifier, final Predicate<ItemStack> items, final boolean canScare, final double stopDistance) {
|
| 38 | this((Mob)mob, speedModifier, items, canScare, stopDistance);
|
| 39 | }
|
| 40 |
|
| 41 | private TemptGoal(final Mob mob, final double speedModifier, final Predicate<ItemStack> items, final boolean canScare, final double stopDistance) {
|
| 42 | this.mob = mob;
|
| 43 | this.speedModifier = speedModifier;
|
| 44 | this.items = items;
|
| 45 | this.canScare = canScare;
|
| 46 | this.stopDistance = stopDistance;
|
| 47 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 48 | this.targetingConditions = TEMPT_TARGETING.copy().selector((target, level) -> this.shouldFollow(target));
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public boolean canUse() {
|
| 53 | if (this.calmDown > 0) {
|
| 54 | this.calmDown--;
|
| 55 | return false;
|
| 56 | } else {
|
| 57 | this.player = getServerLevel(this.mob).getNearestPlayer(this.targetingConditions.range(this.mob.getAttributeValue(Attributes.TEMPT_RANGE)), this.mob);
|
| 58 | return this.player != null;
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private boolean shouldFollow(final LivingEntity player) {
|
| 63 | return this.items.test(player.getMainHandItem()) || this.items.test(player.getOffhandItem());
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public boolean canContinueToUse() {
|
| 68 | if (this.canScare()) {
|
| 69 | if (this.mob.distanceToSqr(this.player) < 36.0) {
|
| 70 | if (this.player.distanceToSqr(this.px, this.py, this.pz) > 0.010000000000000002) {
|
| 71 | return false;
|
| 72 | }
|
| 73 |
|
| 74 | if (Math.abs(this.player.getXRot() - this.pRotX) > 5.0 || Math.abs(this.player.getYRot() - this.pRotY) > 5.0) {
|
| 75 | return false;
|
| 76 | }
|
| 77 | } else {
|
| 78 | this.px = this.player.getX();
|
| 79 | this.py = this.player.getY();
|
| 80 | this.pz = this.player.getZ();
|
| 81 | }
|
| 82 |
|
| 83 | this.pRotX = this.player.getXRot();
|
| 84 | this.pRotY = this.player.getYRot();
|
| 85 | }
|
| 86 |
|
| 87 | return this.canUse();
|
| 88 | }
|
| 89 |
|
| 90 | protected boolean canScare() {
|
| 91 | return this.canScare;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public void start() {
|
| 96 | this.px = this.player.getX();
|
| 97 | this.py = this.player.getY();
|
| 98 | this.pz = this.player.getZ();
|
| 99 | this.isRunning = true;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public void stop() {
|
| 104 | this.player = null;
|
| 105 | this.stopNavigation();
|
| 106 | this.calmDown = reducedTickDelay(100);
|
| 107 | this.isRunning = false;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void tick() {
|
| 112 | this.mob.getLookControl().setLookAt(this.player, this.mob.getMaxHeadYRot() + 20, this.mob.getMaxHeadXRot());
|
| 113 | if (this.mob.distanceToSqr(this.player) < this.stopDistance * this.stopDistance) {
|
| 114 | this.stopNavigation();
|
| 115 | } else {
|
| 116 | this.navigateTowards(this.player);
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | protected void stopNavigation() {
|
| 121 | this.mob.getNavigation().stop();
|
| 122 | }
|
| 123 |
|
| 124 | protected void navigateTowards(final Player player) {
|
| 125 | this.mob.getNavigation().moveTo(player, this.speedModifier);
|
| 126 | }
|
| 127 |
|
| 128 | public boolean isRunning() {
|
| 129 | return this.isRunning;
|
| 130 | }
|
| 131 |
|
| 132 | public static class ForNonPathfinders extends TemptGoal {
|
| 133 | public ForNonPathfinders(final Mob mob, final double speedModifier, final Predicate<ItemStack> items, final boolean canScare, final double stopDistance) {
|
| 134 | super(mob, speedModifier, items, canScare, stopDistance);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | protected void stopNavigation() {
|
| 139 | this.mob.getMoveControl().setWait();
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | protected void navigateTowards(final Player player) {
|
| 144 | Vec3 target = player.getEyePosition().subtract(this.mob.position()).scale(this.mob.getRandom().nextDouble()).add(this.mob.position());
|
| 145 | this.mob.getMoveControl().setWantedPosition(target.x, target.y, target.z, this.speedModifier);
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|