VillageBoundRandomStroll .java

net.minecraft.world.entity.ai.behavior.VillageBoundRandomStroll (class): members: MAX_XZ_DIST, MAX_Y_DIST, create. imports: LandRandomPos, WalkTarget.

net.minecraft.world.entity.ai.behaviorclass43 linescommon1.7 KB

API Surface (2)

OneShot<PathfinderMob> create(final float speedModifier)
OneShot<PathfinderMob> create(final float speedModifier, final int maxXyDist, final int maxYDist)

Fields & Constants (2)

MAX_XZ_DIST
MAX_Y_DIST

Full Source

VillageBoundRandomStroll.java43 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import java.util.Optional;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.SectionPos;
6
import net.minecraft.world.entity.PathfinderMob;
7
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
8
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
9
import net.minecraft.world.entity.ai.memory.WalkTarget;
10
import net.minecraft.world.entity.ai.util.DefaultRandomPos;
11
import net.minecraft.world.entity.ai.util.LandRandomPos;
12
import net.minecraft.world.phys.Vec3;
13
14
public class VillageBoundRandomStroll {
15
	private static final int MAX_XZ_DIST = 10;
16
	private static final int MAX_Y_DIST = 7;
17
18
	public static OneShot<PathfinderMob> create(final float speedModifier) {
19
		return create(speedModifier, 10, 7);
20
	}
21
22
	public static OneShot<PathfinderMob> create(final float speedModifier, final int maxXyDist, final int maxYDist) {
23
		return BehaviorBuilder.create(i -> i.group(i.absent(MemoryModuleType.WALK_TARGET)).apply(i, walkTarget -> (level, body, timestamp) -> {
24
			BlockPos bodyPos = body.blockPosition();
25
			Vec3 landPos;
26
			if (level.isVillage(bodyPos)) {
27
				landPos = LandRandomPos.getPos(body, maxXyDist, maxYDist);
28
			} else {
29
				SectionPos sectionPos = SectionPos.of(bodyPos);
30
				SectionPos optimalSectionPos = BehaviorUtils.findSectionClosestToVillage(level, sectionPos, 2);
31
				if (optimalSectionPos != sectionPos) {
32
					landPos = DefaultRandomPos.getPosTowards(body, maxXyDist, maxYDist, Vec3.atBottomCenterOf(optimalSectionPos.center()), (float) (Math.PI / 2));
33
				} else {
34
					landPos = LandRandomPos.getPos(body, maxXyDist, maxYDist);
35
				}
36
			}
37
38
			walkTarget.setOrErase(Optional.ofNullable(landPos).map(pos -> new WalkTarget(pos, speedModifier, 0)));
39
			return true;
40
		}));
41
	}
42
}
43

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