TargetingConditions .java

net.minecraft.world.entity.ai.targeting.TargetingConditions (class, 103 lines): members: MIN_VISIBILITY_DISTANCE_FOR_INVISIBLE_TARGET, ignoreLineOfSight.

net.minecraft.world.entity.ai.targetingclass103 linescommon2.8 KB

API Surface (8)

TargetingConditions forCombat()
TargetingConditions forNonCombat()
TargetingConditions copy()
TargetingConditions range(final double range)
TargetingConditions ignoreLineOfSight()
TargetingConditions ignoreInvisibilityTesting()
TargetingConditions selector(final TargetingConditions.@Nullable Selector selector)
boolean test(final ServerLevel level, final @Nullable LivingEntity targeter, final LivingEntity target)

Fields & Constants (7)

DEFAULT
MIN_VISIBILITY_DISTANCE_FOR_INVISIBLE_TARGET
isCombat
range
checkLineOfSight
testInvisible
selector

Same Package (1)

Code Graph: Dependencies (4)

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

Full Source

TargetingConditions.java103 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.targeting;
2
3
import net.minecraft.server.level.ServerLevel;
4
import net.minecraft.world.Difficulty;
5
import net.minecraft.world.entity.LivingEntity;
6
import net.minecraft.world.entity.Mob;
7
import org.jspecify.annotations.Nullable;
8
9
public class TargetingConditions {
10
	public static final TargetingConditions DEFAULT = forCombat();
11
	private static final double MIN_VISIBILITY_DISTANCE_FOR_INVISIBLE_TARGET = 2.0;
12
	private final boolean isCombat;
13
	private double range = -1.0;
14
	private boolean checkLineOfSight = true;
15
	private boolean testInvisible = true;
16
	private TargetingConditions.@Nullable Selector selector;
17
18
	private TargetingConditions(final boolean isCombat) {
19
		this.isCombat = isCombat;
20
	}
21
22
	public static TargetingConditions forCombat() {
23
		return new TargetingConditions(true);
24
	}
25
26
	public static TargetingConditions forNonCombat() {
27
		return new TargetingConditions(false);
28
	}
29
30
	public TargetingConditions copy() {
31
		TargetingConditions clone = this.isCombat ? forCombat() : forNonCombat();
32
		clone.range = this.range;
33
		clone.checkLineOfSight = this.checkLineOfSight;
34
		clone.testInvisible = this.testInvisible;
35
		clone.selector = this.selector;
36
		return clone;
37
	}
38
39
	public TargetingConditions range(final double range) {
40
		this.range = range;
41
		return this;
42
	}
43
44
	public TargetingConditions ignoreLineOfSight() {
45
		this.checkLineOfSight = false;
46
		return this;
47
	}
48
49
	public TargetingConditions ignoreInvisibilityTesting() {
50
		this.testInvisible = false;
51
		return this;
52
	}
53
54
	public TargetingConditions selector(final TargetingConditions.@Nullable Selector selector) {
55
		this.selector = selector;
56
		return this;
57
	}
58
59
	public boolean test(final ServerLevel level, final @Nullable LivingEntity targeter, final LivingEntity target) {
60
		if (targeter == target) {
61
			return false;
62
		}
63
64
		if (!target.canBeSeenByAnyone()) {
65
			return false;
66
		}
67
68
		if (this.selector != null && !this.selector.test(target, level)) {
69
			return false;
70
		}
71
72
		if (targeter == null) {
73
			if (this.isCombat && (!target.canBeSeenAsEnemy() || level.getDifficulty() == Difficulty.PEACEFUL)) {
74
				return false;
75
			}
76
		} else {
77
			if (this.isCombat && (!targeter.canAttack(target) || targeter.isAlliedTo(target))) {
78
				return false;
79
			}
80
81
			if (this.range > 0.0) {
82
				double modifier = this.testInvisible ? target.getVisibilityPercent(level, targeter) : 1.0;
83
				double visibilityDistance = Math.max(this.range * modifier, 2.0);
84
				double distanceToSqr = targeter.distanceToSqr(target.getX(), target.getY(), target.getZ());
85
				if (distanceToSqr > visibilityDistance * visibilityDistance) {
86
					return false;
87
				}
88
			}
89
90
			if (this.checkLineOfSight && targeter instanceof Mob mob && !mob.getSensing().hasLineOfSight(target)) {
91
				return false;
92
			}
93
		}
94
95
		return true;
96
	}
97
98
	@FunctionalInterface
99
	public interface Selector {
100
		boolean test(LivingEntity target, ServerLevel level);
101
	}
102
}
103

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