TransientEntitySectionManager .java

net.minecraft.world.level.entity.TransientEntitySectionManager (class): members: tickingChunks, startTicking, stopTicking, getEntityGetter, entityStorage.

net.minecraft.world.level.entityclass143 linescommonextends EntityAccess>5.6 KB

API Surface (8)

void startTicking(final ChunkPos pos)
void stopTicking(final ChunkPos pos)
LevelEntityGetter<T> getEntityGetter()
void addEntity(final T entity)
int count()
String gatherStats()
void onMove()
void onRemove(final Entity.RemovalReason reason)

Fields & Constants (6)

LOGGER
callbacks
entityStorage
sectionStorage
tickingChunks
entityGetter

Full Source

TransientEntitySectionManager.java143 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.entity;
2
3
import com.mojang.logging.LogUtils;
4
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
5
import it.unimi.dsi.fastutil.longs.LongSet;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.SectionPos;
8
import net.minecraft.util.VisibleForDebug;
9
import net.minecraft.world.entity.Entity;
10
import net.minecraft.world.level.ChunkPos;
11
import org.slf4j.Logger;
12
13
public class TransientEntitySectionManager<T extends EntityAccess> {
14
	private static final Logger LOGGER = LogUtils.getLogger();
15
	private final LevelCallback<T> callbacks;
16
	private final EntityLookup<T> entityStorage;
17
	private final EntitySectionStorage<T> sectionStorage;
18
	private final LongSet tickingChunks = new LongOpenHashSet();
19
	private final LevelEntityGetter<T> entityGetter;
20
21
	public TransientEntitySectionManager(final Class<T> entityClass, final LevelCallback<T> callbacks) {
22
		this.entityStorage = new EntityLookup<>();
23
		this.sectionStorage = new EntitySectionStorage<>(entityClass, key -> this.tickingChunks.contains(key) ? Visibility.TICKING : Visibility.TRACKED);
24
		this.callbacks = callbacks;
25
		this.entityGetter = new LevelEntityGetterAdapter<>(this.entityStorage, this.sectionStorage);
26
	}
27
28
	public void startTicking(final ChunkPos pos) {
29
		long chunkKey = pos.pack();
30
		this.tickingChunks.add(chunkKey);
31
		this.sectionStorage.getExistingSectionsInChunk(chunkKey).forEach(section -> {
32
			Visibility previousStatus = section.updateChunkStatus(Visibility.TICKING);
33
			if (!previousStatus.isTicking()) {
34
				section.getEntities().filter(e -> !e.isAlwaysTicking()).forEach(this.callbacks::onTickingStart);
35
			}
36
		});
37
	}
38
39
	public void stopTicking(final ChunkPos pos) {
40
		long chunkKey = pos.pack();
41
		this.tickingChunks.remove(chunkKey);
42
		this.sectionStorage.getExistingSectionsInChunk(chunkKey).forEach(section -> {
43
			Visibility previousStatus = section.updateChunkStatus(Visibility.TRACKED);
44
			if (previousStatus.isTicking()) {
45
				section.getEntities().filter(e -> !e.isAlwaysTicking()).forEach(this.callbacks::onTickingEnd);
46
			}
47
		});
48
	}
49
50
	public LevelEntityGetter<T> getEntityGetter() {
51
		return this.entityGetter;
52
	}
53
54
	public void addEntity(final T entity) {
55
		this.entityStorage.add(entity);
56
		long sectionKey = SectionPos.asLong(entity.blockPosition());
57
		EntitySection<T> entitySection = this.sectionStorage.getOrCreateSection(sectionKey);
58
		entitySection.add(entity);
59
		entity.setLevelCallback(new TransientEntitySectionManager.Callback(entity, sectionKey, entitySection));
60
		this.callbacks.onCreated(entity);
61
		this.callbacks.onTrackingStart(entity);
62
		if (entity.isAlwaysTicking() || entitySection.getStatus().isTicking()) {
63
			this.callbacks.onTickingStart(entity);
64
		}
65
	}
66
67
	@VisibleForDebug
68
	public int count() {
69
		return this.entityStorage.count();
70
	}
71
72
	private void removeSectionIfEmpty(final long sectionPos, final EntitySection<T> section) {
73
		if (section.isEmpty()) {
74
			this.sectionStorage.remove(sectionPos);
75
		}
76
	}
77
78
	@VisibleForDebug
79
	public String gatherStats() {
80
		return this.entityStorage.count() + "," + this.sectionStorage.count() + "," + this.tickingChunks.size();
81
	}
82
83
	private class Callback implements EntityInLevelCallback {
84
		private final T entity;
85
		private long currentSectionKey;
86
		private EntitySection<T> currentSection;
87
88
		private Callback(final T entity, final long currentSectionKey, final EntitySection<T> currentSection) {
89
			this.entity = entity;
90
			this.currentSectionKey = currentSectionKey;
91
			this.currentSection = currentSection;
92
		}
93
94
		@Override
95
		public void onMove() {
96
			BlockPos pos = this.entity.blockPosition();
97
			long newSectionPos = SectionPos.asLong(pos);
98
			if (newSectionPos != this.currentSectionKey) {
99
				Visibility previousStatus = this.currentSection.getStatus();
100
				if (!this.currentSection.remove(this.entity)) {
101
					TransientEntitySectionManager.LOGGER
102
						.warn("Entity {} wasn't found in section {} (moving to {})", this.entity, SectionPos.of(this.currentSectionKey), newSectionPos);
103
				}
104
105
				TransientEntitySectionManager.this.removeSectionIfEmpty(this.currentSectionKey, this.currentSection);
106
				EntitySection<T> newSection = TransientEntitySectionManager.this.sectionStorage.getOrCreateSection(newSectionPos);
107
				newSection.add(this.entity);
108
				this.currentSection = newSection;
109
				this.currentSectionKey = newSectionPos;
110
				TransientEntitySectionManager.this.callbacks.onSectionChange(this.entity);
111
				if (!this.entity.isAlwaysTicking()) {
112
					boolean wasTicking = previousStatus.isTicking();
113
					boolean isTicking = newSection.getStatus().isTicking();
114
					if (wasTicking && !isTicking) {
115
						TransientEntitySectionManager.this.callbacks.onTickingEnd(this.entity);
116
					} else if (!wasTicking && isTicking) {
117
						TransientEntitySectionManager.this.callbacks.onTickingStart(this.entity);
118
					}
119
				}
120
			}
121
		}
122
123
		@Override
124
		public void onRemove(final Entity.RemovalReason reason) {
125
			if (!this.currentSection.remove(this.entity)) {
126
				TransientEntitySectionManager.LOGGER
127
					.warn("Entity {} wasn't found in section {} (destroying due to {})", this.entity, SectionPos.of(this.currentSectionKey), reason);
128
			}
129
130
			Visibility status = this.currentSection.getStatus();
131
			if (status.isTicking() || this.entity.isAlwaysTicking()) {
132
				TransientEntitySectionManager.this.callbacks.onTickingEnd(this.entity);
133
			}
134
135
			TransientEntitySectionManager.this.callbacks.onTrackingEnd(this.entity);
136
			TransientEntitySectionManager.this.callbacks.onDestroyed(this.entity);
137
			TransientEntitySectionManager.this.entityStorage.remove(this.entity);
138
			this.entity.setLevelCallback(NULL);
139
			TransientEntitySectionManager.this.removeSectionIfEmpty(this.currentSectionKey, this.currentSection);
140
		}
141
	}
142
}
143

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