NearestAttackableTargetGoal .java

net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal (class, 92 lines): members: DEFAULT_RANDOM_INTERVAL, getTargetSearchArea, findTarget.

net.minecraft.world.entity.ai.goal.targetclass92 linescommonextends LivingEntity>3.1 KB

API Surface (5)

boolean canUse()
AABB getTargetSearchArea(final double followDistance)
void findTarget()
void start()
void setTarget(final @Nullable LivingEntity target)

Fields & Constants (8)

DEFAULT_RANDOM_INTERVAL
targetType
randomInterval
target
targetConditions
mob
mustSee
mustReach

Full Source

NearestAttackableTargetGoal.java92 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal.target;
2
3
import java.util.EnumSet;
4
import net.minecraft.server.level.ServerLevel;
5
import net.minecraft.server.level.ServerPlayer;
6
import net.minecraft.world.entity.LivingEntity;
7
import net.minecraft.world.entity.Mob;
8
import net.minecraft.world.entity.ai.goal.Goal;
9
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
10
import net.minecraft.world.entity.player.Player;
11
import net.minecraft.world.phys.AABB;
12
import org.jspecify.annotations.Nullable;
13
14
public class NearestAttackableTargetGoal<T extends LivingEntity> extends TargetGoal {
15
	private static final int DEFAULT_RANDOM_INTERVAL = 10;
16
	protected final Class<T> targetType;
17
	protected final int randomInterval;
18
	protected @Nullable LivingEntity target;
19
	protected final TargetingConditions targetConditions;
20
21
	public NearestAttackableTargetGoal(final Mob mob, final Class<T> targetType, final boolean mustSee) {
22
		this(mob, targetType, 10, mustSee, false, null);
23
	}
24
25
	public NearestAttackableTargetGoal(final Mob mob, final Class<T> targetType, final boolean mustSee, final TargetingConditions.Selector selector) {
26
		this(mob, targetType, 10, mustSee, false, selector);
27
	}
28
29
	public NearestAttackableTargetGoal(final Mob mob, final Class<T> targetType, final boolean mustSee, final boolean mustReach) {
30
		this(mob, targetType, 10, mustSee, mustReach, null);
31
	}
32
33
	public NearestAttackableTargetGoal(
34
		final Mob mob,
35
		final Class<T> targetType,
36
		final int randomInterval,
37
		final boolean mustSee,
38
		final boolean mustReach,
39
		final TargetingConditions.@Nullable Selector selector
40
	) {
41
		super(mob, mustSee, mustReach);
42
		this.targetType = targetType;
43
		this.randomInterval = reducedTickDelay(randomInterval);
44
		this.setFlags(EnumSet.of(Goal.Flag.TARGET));
45
		this.targetConditions = TargetingConditions.forCombat().range(this.getFollowDistance()).selector(selector);
46
	}
47
48
	@Override
49
	public boolean canUse() {
50
		if (this.randomInterval > 0 && this.mob.getRandom().nextInt(this.randomInterval) != 0) {
51
			return false;
52
		}
53
54
		this.findTarget();
55
		return this.target != null;
56
	}
57
58
	protected AABB getTargetSearchArea(final double followDistance) {
59
		return this.mob.getBoundingBox().inflate(followDistance, followDistance, followDistance);
60
	}
61
62
	protected void findTarget() {
63
		ServerLevel level = getServerLevel(this.mob);
64
		if (this.targetType != Player.class && this.targetType != ServerPlayer.class) {
65
			this.target = level.getNearestEntity(
66
				this.mob.level().getEntitiesOfClass(this.targetType, this.getTargetSearchArea(this.getFollowDistance()), entity -> true),
67
				this.getTargetConditions(),
68
				this.mob,
69
				this.mob.getX(),
70
				this.mob.getEyeY(),
71
				this.mob.getZ()
72
			);
73
		} else {
74
			this.target = level.getNearestPlayer(this.getTargetConditions(), this.mob, this.mob.getX(), this.mob.getEyeY(), this.mob.getZ());
75
		}
76
	}
77
78
	@Override
79
	public void start() {
80
		this.mob.setTarget(this.target);
81
		super.start();
82
	}
83
84
	public void setTarget(final @Nullable LivingEntity target) {
85
		this.target = target;
86
	}
87
88
	private TargetingConditions getTargetConditions() {
89
		return this.targetConditions.range(this.getFollowDistance());
90
	}
91
}
92

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