RangedAttackGoal .java

net.minecraft.world.entity.ai.goal.RangedAttackGoal (class, 102 lines): members: attackIntervalMax, rangedAttackMob, attackRadius, attackIntervalMin, mob.

net.minecraft.world.entity.ai.goalclass102 linescommonextends Goal3.1 KB

API Surface (5)

boolean canUse()
boolean canContinueToUse()
void stop()
boolean requiresUpdateEveryTick()
void tick()

Fields & Constants (10)

mob
rangedAttackMob
target
attackTime
speedModifier
seeTime
attackIntervalMin
attackIntervalMax
attackRadius
attackRadiusSqr

Code Graph: Dependencies (5)

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

Full Source

RangedAttackGoal.java102 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal;
2
3
import java.util.EnumSet;
4
import net.minecraft.util.Mth;
5
import net.minecraft.world.entity.LivingEntity;
6
import net.minecraft.world.entity.Mob;
7
import net.minecraft.world.entity.monster.RangedAttackMob;
8
import org.jspecify.annotations.Nullable;
9
10
public class RangedAttackGoal extends Goal {
11
	private final Mob mob;
12
	private final RangedAttackMob rangedAttackMob;
13
	private @Nullable LivingEntity target;
14
	private int attackTime = -1;
15
	private final double speedModifier;
16
	private int seeTime;
17
	private final int attackIntervalMin;
18
	private final int attackIntervalMax;
19
	private final float attackRadius;
20
	private final float attackRadiusSqr;
21
22
	public RangedAttackGoal(final RangedAttackMob mob, final double speedModifier, final int attackInterval, final float attackRadius) {
23
		this(mob, speedModifier, attackInterval, attackInterval, attackRadius);
24
	}
25
26
	public RangedAttackGoal(
27
		final RangedAttackMob mob, final double speedModifier, final int attackIntervalMin, final int attackIntervalMax, final float attackRadius
28
	) {
29
		if (!(mob instanceof LivingEntity)) {
30
			throw new IllegalArgumentException("ArrowAttackGoal requires Mob implements RangedAttackMob");
31
		}
32
33
		this.rangedAttackMob = mob;
34
		this.mob = (Mob)mob;
35
		this.speedModifier = speedModifier;
36
		this.attackIntervalMin = attackIntervalMin;
37
		this.attackIntervalMax = attackIntervalMax;
38
		this.attackRadius = attackRadius;
39
		this.attackRadiusSqr = attackRadius * attackRadius;
40
		this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
41
	}
42
43
	@Override
44
	public boolean canUse() {
45
		LivingEntity bestTarget = this.mob.getTarget();
46
		if (bestTarget != null && bestTarget.isAlive()) {
47
			this.target = bestTarget;
48
			return true;
49
		} else {
50
			return false;
51
		}
52
	}
53
54
	@Override
55
	public boolean canContinueToUse() {
56
		return this.canUse() || this.target.isAlive() && !this.mob.getNavigation().isDone();
57
	}
58
59
	@Override
60
	public void stop() {
61
		this.target = null;
62
		this.seeTime = 0;
63
		this.attackTime = -1;
64
	}
65
66
	@Override
67
	public boolean requiresUpdateEveryTick() {
68
		return true;
69
	}
70
71
	@Override
72
	public void tick() {
73
		double targetDistSqr = this.mob.distanceToSqr(this.target.getX(), this.target.getY(), this.target.getZ());
74
		boolean hasLineOfSight = this.mob.getSensing().hasLineOfSight(this.target);
75
		if (hasLineOfSight) {
76
			this.seeTime++;
77
		} else {
78
			this.seeTime = 0;
79
		}
80
81
		if (!(targetDistSqr > this.attackRadiusSqr) && this.seeTime >= 5) {
82
			this.mob.getNavigation().stop();
83
		} else {
84
			this.mob.getNavigation().moveTo(this.target, this.speedModifier);
85
		}
86
87
		this.mob.getLookControl().setLookAt(this.target, 30.0F, 30.0F);
88
		if (--this.attackTime == 0) {
89
			if (!hasLineOfSight) {
90
				return;
91
			}
92
93
			float dist = (float)Math.sqrt(targetDistSqr) / this.attackRadius;
94
			float power = Mth.clamp(dist, 0.1F, 1.0F);
95
			this.rangedAttackMob.performRangedAttack(this.target, power);
96
			this.attackTime = Mth.floor(dist * (this.attackIntervalMax - this.attackIntervalMin) + this.attackIntervalMin);
97
		} else if (this.attackTime < 0) {
98
			this.attackTime = Mth.floor(Mth.lerp(Math.sqrt(targetDistSqr) / this.attackRadius, this.attackIntervalMin, this.attackIntervalMax));
99
		}
100
	}
101
}
102

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