| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.entity.LivingEntity;
|
| 7 | import net.minecraft.world.entity.Mob;
|
| 8 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 9 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 10 | import net.minecraft.world.entity.monster.CrossbowAttackMob;
|
| 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 CrossbowAttack<E extends Mob & CrossbowAttackMob, T extends LivingEntity> extends Behavior<E> {
|
| 18 | private static final int TIMEOUT = 1200;
|
| 19 | private int attackDelay;
|
| 20 | private CrossbowAttack.CrossbowState crossbowState = CrossbowAttack.CrossbowState.UNCHARGED;
|
| 21 |
|
| 22 | public CrossbowAttack() {
|
| 23 | super(ImmutableMap.of(MemoryModuleType.LOOK_TARGET, MemoryStatus.REGISTERED, MemoryModuleType.ATTACK_TARGET, MemoryStatus.VALUE_PRESENT), 1200);
|
| 24 | }
|
| 25 |
|
| 26 | protected boolean checkExtraStartConditions(final ServerLevel level, final E body) {
|
| 27 | LivingEntity attackTarget = getAttackTarget(body);
|
| 28 | return body.isHolding(Items.CROSSBOW) && BehaviorUtils.canSee(body, attackTarget) && BehaviorUtils.isWithinAttackRange(body, attackTarget, 0);
|
| 29 | }
|
| 30 |
|
| 31 | protected boolean canStillUse(final ServerLevel level, final E body, final long timestamp) {
|
| 32 | return body.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET) && this.checkExtraStartConditions(level, body);
|
| 33 | }
|
| 34 |
|
| 35 | protected void tick(final ServerLevel level, final E body, final long timestamp) {
|
| 36 | LivingEntity target = getAttackTarget(body);
|
| 37 | this.lookAtTarget(body, target);
|
| 38 | this.crossbowAttack(body, target);
|
| 39 | }
|
| 40 |
|
| 41 | protected void stop(final ServerLevel level, final E body, final long timestamp) {
|
| 42 | if (body.isUsingItem()) {
|
| 43 | body.stopUsingItem();
|
| 44 | }
|
| 45 |
|
| 46 | if (body.isHolding(Items.CROSSBOW)) {
|
| 47 | body.setChargingCrossbow(false);
|
| 48 | body.getUseItem().set(DataComponents.CHARGED_PROJECTILES, ChargedProjectiles.EMPTY);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | private void crossbowAttack(final E body, final LivingEntity target) {
|
| 53 | if (this.crossbowState == CrossbowAttack.CrossbowState.UNCHARGED) {
|
| 54 | body.startUsingItem(ProjectileUtil.getWeaponHoldingHand(body, Items.CROSSBOW));
|
| 55 | this.crossbowState = CrossbowAttack.CrossbowState.CHARGING;
|
| 56 | body.setChargingCrossbow(true);
|
| 57 | } else if (this.crossbowState == CrossbowAttack.CrossbowState.CHARGING) {
|
| 58 | if (!body.isUsingItem()) {
|
| 59 | this.crossbowState = CrossbowAttack.CrossbowState.UNCHARGED;
|
| 60 | }
|
| 61 |
|
| 62 | int pullTime = body.getTicksUsingItem();
|
| 63 | ItemStack useItem = body.getUseItem();
|
| 64 | if (pullTime >= CrossbowItem.getChargeDuration(useItem, body)) {
|
| 65 | body.releaseUsingItem();
|
| 66 | this.crossbowState = CrossbowAttack.CrossbowState.CHARGED;
|
| 67 | this.attackDelay = 20 + body.getRandom().nextInt(20);
|
| 68 | body.setChargingCrossbow(false);
|
| 69 | }
|
| 70 | } else if (this.crossbowState == CrossbowAttack.CrossbowState.CHARGED) {
|
| 71 | this.attackDelay--;
|
| 72 | if (this.attackDelay == 0) {
|
| 73 | this.crossbowState = CrossbowAttack.CrossbowState.READY_TO_ATTACK;
|
| 74 | }
|
| 75 | } else if (this.crossbowState == CrossbowAttack.CrossbowState.READY_TO_ATTACK) {
|
| 76 | body.performRangedAttack(target, 1.0F);
|
| 77 | this.crossbowState = CrossbowAttack.CrossbowState.UNCHARGED;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private void lookAtTarget(final Mob body, final LivingEntity target) {
|
| 82 | body.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(target, true));
|
| 83 | }
|
| 84 |
|
| 85 | private static LivingEntity getAttackTarget(final LivingEntity body) {
|
| 86 | return body.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).get();
|
| 87 | }
|
| 88 |
|
| 89 | private enum CrossbowState {
|
| 90 | UNCHARGED,
|
| 91 | CHARGING,
|
| 92 | CHARGED,
|
| 93 | READY_TO_ATTACK;
|
| 94 | }
|
| 95 | }
|
| 96 |
|