SetLookAndInteract .java

net.minecraft.world.entity.ai.behavior.SetLookAndInteract (class): members: create. imports: NearestVisibleLivingEntities, BehaviorBuilder, EntityType.

net.minecraft.world.entity.ai.behaviorclass35 linescommon1.3 KB

API Surface (1)

BehaviorControl<LivingEntity> create(final EntityType<?> type, final int interactionRange)

Code Graph: Dependencies (6)

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

Full Source

SetLookAndInteract.java35 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.behavior;
2
3
import java.util.Optional;
4
import net.minecraft.world.entity.EntityType;
5
import net.minecraft.world.entity.LivingEntity;
6
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
7
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
8
import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
9
10
public class SetLookAndInteract {
11
	public static BehaviorControl<LivingEntity> create(final EntityType<?> type, final int interactionRange) {
12
		int interactionRangeSqr = interactionRange * interactionRange;
13
		return BehaviorBuilder.create(
14
			i -> i.group(
15
					i.registered(MemoryModuleType.LOOK_TARGET), i.absent(MemoryModuleType.INTERACTION_TARGET), i.present(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
16
				)
17
				.apply(
18
					i,
19
					(lookTarget, interactionTarget, nearestEntities) -> (level, body, timestamp) -> {
20
						Optional<LivingEntity> closest = i.<NearestVisibleLivingEntities>get(nearestEntities)
21
							.findClosest(e -> e.distanceToSqr(body) <= interactionRangeSqr && e.is(type));
22
						if (closest.isEmpty()) {
23
							return false;
24
						}
25
26
						LivingEntity closestEntity = closest.get();
27
						interactionTarget.set(closestEntity);
28
						lookTarget.set(new EntityTracker(closestEntity, true));
29
						return true;
30
					}
31
				)
32
		);
33
	}
34
}
35

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