| 1 | package net.minecraft.world.entity.ai.behavior;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.entity.Entity;
|
| 7 | import net.minecraft.world.entity.LivingEntity;
|
| 8 | import net.minecraft.world.entity.Mob;
|
| 9 | import net.minecraft.world.entity.PathfinderMob;
|
| 10 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 11 | import net.minecraft.world.entity.ai.memory.MemoryStatus;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class SpearApproach extends Behavior<PathfinderMob> {
|
| 15 | private final double speedModifierWhenRepositioning;
|
| 16 | private final float approachDistanceSq;
|
| 17 |
|
| 18 | public SpearApproach(final double speedModifierWhenRepositioning, final float approachDistance) {
|
| 19 | super(Map.of(MemoryModuleType.SPEAR_STATUS, MemoryStatus.VALUE_ABSENT));
|
| 20 | this.speedModifierWhenRepositioning = speedModifierWhenRepositioning;
|
| 21 | this.approachDistanceSq = approachDistance * approachDistance;
|
| 22 | }
|
| 23 |
|
| 24 | private boolean ableToAttack(final PathfinderMob mob) {
|
| 25 | return this.getTarget(mob) != null && mob.getMainHandItem().has(DataComponents.KINETIC_WEAPON);
|
| 26 | }
|
| 27 |
|
| 28 | protected boolean checkExtraStartConditions(final ServerLevel level, final PathfinderMob body) {
|
| 29 | return this.ableToAttack(body) && !body.isUsingItem();
|
| 30 | }
|
| 31 |
|
| 32 | protected void start(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 33 | body.setAggressive(true);
|
| 34 | body.getBrain().setMemory(MemoryModuleType.SPEAR_STATUS, SpearAttack.SpearStatus.APPROACH);
|
| 35 | super.start(level, body, timestamp);
|
| 36 | }
|
| 37 |
|
| 38 | private @Nullable LivingEntity getTarget(final PathfinderMob mob) {
|
| 39 | return mob.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
|
| 40 | }
|
| 41 |
|
| 42 | protected boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 43 | return this.ableToAttack(body) && this.farEnough(body);
|
| 44 | }
|
| 45 |
|
| 46 | private boolean farEnough(final PathfinderMob mob) {
|
| 47 | LivingEntity target = this.getTarget(mob);
|
| 48 | double targetDistSqr = mob.distanceToSqr(target.getX(), target.getY(), target.getZ());
|
| 49 | return targetDistSqr > this.approachDistanceSq;
|
| 50 | }
|
| 51 |
|
| 52 | protected void tick(final ServerLevel level, final PathfinderMob mob, final long timestamp) {
|
| 53 | LivingEntity target = this.getTarget(mob);
|
| 54 | Entity mount = mob.getRootVehicle();
|
| 55 | float speedModifier = 1.0F;
|
| 56 | if (mount instanceof Mob vehicleMob) {
|
| 57 | speedModifier = vehicleMob.chargeSpeedModifier();
|
| 58 | }
|
| 59 |
|
| 60 | mob.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(target, true));
|
| 61 | mob.getNavigation().moveTo(target, speedModifier * this.speedModifierWhenRepositioning);
|
| 62 | }
|
| 63 |
|
| 64 | protected void stop(final ServerLevel level, final PathfinderMob body, final long timestamp) {
|
| 65 | body.getNavigation().stop();
|
| 66 | body.getBrain().setMemory(MemoryModuleType.SPEAR_STATUS, SpearAttack.SpearStatus.CHARGING);
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | protected boolean timedOut(final long timestamp) {
|
| 71 | return false;
|
| 72 | }
|
| 73 | }
|
| 74 |
|