TargetGoal .java

net.minecraft.world.entity.ai.goal.target.TargetGoal (class): members: setUnseenMemoryTicks, EMPTY_REACH_CACHE, unseenMemoryTicks, CANT_REACH_CACHE, mob.

net.minecraft.world.entity.ai.goal.targetclass143 linescommonextends Goal3.4 KB

API Surface (6)

boolean canContinueToUse()
double getFollowDistance()
void start()
void stop()
boolean canAttack(final @Nullable LivingEntity target, final TargetingConditions targetConditions)
TargetGoal setUnseenMemoryTicks(final int unseenMemoryTicks)

Fields & Constants (11)

EMPTY_REACH_CACHE
CAN_REACH_CACHE
CANT_REACH_CACHE
mob
mustSee
mustReach
reachCache
reachCacheTime
unseenTicks
targetMob
unseenMemoryTicks

Full Source

TargetGoal.java143 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal.target;
2
3
import net.minecraft.world.entity.LivingEntity;
4
import net.minecraft.world.entity.Mob;
5
import net.minecraft.world.entity.ai.attributes.Attributes;
6
import net.minecraft.world.entity.ai.goal.Goal;
7
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
8
import net.minecraft.world.level.pathfinder.Node;
9
import net.minecraft.world.level.pathfinder.Path;
10
import net.minecraft.world.scores.Team;
11
import org.jspecify.annotations.Nullable;
12
13
public abstract class TargetGoal extends Goal {
14
	private static final int EMPTY_REACH_CACHE = 0;
15
	private static final int CAN_REACH_CACHE = 1;
16
	private static final int CANT_REACH_CACHE = 2;
17
	protected final Mob mob;
18
	protected final boolean mustSee;
19
	private final boolean mustReach;
20
	private int reachCache;
21
	private int reachCacheTime;
22
	private int unseenTicks;
23
	protected @Nullable LivingEntity targetMob;
24
	protected int unseenMemoryTicks = 60;
25
26
	public TargetGoal(final Mob mob, final boolean mustSee) {
27
		this(mob, mustSee, false);
28
	}
29
30
	public TargetGoal(final Mob mob, final boolean mustSee, final boolean mustReach) {
31
		this.mob = mob;
32
		this.mustSee = mustSee;
33
		this.mustReach = mustReach;
34
	}
35
36
	@Override
37
	public boolean canContinueToUse() {
38
		LivingEntity target = this.mob.getTarget();
39
		if (target == null) {
40
			target = this.targetMob;
41
		}
42
43
		if (target == null) {
44
			return false;
45
		}
46
47
		if (!this.mob.canAttack(target)) {
48
			return false;
49
		}
50
51
		Team mobTeam = this.mob.getTeam();
52
		Team targetTeam = target.getTeam();
53
		if (mobTeam != null && targetTeam == mobTeam) {
54
			return false;
55
		}
56
57
		double within = this.getFollowDistance();
58
		if (this.mob.distanceToSqr(target) > within * within) {
59
			return false;
60
		}
61
62
		if (this.mustSee) {
63
			if (this.mob.getSensing().hasLineOfSight(target)) {
64
				this.unseenTicks = 0;
65
			} else if (++this.unseenTicks > reducedTickDelay(this.unseenMemoryTicks)) {
66
				return false;
67
			}
68
		}
69
70
		this.mob.setTarget(target);
71
		return true;
72
	}
73
74
	protected double getFollowDistance() {
75
		return this.mob.getAttributeValue(Attributes.FOLLOW_RANGE);
76
	}
77
78
	@Override
79
	public void start() {
80
		this.reachCache = 0;
81
		this.reachCacheTime = 0;
82
		this.unseenTicks = 0;
83
	}
84
85
	@Override
86
	public void stop() {
87
		this.mob.setTarget(null);
88
		this.targetMob = null;
89
	}
90
91
	protected boolean canAttack(final @Nullable LivingEntity target, final TargetingConditions targetConditions) {
92
		if (target == null) {
93
			return false;
94
		}
95
96
		if (!targetConditions.test(getServerLevel(this.mob), this.mob, target)) {
97
			return false;
98
		}
99
100
		if (!this.mob.isWithinHome(target.blockPosition())) {
101
			return false;
102
		}
103
104
		if (this.mustReach) {
105
			if (--this.reachCacheTime <= 0) {
106
				this.reachCache = 0;
107
			}
108
109
			if (this.reachCache == 0) {
110
				this.reachCache = this.canReach(target) ? 1 : 2;
111
			}
112
113
			if (this.reachCache == 2) {
114
				return false;
115
			}
116
		}
117
118
		return true;
119
	}
120
121
	private boolean canReach(final LivingEntity target) {
122
		this.reachCacheTime = reducedTickDelay(10 + this.mob.getRandom().nextInt(5));
123
		Path path = this.mob.getNavigation().createPath(target, 0);
124
		if (path == null) {
125
			return false;
126
		}
127
128
		Node last = path.getEndNode();
129
		if (last == null) {
130
			return false;
131
		}
132
133
		int xx = last.x - target.getBlockX();
134
		int zz = last.z - target.getBlockZ();
135
		return xx * xx + zz * zz <= 2.25;
136
	}
137
138
	public TargetGoal setUnseenMemoryTicks(final int unseenMemoryTicks) {
139
		this.unseenMemoryTicks = unseenMemoryTicks;
140
		return this;
141
	}
142
}
143

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