GoToTargetLocation .java

net.minecraft.world.entity.ai.behavior.GoToTargetLocation (class, 36 lines): members: create. imports: BehaviorBuilder, Mob, MemoryModuleType.

net.minecraft.world.entity.ai.behaviorclass36 linescommon1.4 KB

API Surface (1)

<E extends Mob> OneShot<E> create(final MemoryModuleType<BlockPos> locationMemory, final int closeEnoughDist, final float speedModifier)

Code Graph: Dependencies (7)

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

Full Source

GoToTargetLocation.java36 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.util.RandomSource;
5
import net.minecraft.world.entity.Mob;
6
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
7
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
8
9
public class GoToTargetLocation {
10
	private static BlockPos getNearbyPos(final Mob body, final BlockPos pos) {
11
		RandomSource random = body.level().getRandom();
12
		return pos.offset(getRandomOffset(random), 0, getRandomOffset(random));
13
	}
14
15
	private static int getRandomOffset(final RandomSource random) {
16
		return random.nextInt(3) - 1;
17
	}
18
19
	public static <E extends Mob> OneShot<E> create(final MemoryModuleType<BlockPos> locationMemory, final int closeEnoughDist, final float speedModifier) {
20
		return BehaviorBuilder.create(
21
			i -> i.group(
22
					i.present(locationMemory), i.absent(MemoryModuleType.ATTACK_TARGET), i.absent(MemoryModuleType.WALK_TARGET), i.registered(MemoryModuleType.LOOK_TARGET)
23
				)
24
				.apply(i, (location, attackTarget, walkTarget, lookTarget) -> (level, body, timestamp) -> {
25
					BlockPos celebrateLocation = i.get(location);
26
					boolean closeEnoughToTarget = celebrateLocation.closerThan(body.blockPosition(), closeEnoughDist);
27
					if (!closeEnoughToTarget) {
28
						BehaviorUtils.setWalkAndLookTargetMemories(body, getNearbyPos(body, celebrateLocation), speedModifier, closeEnoughDist);
29
					}
30
31
					return true;
32
				})
33
		);
34
	}
35
}
36

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