EntityLookup .java

net.minecraft.world.level.entity.EntityLookup (class, 59 lines): members: byUuid, getAllEntities, getEntities, byId, getEntity, count, remove, add, LOGGER.

net.minecraft.world.level.entityclass59 linescommonextends EntityAccess>1.7 KB

API Surface (5)

<U extends T> void getEntities(final EntityTypeTest<T, U> type, final AbortableIterationConsumer<U> consumer)
Iterable<T> getAllEntities()
void add(final T entity)
void remove(final T entity)
int count()

Fields & Constants (3)

LOGGER
byId
byUuid

Code Graph: Dependencies (2)

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

Full Source

EntityLookup.java59 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.entity;
2
3
import com.google.common.collect.Iterables;
4
import com.google.common.collect.Maps;
5
import com.mojang.logging.LogUtils;
6
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
7
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
8
import java.util.Map;
9
import java.util.UUID;
10
import net.minecraft.util.AbortableIterationConsumer;
11
import org.jspecify.annotations.Nullable;
12
import org.slf4j.Logger;
13
14
public class EntityLookup<T extends EntityAccess> {
15
	private static final Logger LOGGER = LogUtils.getLogger();
16
	private final Int2ObjectMap<T> byId = new Int2ObjectLinkedOpenHashMap<>();
17
	private final Map<UUID, T> byUuid = Maps.newHashMap();
18
19
	public <U extends T> void getEntities(final EntityTypeTest<T, U> type, final AbortableIterationConsumer<U> consumer) {
20
		for (T entity : this.byId.values()) {
21
			U maybeEntity = (U)type.tryCast(entity);
22
			if (maybeEntity != null && consumer.accept(maybeEntity).shouldAbort()) {
23
				return;
24
			}
25
		}
26
	}
27
28
	public Iterable<T> getAllEntities() {
29
		return Iterables.unmodifiableIterable(this.byId.values());
30
	}
31
32
	public void add(final T entity) {
33
		UUID uuid = entity.getUUID();
34
		if (this.byUuid.containsKey(uuid)) {
35
			LOGGER.warn("Duplicate entity UUID {}: {}", uuid, entity);
36
		} else {
37
			this.byUuid.put(uuid, entity);
38
			this.byId.put(entity.getId(), entity);
39
		}
40
	}
41
42
	public void remove(final T entity) {
43
		this.byUuid.remove(entity.getUUID());
44
		this.byId.remove(entity.getId());
45
	}
46
47
	public @Nullable T getEntity(final int id) {
48
		return this.byId.get(id);
49
	}
50
51
	public @Nullable T getEntity(final UUID id) {
52
		return this.byUuid.get(id);
53
	}
54
55
	public int count() {
56
		return this.byUuid.size();
57
	}
58
}
59

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