| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.world.InteractionHand;
|
| 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.player.Player;
|
| 9 | import net.minecraft.world.level.pathfinder.Path;
|
| 10 |
|
| 11 | public class MeleeAttackGoal extends Goal {
|
| 12 | protected final PathfinderMob mob;
|
| 13 | private final double speedModifier;
|
| 14 | private final boolean followingTargetEvenIfNotSeen;
|
| 15 | private Path path;
|
| 16 | private double pathedTargetX;
|
| 17 | private double pathedTargetY;
|
| 18 | private double pathedTargetZ;
|
| 19 | private int ticksUntilNextPathRecalculation;
|
| 20 | private int ticksUntilNextAttack;
|
| 21 | private final int attackInterval = 20;
|
| 22 | private long lastCanUseCheck;
|
| 23 | private static final long COOLDOWN_BETWEEN_CAN_USE_CHECKS = 20L;
|
| 24 |
|
| 25 | public MeleeAttackGoal(final PathfinderMob mob, final double speedModifier, final boolean followingTargetEvenIfNotSeen) {
|
| 26 | this.mob = mob;
|
| 27 | this.speedModifier = speedModifier;
|
| 28 | this.followingTargetEvenIfNotSeen = followingTargetEvenIfNotSeen;
|
| 29 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public boolean canUse() {
|
| 34 | long time = this.mob.level().getGameTime();
|
| 35 | if (time - this.lastCanUseCheck < 20L) {
|
| 36 | return false;
|
| 37 | }
|
| 38 |
|
| 39 | this.lastCanUseCheck = time;
|
| 40 | LivingEntity target = this.mob.getTarget();
|
| 41 | if (target == null) {
|
| 42 | return false;
|
| 43 | }
|
| 44 |
|
| 45 | if (!target.isAlive()) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | this.path = this.mob.getNavigation().createPath(target, 0);
|
| 50 | return this.path != null ? true : this.mob.isWithinMeleeAttackRange(target);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public boolean canContinueToUse() {
|
| 55 | LivingEntity target = this.mob.getTarget();
|
| 56 | if (target == null) {
|
| 57 | return false;
|
| 58 | } else if (!target.isAlive()) {
|
| 59 | return false;
|
| 60 | } else if (!this.followingTargetEvenIfNotSeen) {
|
| 61 | return !this.mob.getNavigation().isDone();
|
| 62 | } else {
|
| 63 | return !this.mob.isWithinHome(target.blockPosition()) ? false : !(target instanceof Player player && (player.isSpectator() || player.isCreative()));
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void start() {
|
| 69 | this.mob.getNavigation().moveTo(this.path, this.speedModifier);
|
| 70 | this.mob.setAggressive(true);
|
| 71 | this.ticksUntilNextPathRecalculation = 0;
|
| 72 | this.ticksUntilNextAttack = 0;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public void stop() {
|
| 77 | LivingEntity target = this.mob.getTarget();
|
| 78 | if (!EntitySelector.NO_CREATIVE_OR_SPECTATOR.test(target)) {
|
| 79 | this.mob.setTarget(null);
|
| 80 | }
|
| 81 |
|
| 82 | this.mob.setAggressive(false);
|
| 83 | this.mob.getNavigation().stop();
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public boolean requiresUpdateEveryTick() {
|
| 88 | return true;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public void tick() {
|
| 93 | LivingEntity target = this.mob.getTarget();
|
| 94 | if (target != null) {
|
| 95 | this.mob.getLookControl().setLookAt(target, 30.0F, 30.0F);
|
| 96 | this.ticksUntilNextPathRecalculation = Math.max(this.ticksUntilNextPathRecalculation - 1, 0);
|
| 97 | if ((this.followingTargetEvenIfNotSeen || this.mob.getSensing().hasLineOfSight(target))
|
| 98 | && this.ticksUntilNextPathRecalculation <= 0
|
| 99 | && (
|
| 100 | this.pathedTargetX == 0.0 && this.pathedTargetY == 0.0 && this.pathedTargetZ == 0.0
|
| 101 | || target.distanceToSqr(this.pathedTargetX, this.pathedTargetY, this.pathedTargetZ) >= 1.0
|
| 102 | || this.mob.getRandom().nextFloat() < 0.05F
|
| 103 | )) {
|
| 104 | this.pathedTargetX = target.getX();
|
| 105 | this.pathedTargetY = target.getY();
|
| 106 | this.pathedTargetZ = target.getZ();
|
| 107 | this.ticksUntilNextPathRecalculation = 4 + this.mob.getRandom().nextInt(7);
|
| 108 | double targetDistanceSqr = this.mob.distanceToSqr(target);
|
| 109 | if (targetDistanceSqr > 1024.0) {
|
| 110 | this.ticksUntilNextPathRecalculation += 10;
|
| 111 | } else if (targetDistanceSqr > 256.0) {
|
| 112 | this.ticksUntilNextPathRecalculation += 5;
|
| 113 | }
|
| 114 |
|
| 115 | if (!this.mob.getNavigation().moveTo(target, 0, this.speedModifier)) {
|
| 116 | this.ticksUntilNextPathRecalculation += 15;
|
| 117 | }
|
| 118 |
|
| 119 | this.ticksUntilNextPathRecalculation = this.adjustedTickDelay(this.ticksUntilNextPathRecalculation);
|
| 120 | }
|
| 121 |
|
| 122 | this.ticksUntilNextAttack = Math.max(this.ticksUntilNextAttack - 1, 0);
|
| 123 | this.checkAndPerformAttack(target);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | protected void checkAndPerformAttack(final LivingEntity target) {
|
| 128 | if (this.canPerformAttack(target)) {
|
| 129 | this.resetAttackCooldown();
|
| 130 | this.mob.swingForAttack(InteractionHand.MAIN_HAND);
|
| 131 | this.mob.doHurtTarget(getServerLevel(this.mob), target);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | protected void resetAttackCooldown() {
|
| 136 | this.ticksUntilNextAttack = this.adjustedTickDelay(20);
|
| 137 | }
|
| 138 |
|
| 139 | protected boolean isTimeToAttack() {
|
| 140 | return this.ticksUntilNextAttack <= 0;
|
| 141 | }
|
| 142 |
|
| 143 | protected boolean canPerformAttack(final LivingEntity target) {
|
| 144 | return this.isTimeToAttack() && this.mob.isWithinMeleeAttackRange(target) && this.mob.getSensing().hasLineOfSight(target);
|
| 145 | }
|
| 146 |
|
| 147 | protected int getTicksUntilNextAttack() {
|
| 148 | return this.ticksUntilNextAttack;
|
| 149 | }
|
| 150 |
|
| 151 | protected int getAttackInterval() {
|
| 152 | return this.adjustedTickDelay(20);
|
| 153 | }
|
| 154 | }
|
| 155 |
|