SpearRetreat .java

net.minecraft.world.entity.ai.behavior.SpearRetreat (class, 90 lines): members: MAX_COOLDOWN_DISTANCE, MIN_COOLDOWN_DISTANCE, MAX_FLEEING_TIME, stop, tick.

net.minecraft.world.entity.ai.behaviorclass90 linescommonextends Behavior<PathfinderMob>3.9 KB

API Surface (5)

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)

Fields & Constants (4)

MIN_COOLDOWN_DISTANCE
MAX_COOLDOWN_DISTANCE
MAX_FLEEING_TIME
speedModifierWhenRepositioning

Full Source

SpearRetreat.java90 lines · Minecraft 26.3 · verbatim
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.LivingEntity;
7
import net.minecraft.world.entity.Mob;
8
import net.minecraft.world.entity.PathfinderMob;
9
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
10
import net.minecraft.world.entity.ai.memory.MemoryStatus;
11
import net.minecraft.world.entity.ai.util.LandRandomPos;
12
import net.minecraft.world.phys.Vec3;
13
import org.jspecify.annotations.Nullable;
14
15
public class SpearRetreat extends Behavior<PathfinderMob> {
16
	public static final int MIN_COOLDOWN_DISTANCE = 9;
17
	public static final int MAX_COOLDOWN_DISTANCE = 11;
18
	public static final int MAX_FLEEING_TIME = 100;
19
	private final double speedModifierWhenRepositioning;
20
21
	public SpearRetreat(final double speedModifierWhenRepositioning) {
22
		super(Map.of(MemoryModuleType.SPEAR_STATUS, MemoryStatus.VALUE_PRESENT), 100);
23
		this.speedModifierWhenRepositioning = speedModifierWhenRepositioning;
24
	}
25
26
	private @Nullable LivingEntity getTarget(final PathfinderMob mob) {
27
		return mob.getBrain().getMemory(MemoryModuleType.ATTACK_TARGET).orElse(null);
28
	}
29
30
	private boolean ableToAttack(final PathfinderMob mob) {
31
		return this.getTarget(mob) != null && mob.getMainHandItem().has(DataComponents.KINETIC_WEAPON);
32
	}
33
34
	protected boolean checkExtraStartConditions(final ServerLevel level, final PathfinderMob body) {
35
		if (this.ableToAttack(body) && !body.isUsingItem()) {
36
			if (body.getBrain().getMemory(MemoryModuleType.SPEAR_STATUS).orElse(SpearAttack.SpearStatus.APPROACH) != SpearAttack.SpearStatus.RETREAT) {
37
				return false;
38
			}
39
40
			LivingEntity target = this.getTarget(body);
41
			double targetDistSqr = body.distanceToSqr(target.getX(), target.getY(), target.getZ());
42
			int mountDistance = body.isPassenger() ? 2 : 0;
43
			double distance = Math.sqrt(targetDistSqr);
44
			Vec3 awayPos = LandRandomPos.getPosAway(
45
				body, Math.max(0.0, 9 + mountDistance - distance), Math.max(1.0, 11 + mountDistance - distance), 7, target.position()
46
			);
47
			if (awayPos == null) {
48
				return false;
49
			}
50
51
			body.getBrain().setMemory(MemoryModuleType.SPEAR_FLEEING_POSITION, awayPos);
52
			return true;
53
		} else {
54
			return false;
55
		}
56
	}
57
58
	protected void start(final ServerLevel level, final PathfinderMob body, final long timestamp) {
59
		body.setAggressive(true);
60
		body.getBrain().setMemory(MemoryModuleType.SPEAR_FLEEING_TIME, 0);
61
		super.start(level, body, timestamp);
62
	}
63
64
	protected boolean canStillUse(final ServerLevel level, final PathfinderMob body, final long timestamp) {
65
		return body.getBrain().getMemory(MemoryModuleType.SPEAR_FLEEING_TIME).orElse(100) < 100
66
			&& body.getBrain().getMemory(MemoryModuleType.SPEAR_FLEEING_POSITION).isPresent()
67
			&& !body.getNavigation().isDone()
68
			&& this.ableToAttack(body);
69
	}
70
71
	protected void tick(final ServerLevel level, final PathfinderMob mob, final long timestamp) {
72
		LivingEntity target = this.getTarget(mob);
73
		float speedModifier = mob.getRootVehicle() instanceof Mob vehicleMob ? vehicleMob.chargeSpeedModifier() : 1.0F;
74
		mob.getBrain().setMemory(MemoryModuleType.LOOK_TARGET, new EntityTracker(target, true));
75
		mob.getBrain().setMemory(MemoryModuleType.SPEAR_FLEEING_TIME, mob.getBrain().getMemory(MemoryModuleType.SPEAR_FLEEING_TIME).orElse(0) + 1);
76
		mob.getBrain()
77
			.getMemory(MemoryModuleType.SPEAR_FLEEING_POSITION)
78
			.ifPresent(fleePos -> mob.getNavigation().moveTo(fleePos.x, fleePos.y, fleePos.z, speedModifier * this.speedModifierWhenRepositioning));
79
	}
80
81
	protected void stop(final ServerLevel level, final PathfinderMob body, final long timestamp) {
82
		body.getNavigation().stop();
83
		body.setAggressive(false);
84
		body.stopUsingItem();
85
		body.getBrain().eraseMemory(MemoryModuleType.SPEAR_FLEEING_TIME);
86
		body.getBrain().eraseMemory(MemoryModuleType.SPEAR_FLEEING_POSITION);
87
		body.getBrain().eraseMemory(MemoryModuleType.SPEAR_STATUS);
88
	}
89
}
90

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