MoveTowardsTargetGoal .java

net.minecraft.world.entity.ai.goal.MoveTowardsTargetGoal (class): members: within, wantedX, wantedY, wantedZ, speedModifier, canContinueToUse, target, mob.

net.minecraft.world.entity.ai.goalclass63 linescommonextends Goal1.6 KB

API Surface (4)

boolean canUse()
boolean canContinueToUse()
void stop()
void start()

Fields & Constants (7)

mob
target
wantedX
wantedY
wantedZ
speedModifier
within

Code Graph: Dependencies (5)

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

Full Source

MoveTowardsTargetGoal.java63 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.goal;
2
3
import java.util.EnumSet;
4
import net.minecraft.world.entity.LivingEntity;
5
import net.minecraft.world.entity.PathfinderMob;
6
import net.minecraft.world.entity.ai.util.DefaultRandomPos;
7
import net.minecraft.world.phys.Vec3;
8
import org.jspecify.annotations.Nullable;
9
10
public class MoveTowardsTargetGoal extends Goal {
11
	private final PathfinderMob mob;
12
	private @Nullable LivingEntity target;
13
	private double wantedX;
14
	private double wantedY;
15
	private double wantedZ;
16
	private final double speedModifier;
17
	private final float within;
18
19
	public MoveTowardsTargetGoal(final PathfinderMob mob, final double speedModifier, final float within) {
20
		this.mob = mob;
21
		this.speedModifier = speedModifier;
22
		this.within = within;
23
		this.setFlags(EnumSet.of(Goal.Flag.MOVE));
24
	}
25
26
	@Override
27
	public boolean canUse() {
28
		this.target = this.mob.getTarget();
29
		if (this.target == null) {
30
			return false;
31
		}
32
33
		if (this.target.distanceToSqr(this.mob) > this.within * this.within) {
34
			return false;
35
		}
36
37
		Vec3 pos = DefaultRandomPos.getPosTowards(this.mob, 16, 7, this.target.position(), (float) (Math.PI / 2));
38
		if (pos == null) {
39
			return false;
40
		}
41
42
		this.wantedX = pos.x;
43
		this.wantedY = pos.y;
44
		this.wantedZ = pos.z;
45
		return true;
46
	}
47
48
	@Override
49
	public boolean canContinueToUse() {
50
		return !this.mob.getNavigation().isDone() && this.target.isAlive() && this.target.distanceToSqr(this.mob) < this.within * this.within;
51
	}
52
53
	@Override
54
	public void stop() {
55
		this.target = null;
56
	}
57
58
	@Override
59
	public void start() {
60
		this.mob.getNavigation().moveTo(this.wantedX, this.wantedY, this.wantedZ, this.speedModifier);
61
	}
62
}
63

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