| 1 | package net.minecraft.world.entity.ai.goal;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.util.TimeUtil;
|
| 6 | import net.minecraft.util.valueproviders.UniformInt;
|
| 7 | import net.minecraft.world.entity.LivingEntity;
|
| 8 | import net.minecraft.world.entity.monster.CrossbowAttackMob;
|
| 9 | import net.minecraft.world.entity.monster.Monster;
|
| 10 | import net.minecraft.world.entity.monster.RangedAttackMob;
|
| 11 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 12 | import net.minecraft.world.item.CrossbowItem;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.Items;
|
| 15 | import net.minecraft.world.item.component.ChargedProjectiles;
|
| 16 |
|
| 17 | public class RangedCrossbowAttackGoal<T extends Monster & RangedAttackMob & CrossbowAttackMob> extends Goal {
|
| 18 | public static final UniformInt PATHFINDING_DELAY_RANGE = TimeUtil.rangeOfSeconds(1, 2);
|
| 19 | private final T mob;
|
| 20 | private RangedCrossbowAttackGoal.CrossbowState crossbowState = RangedCrossbowAttackGoal.CrossbowState.UNCHARGED;
|
| 21 | private final double speedModifier;
|
| 22 | private final float attackRadiusSqr;
|
| 23 | private int seeTime;
|
| 24 | private int attackDelay;
|
| 25 | private int updatePathDelay;
|
| 26 |
|
| 27 | public RangedCrossbowAttackGoal(final T mob, final double speedModifier, final float attackRadius) {
|
| 28 | this.mob = mob;
|
| 29 | this.speedModifier = speedModifier;
|
| 30 | this.attackRadiusSqr = attackRadius * attackRadius;
|
| 31 | this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean canUse() {
|
| 36 | return this.isValidTarget() && this.isHoldingCrossbow();
|
| 37 | }
|
| 38 |
|
| 39 | private boolean isHoldingCrossbow() {
|
| 40 | return this.mob.isHolding(Items.CROSSBOW);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public boolean canContinueToUse() {
|
| 45 | return this.isValidTarget() && (this.canUse() || !this.mob.getNavigation().isDone()) && this.isHoldingCrossbow();
|
| 46 | }
|
| 47 |
|
| 48 | private boolean isValidTarget() {
|
| 49 | return this.mob.getTarget() != null && this.mob.getTarget().isAlive();
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void stop() {
|
| 54 | super.stop();
|
| 55 | this.mob.setAggressive(false);
|
| 56 | this.mob.setTarget(null);
|
| 57 | this.seeTime = 0;
|
| 58 | if (this.mob.isUsingItem()) {
|
| 59 | this.mob.stopUsingItem();
|
| 60 | this.mob.setChargingCrossbow(false);
|
| 61 | this.mob.getUseItem().set(DataComponents.CHARGED_PROJECTILES, ChargedProjectiles.EMPTY);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public boolean requiresUpdateEveryTick() {
|
| 67 | return true;
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void tick() {
|
| 72 | LivingEntity target = this.mob.getTarget();
|
| 73 | if (target != null) {
|
| 74 | boolean hasLineOfSight = this.mob.getSensing().hasLineOfSight(target);
|
| 75 | boolean hadLineOfSight = this.seeTime > 0;
|
| 76 | if (hasLineOfSight != hadLineOfSight) {
|
| 77 | this.seeTime = 0;
|
| 78 | }
|
| 79 |
|
| 80 | if (hasLineOfSight) {
|
| 81 | this.seeTime++;
|
| 82 | } else {
|
| 83 | this.seeTime--;
|
| 84 | }
|
| 85 |
|
| 86 | double distanceToSqr = this.mob.distanceToSqr(target);
|
| 87 | boolean needsToMove = (distanceToSqr > this.attackRadiusSqr || this.seeTime < 5) && this.attackDelay == 0;
|
| 88 | if (needsToMove) {
|
| 89 | this.updatePathDelay--;
|
| 90 | if (this.updatePathDelay <= 0) {
|
| 91 | this.mob.getNavigation().moveTo(target, this.canRun() ? this.speedModifier : this.speedModifier * 0.5);
|
| 92 | this.updatePathDelay = PATHFINDING_DELAY_RANGE.sample(this.mob.getRandom());
|
| 93 | }
|
| 94 | } else {
|
| 95 | this.updatePathDelay = 0;
|
| 96 | this.mob.getNavigation().stop();
|
| 97 | }
|
| 98 |
|
| 99 | this.mob.getLookControl().setLookAt(target, 30.0F, 30.0F);
|
| 100 | if (this.crossbowState == RangedCrossbowAttackGoal.CrossbowState.UNCHARGED) {
|
| 101 | if (!needsToMove) {
|
| 102 | this.mob.startUsingItem(ProjectileUtil.getWeaponHoldingHand(this.mob, Items.CROSSBOW));
|
| 103 | this.crossbowState = RangedCrossbowAttackGoal.CrossbowState.CHARGING;
|
| 104 | this.mob.setChargingCrossbow(true);
|
| 105 | }
|
| 106 | } else if (this.crossbowState == RangedCrossbowAttackGoal.CrossbowState.CHARGING) {
|
| 107 | if (!this.mob.isUsingItem()) {
|
| 108 | this.crossbowState = RangedCrossbowAttackGoal.CrossbowState.UNCHARGED;
|
| 109 | }
|
| 110 |
|
| 111 | int pullTime = this.mob.getTicksUsingItem();
|
| 112 | ItemStack useItem = this.mob.getUseItem();
|
| 113 | if (pullTime >= CrossbowItem.getChargeDuration(useItem, this.mob)) {
|
| 114 | this.mob.releaseUsingItem();
|
| 115 | this.crossbowState = RangedCrossbowAttackGoal.CrossbowState.CHARGED;
|
| 116 | this.attackDelay = 20 + this.mob.getRandom().nextInt(20);
|
| 117 | this.mob.setChargingCrossbow(false);
|
| 118 | }
|
| 119 | } else if (this.crossbowState == RangedCrossbowAttackGoal.CrossbowState.CHARGED) {
|
| 120 | this.attackDelay--;
|
| 121 | if (this.attackDelay == 0) {
|
| 122 | this.crossbowState = RangedCrossbowAttackGoal.CrossbowState.READY_TO_ATTACK;
|
| 123 | }
|
| 124 | } else if (this.crossbowState == RangedCrossbowAttackGoal.CrossbowState.READY_TO_ATTACK && hasLineOfSight) {
|
| 125 | this.mob.performRangedAttack(target, 1.0F);
|
| 126 | this.crossbowState = RangedCrossbowAttackGoal.CrossbowState.UNCHARGED;
|
| 127 | }
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | private boolean canRun() {
|
| 132 | return this.crossbowState == RangedCrossbowAttackGoal.CrossbowState.UNCHARGED;
|
| 133 | }
|
| 134 |
|
| 135 | private enum CrossbowState {
|
| 136 | UNCHARGED,
|
| 137 | CHARGING,
|
| 138 | CHARGED,
|
| 139 | READY_TO_ATTACK;
|
| 140 | }
|
| 141 | }
|
| 142 |
|