SpearAttack .java

net.minecraft.world.entity.ai.behavior.SpearAttack (class): members: SpearStatus, speedModifierWhenCharging, MAX_REPOSITION_DISTANCE, timedOut, stop, tick.

net.minecraft.world.entity.ai.behaviorclass110 linescommonextends Behavior<PathfinderMob>4.8 KB

API Surface (6)

boolean checkExtraStartConditions(final ServerLevel level, final PathfinderMob body)
void start(final ServerLevel level, final PathfinderMob body, final long timestamp)
boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp)
void tick(final ServerLevel level, final PathfinderMob mob, final long timestamp)
void stop(final ServerLevel level, final PathfinderMob body, final long timestamp)
boolean timedOut(final long timestamp)

Fields & Constants (5)

MIN_REPOSITION_DISTANCE
MAX_REPOSITION_DISTANCE
speedModifierWhenCharging
speedModifierWhenRepositioning
targetInRangeRadiusSq

Full Source

SpearAttack.java110 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import java.util.Map;
4
import java.util.Optional;
5
import net.minecraft.core.component.DataComponents;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.world.InteractionHand;
8
import net.minecraft.world.entity.Entity;
9
import net.minecraft.world.entity.LivingEntity;
10
import net.minecraft.world.entity.Mob;
11
import net.minecraft.world.entity.PathfinderMob;
12
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
13
import net.minecraft.world.entity.ai.memory.MemoryStatus;
14
import net.minecraft.world.entity.ai.util.LandRandomPos;
15
import net.minecraft.world.item.component.KineticWeapon;
16
import net.minecraft.world.phys.Vec3;
17
import org.jspecify.annotations.Nullable;
18
19
public class SpearAttack extends Behavior<PathfinderMob> {
20
	public static final int MIN_REPOSITION_DISTANCE = 6;
21
	public static final int MAX_REPOSITION_DISTANCE = 7;
22
	private final double speedModifierWhenCharging;
23
	private final double speedModifierWhenRepositioning;
24
	private final float targetInRangeRadiusSq;
25
26
	public SpearAttack(final double speedModifierWhenCharging, final double speedModifierWhenRepositioning, final float targetInRangeRadius) {
27
		super(Map.of(MemoryModuleType.SPEAR_STATUS, MemoryStatus.VALUE_PRESENT));
28
		this.speedModifierWhenCharging = speedModifierWhenCharging;
29
		this.speedModifierWhenRepositioning = speedModifierWhenRepositioning;
30
		this.targetInRangeRadiusSq = targetInRangeRadius * targetInRangeRadius;
31
	}
32
33
	private @Nullable LivingEntity getTarget(final PathfinderMob mob) {
34
		return mob.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
35
	}
36
37
	private boolean ableToAttack(final PathfinderMob mob) {
38
		return this.getTarget(mob) != null && mob.getMainHandItem().has(DataComponents.KINETIC_WEAPON);
39
	}
40
41
	private int getKineticWeaponUseDuration(final PathfinderMob mob) {
42
		return Optional.ofNullable(mob.getMainHandItem().get(DataComponents.KINETIC_WEAPON)).map(KineticWeapon::computeDamageUseDuration).orElse(0);
43
	}
44
45
	protected boolean checkExtraStartConditions(final ServerLevel level, final PathfinderMob body) {
46
		return body.getBrain().getMemory(MemoryModuleType.SPEAR_STATUS).orElse(SpearAttack.SpearStatus.APPROACH) == SpearAttack.SpearStatus.CHARGING
47
			&& this.ableToAttack(body)
48
			&& !body.isUsingItem();
49
	}
50
51
	protected void start(final ServerLevel level, final PathfinderMob body, final long timestamp) {
52
		body.setAggressive(true);
53
		body.getBrain().setMemory(MemoryModuleType.SPEAR_ENGAGE_TIME, this.getKineticWeaponUseDuration(body));
54
		body.getBrain().eraseMemory(MemoryModuleType.SPEAR_CHARGE_POSITION);
55
		body.startUsingItem(InteractionHand.MAIN_HAND);
56
		super.start(level, body, timestamp);
57
	}
58
59
	protected boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp) {
60
		return body.getBrain().getMemory(MemoryModuleType.SPEAR_ENGAGE_TIME).orElse(0) > 0 && this.ableToAttack(body);
61
	}
62
63
	protected void tick(final ServerLevel level, final PathfinderMob mob, final long timestamp) {
64
		LivingEntity target = this.getTarget(mob);
65
		double targetDistSqr = mob.distanceToSqr(target.getX(), target.getY(), target.getZ());
66
		Entity mount = mob.getRootVehicle();
67
		float speedModifier = 1.0F;
68
		if (mount instanceof Mob vehicleMob) {
69
			speedModifier = vehicleMob.chargeSpeedModifier();
70
		}
71
72
		int mountDistance = mob.isPassenger() ? 2 : 0;
73
		mob.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(target, true));
74
		mob.getBrain().setMemory(MemoryModuleType.SPEAR_ENGAGE_TIME, mob.getBrain().getMemory(MemoryModuleType.SPEAR_ENGAGE_TIME).orElse(0) - 1);
75
		Vec3 awayPos = mob.getBrain().getMemory(MemoryModuleType.SPEAR_CHARGE_POSITION).orElse(null);
76
		if (awayPos != null) {
77
			mob.getNavigation().moveTo(awayPos.x, awayPos.y, awayPos.z, speedModifier * this.speedModifierWhenRepositioning);
78
			if (mob.getNavigation().isDone()) {
79
				mob.getBrain().eraseMemory(MemoryModuleType.SPEAR_CHARGE_POSITION);
80
			}
81
		} else {
82
			mob.getNavigation().moveTo(target, speedModifier * this.speedModifierWhenCharging);
83
			if (targetDistSqr < this.targetInRangeRadiusSq || mob.getNavigation().isDone()) {
84
				double distance = Math.sqrt(targetDistSqr);
85
				Vec3 newAwayPos = LandRandomPos.getPosAway(mob, 6 + mountDistance - distance, 7 + mountDistance - distance, 7, target.position());
86
				mob.getBrain().setMemory(MemoryModuleType.SPEAR_CHARGE_POSITION, newAwayPos);
87
			}
88
		}
89
	}
90
91
	protected void stop(final ServerLevel level, final PathfinderMob body, final long timestamp) {
92
		body.getNavigation().stop();
93
		body.stopUsingItem();
94
		body.getBrain().eraseMemory(MemoryModuleType.SPEAR_CHARGE_POSITION);
95
		body.getBrain().eraseMemory(MemoryModuleType.SPEAR_ENGAGE_TIME);
96
		body.getBrain().setMemory(MemoryModuleType.SPEAR_STATUS, SpearAttack.SpearStatus.RETREAT);
97
	}
98
99
	@Override
100
	protected boolean timedOut(final long timestamp) {
101
		return false;
102
	}
103
104
	public enum SpearStatus {
105
		APPROACH,
106
		CHARGING,
107
		RETREAT;
108
	}
109
}
110

Decompiled Minecraft 26.3 source of SpearAttack (net/minecraft/world/entity/ai/behavior/SpearAttack.java) with Mojang official mappings, stored verbatim on the site.