DefaultRandomPos .java

net.minecraft.world.entity.ai.util.DefaultRandomPos (class, 59 lines): members: getPosAway, towardsPos, restrict, getPosTowards, getPos. imports: Vec3.

net.minecraft.world.entity.ai.utilclass59 linescommon2.4 KB

API Surface (0)

No public/protected methods detected (fields or constants only).

Fields & Constants (2)

towardsPos
restrict

Code Graph: Dependencies (5)

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

Full Source

DefaultRandomPos.java59 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.util;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.world.entity.PathfinderMob;
5
import net.minecraft.world.phys.Vec3;
6
import org.jspecify.annotations.Nullable;
7
8
public class DefaultRandomPos {
9
	public static @Nullable Vec3 getPos(final PathfinderMob mob, final int horizontalDist, final int verticalDist) {
10
		boolean restrict = GoalUtils.mobRestricted(mob, horizontalDist);
11
		return RandomPos.generateRandomPos(mob, () -> {
12
			BlockPos direction = RandomPos.generateRandomDirection(mob.getRandom(), horizontalDist, verticalDist);
13
			return generateRandomPosTowardDirection(mob, horizontalDist, restrict, direction);
14
		});
15
	}
16
17
	public static @Nullable Vec3 getPosTowards(
18
		final PathfinderMob mob, final int horizontalDist, final int verticalDist, final Vec3 towardsPos, final double maxXzRadiansFromDir
19
	) {
20
		Vec3 dir = towardsPos.subtract(mob.getX(), mob.getY(), mob.getZ());
21
		boolean restrict = GoalUtils.mobRestricted(mob, horizontalDist);
22
		return RandomPos.generateRandomPos(
23
			mob,
24
			() -> {
25
				BlockPos direction = RandomPos.generateRandomDirectionWithinRadians(
26
					mob.getRandom(), 0.0, horizontalDist, verticalDist, 0, dir.x, dir.z, maxXzRadiansFromDir
27
				);
28
				return direction == null ? null : generateRandomPosTowardDirection(mob, horizontalDist, restrict, direction);
29
			}
30
		);
31
	}
32
33
	public static @Nullable Vec3 getPosAway(final PathfinderMob mob, final int horizontalDist, final int verticalDist, final Vec3 avoidPos) {
34
		Vec3 dirAway = mob.position().subtract(avoidPos);
35
		boolean restrict = GoalUtils.mobRestricted(mob, horizontalDist);
36
		return RandomPos.generateRandomPos(
37
			mob,
38
			() -> {
39
				BlockPos direction = RandomPos.generateRandomDirectionWithinRadians(
40
					mob.getRandom(), 0.0, horizontalDist, verticalDist, 0, dirAway.x, dirAway.z, (float) (Math.PI / 2)
41
				);
42
				return direction == null ? null : generateRandomPosTowardDirection(mob, horizontalDist, restrict, direction);
43
			}
44
		);
45
	}
46
47
	private static @Nullable BlockPos generateRandomPosTowardDirection(
48
		final PathfinderMob mob, final int horizontalDist, final boolean restrict, final BlockPos direction
49
	) {
50
		BlockPos pos = RandomPos.generateRandomPosTowardDirection(mob, horizontalDist, mob.getRandom(), direction);
51
		return !GoalUtils.isOutsideLimits(pos, mob)
52
				&& !GoalUtils.isRestricted(restrict, mob, pos)
53
				&& !GoalUtils.isNotStable(mob.getNavigation(), pos)
54
				&& !GoalUtils.hasMalus(mob, pos)
55
			? pos
56
			: null;
57
	}
58
}
59

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