NearestVisibleLivingEntities .java

net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities (class): members: lineOfSightTest, nearbyEntities, findAll, findClosest, find, empty.

net.minecraft.world.entity.ai.memoryclass72 linescommon2.5 KB

API Surface (7)

NearestVisibleLivingEntities empty()
List<LivingEntity> nearbyEntities()
Optional<LivingEntity> findClosest(final Predicate<LivingEntity> filter)
Iterable<LivingEntity> findAll(final Predicate<LivingEntity> filter)
Stream<LivingEntity> find(final Predicate<LivingEntity> filter)
boolean contains(final LivingEntity targetEntity)
boolean contains(final Predicate<LivingEntity> filter)

Fields & Constants (3)

EMPTY
nearbyEntities
lineOfSightTest

Code Graph: Dependencies (4)

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

Full Source

NearestVisibleLivingEntities.java72 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.memory;
2
3
import com.google.common.collect.Iterables;
4
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
5
import java.util.List;
6
import java.util.Optional;
7
import java.util.function.Predicate;
8
import java.util.stream.Stream;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.util.VisibleForDebug;
11
import net.minecraft.world.entity.LivingEntity;
12
import net.minecraft.world.entity.ai.sensing.Sensor;
13
14
public class NearestVisibleLivingEntities {
15
	private static final NearestVisibleLivingEntities EMPTY = new NearestVisibleLivingEntities();
16
	private final List<LivingEntity> nearbyEntities;
17
	private final Predicate<LivingEntity> lineOfSightTest;
18
19
	private NearestVisibleLivingEntities() {
20
		this.nearbyEntities = List.of();
21
		this.lineOfSightTest = ignored -> false;
22
	}
23
24
	public NearestVisibleLivingEntities(final ServerLevel level, final LivingEntity body, final List<LivingEntity> livingEntities) {
25
		this.nearbyEntities = livingEntities;
26
		Object2BooleanOpenHashMap<LivingEntity> cache = new Object2BooleanOpenHashMap<>(livingEntities.size());
27
		Predicate<LivingEntity> targetTest = targetEntity -> Sensor.isEntityTargetable(level, body, targetEntity);
28
		this.lineOfSightTest = otherEntity -> cache.computeIfAbsent(otherEntity, targetTest);
29
	}
30
31
	public static NearestVisibleLivingEntities empty() {
32
		return EMPTY;
33
	}
34
35
	@VisibleForDebug
36
	public List<LivingEntity> nearbyEntities() {
37
		return this.nearbyEntities;
38
	}
39
40
	public Optional<LivingEntity> findClosest(final Predicate<LivingEntity> filter) {
41
		for (LivingEntity nearbyEntity : this.nearbyEntities) {
42
			if (filter.test(nearbyEntity) && this.lineOfSightTest.test(nearbyEntity)) {
43
				return Optional.of(nearbyEntity);
44
			}
45
		}
46
47
		return Optional.empty();
48
	}
49
50
	public Iterable<LivingEntity> findAll(final Predicate<LivingEntity> filter) {
51
		return Iterables.filter(this.nearbyEntities, entity -> filter.test(entity) && this.lineOfSightTest.test(entity));
52
	}
53
54
	public Stream<LivingEntity> find(final Predicate<LivingEntity> filter) {
55
		return this.nearbyEntities.stream().filter(entity -> filter.test(entity) && this.lineOfSightTest.test(entity));
56
	}
57
58
	public boolean contains(final LivingEntity targetEntity) {
59
		return this.nearbyEntities.contains(targetEntity) && this.lineOfSightTest.test(targetEntity);
60
	}
61
62
	public boolean contains(final Predicate<LivingEntity> filter) {
63
		for (LivingEntity nearbyEntity : this.nearbyEntities) {
64
			if (filter.test(nearbyEntity) && this.lineOfSightTest.test(nearbyEntity)) {
65
				return true;
66
			}
67
		}
68
69
		return false;
70
	}
71
}
72

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