NearestLivingEntitySensor .java

net.minecraft.world.entity.ai.sensing.NearestLivingEntitySensor (class, 32 lines): extends Sensor. members: doTick, requires. imports: ImmutableSet, Brain.

net.minecraft.world.entity.ai.sensingclass32 linescommonextends LivingEntity>1.5 KB

API Surface (2)

void doTick(final ServerLevel level, final T body)
Set<MemoryModuleType<?>> requires()

Code Graph: Dependencies (8)

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

Full Source

NearestLivingEntitySensor.java32 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.sensing;
2
3
import com.google.common.collect.ImmutableSet;
4
import java.util.Comparator;
5
import java.util.List;
6
import java.util.Set;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.world.entity.LivingEntity;
9
import net.minecraft.world.entity.ai.Brain;
10
import net.minecraft.world.entity.ai.attributes.Attributes;
11
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
12
import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
13
import net.minecraft.world.phys.AABB;
14
15
public class NearestLivingEntitySensor<T extends LivingEntity> extends Sensor<T> {
16
	@Override
17
	protected void doTick(final ServerLevel level, final T body) {
18
		double followRange = body.getAttributeValue(Attributes.FOLLOW_RANGE);
19
		AABB boundingBox = body.getBoundingBox().inflate(followRange, followRange, followRange);
20
		List<LivingEntity> livingEntities = level.getEntitiesOfClass(LivingEntity.class, boundingBox, mob -> mob != body && mob.isAlive());
21
		livingEntities.sort(Comparator.comparingDouble(body::distanceToSqr));
22
		Brain<?> brain = body.getBrain();
23
		brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, livingEntities);
24
		brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, body, livingEntities));
25
	}
26
27
	@Override
28
	public Set<MemoryModuleType<?>> requires() {
29
		return ImmutableSet.of(MemoryModuleType.NEAREST_LIVING_ENTITIES, MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES);
30
	}
31
}
32

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