SpearUseGoal .java

net.minecraft.world.entity.ai.goal.SpearUseGoal (class): extends Goal. members: approachDistance, SpearUseState, speedModifierWhenCharging, mob, canUse.

net.minecraft.world.entity.ai.goalclass172 linescommonextends Monster>5.3 KB

API Surface (9)

boolean canUse()
boolean canContinueToUse()
void start()
void stop()
void tick()
boolean notEngagedYet()
void startEngagement(final int spearDownTime)
boolean tickAndCheckEngagement()
boolean tickAndCheckFleeing()

Fields & Constants (12)

MIN_REPOSITION_DISTANCE
MAX_REPOSITION_DISTANCE
MIN_COOLDOWN_DISTANCE
MAX_COOLDOWN_DISTANCE
MAX_FLEEING_TIME
mob
state
speedModifierWhenCharging
speedModifierWhenRepositioning
approachDistanceSq
targetInRangeRadiusSq
approachDistance

Code Graph: Dependencies (10)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

SpearUseGoal.java172 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal;
2
3
import java.util.EnumSet;
4
import java.util.Optional;
5
import net.minecraft.core.component.DataComponents;
6
import net.minecraft.world.InteractionHand;
7
import net.minecraft.world.entity.Entity;
8
import net.minecraft.world.entity.LivingEntity;
9
import net.minecraft.world.entity.Mob;
10
import net.minecraft.world.entity.ai.util.LandRandomPos;
11
import net.minecraft.world.entity.monster.Monster;
12
import net.minecraft.world.item.component.KineticWeapon;
13
import net.minecraft.world.phys.Vec3;
14
import org.jspecify.annotations.Nullable;
15
16
public class SpearUseGoal<T extends Monster> extends Goal {
17
	private static final int MIN_REPOSITION_DISTANCE = 6;
18
	private static final int MAX_REPOSITION_DISTANCE = 7;
19
	private static final int MIN_COOLDOWN_DISTANCE = 9;
20
	private static final int MAX_COOLDOWN_DISTANCE = 11;
21
	private static final double MAX_FLEEING_TIME = reducedTickDelay(100);
22
	private final T mob;
23
	private SpearUseGoal.@Nullable SpearUseState state;
24
	private final double speedModifierWhenCharging;
25
	private final double speedModifierWhenRepositioning;
26
	private final float approachDistanceSq;
27
	private final float targetInRangeRadiusSq;
28
29
	public SpearUseGoal(
30
		final T mob,
31
		final double speedModifierWhenCharging,
32
		final double speedModifierWhenRepositioning,
33
		final float approachDistance,
34
		final float targetInRangeRadius
35
	) {
36
		this.mob = mob;
37
		this.speedModifierWhenCharging = speedModifierWhenCharging;
38
		this.speedModifierWhenRepositioning = speedModifierWhenRepositioning;
39
		this.approachDistanceSq = approachDistance * approachDistance;
40
		this.targetInRangeRadiusSq = targetInRangeRadius * targetInRangeRadius;
41
		this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
42
	}
43
44
	@Override
45
	public boolean canUse() {
46
		return this.ableToAttack() && !this.mob.isUsingItem();
47
	}
48
49
	private boolean ableToAttack() {
50
		return this.mob.getTarget() != null && this.mob.getMainHandItem().has(DataComponents.KINETIC_WEAPON);
51
	}
52
53
	private int getKineticWeaponUseDuration() {
54
		int durationTicks = Optional.ofNullable(this.mob.getMainHandItem().get(DataComponents.KINETIC_WEAPON)).map(KineticWeapon::computeDamageUseDuration).orElse(0);
55
		return reducedTickDelay(durationTicks);
56
	}
57
58
	@Override
59
	public boolean canContinueToUse() {
60
		return this.state != null && !this.state.done && this.ableToAttack();
61
	}
62
63
	@Override
64
	public void start() {
65
		super.start();
66
		this.mob.setAggressive(true);
67
		this.state = new SpearUseGoal.SpearUseState();
68
	}
69
70
	@Override
71
	public void stop() {
72
		super.stop();
73
		this.mob.getNavigation().stop();
74
		this.mob.setAggressive(false);
75
		this.state = null;
76
		this.mob.stopUsingItem();
77
	}
78
79
	@Override
80
	public void tick() {
81
		if (this.state != null) {
82
			LivingEntity target = this.mob.getTarget();
83
			double targetDistSqr = this.mob.distanceToSqr(target.getX(), target.getY(), target.getZ());
84
			Entity mount = this.mob.getRootVehicle();
85
			float speedModifier = 1.0F;
86
			if (mount instanceof Mob vehicleMob) {
87
				speedModifier = vehicleMob.chargeSpeedModifier();
88
			}
89
90
			int mountDistance = this.mob.isPassenger() ? 2 : 0;
91
			this.mob.lookAt(target, 30.0F, 30.0F);
92
			this.mob.getLookControl().setLookAt(target, 30.0F, 30.0F);
93
			if (this.state.notEngagedYet()) {
94
				if (targetDistSqr > this.approachDistanceSq) {
95
					this.mob.getNavigation().moveTo(target, speedModifier * this.speedModifierWhenRepositioning);
96
					return;
97
				}
98
99
				this.state.startEngagement(this.getKineticWeaponUseDuration());
100
				this.mob.startUsingItem(InteractionHand.MAIN_HAND);
101
			}
102
103
			if (this.state.tickAndCheckEngagement()) {
104
				this.mob.stopUsingItem();
105
				double distance = Math.sqrt(targetDistSqr);
106
				this.state.awayPos = LandRandomPos.getPosAway(
107
					this.mob, Math.max(0.0, 9 + mountDistance - distance), Math.max(1.0, 11 + mountDistance - distance), 7, target.position()
108
				);
109
				this.state.fleeingTime = 1;
110
			}
111
112
			if (!this.state.tickAndCheckFleeing()) {
113
				if (this.state.awayPos != null) {
114
					this.mob.getNavigation().moveTo(this.state.awayPos.x, this.state.awayPos.y, this.state.awayPos.z, speedModifier * this.speedModifierWhenRepositioning);
115
					if (this.mob.getNavigation().isDone()) {
116
						if (this.state.fleeingTime > 0) {
117
							this.state.done = true;
118
							return;
119
						}
120
121
						this.state.awayPos = null;
122
					}
123
				} else {
124
					this.mob.getNavigation().moveTo(target, speedModifier * this.speedModifierWhenCharging);
125
					if (targetDistSqr < this.targetInRangeRadiusSq || this.mob.getNavigation().isDone()) {
126
						double distance = Math.sqrt(targetDistSqr);
127
						this.state.awayPos = LandRandomPos.getPosAway(this.mob, 6 + mountDistance - distance, 7 + mountDistance - distance, 7, target.position());
128
					}
129
				}
130
			}
131
		}
132
	}
133
134
	public static class SpearUseState {
135
		private int engageTime = -1;
136
		private int fleeingTime = -1;
137
		private @Nullable Vec3 awayPos;
138
		private boolean done = false;
139
140
		public boolean notEngagedYet() {
141
			return this.engageTime < 0;
142
		}
143
144
		public void startEngagement(final int spearDownTime) {
145
			this.engageTime = spearDownTime;
146
		}
147
148
		public boolean tickAndCheckEngagement() {
149
			if (this.engageTime > 0) {
150
				this.engageTime--;
151
				if (this.engageTime == 0) {
152
					return true;
153
				}
154
			}
155
156
			return false;
157
		}
158
159
		public boolean tickAndCheckFleeing() {
160
			if (this.fleeingTime > 0) {
161
				this.fleeingTime++;
162
				if (this.fleeingTime > SpearUseGoal.MAX_FLEEING_TIME) {
163
					this.done = true;
164
					return true;
165
				}
166
			}
167
168
			return false;
169
		}
170
	}
171
}
172

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