ThreadedLevelLightEngine .java

net.minecraft.server.level.ThreadedLevelLightEngine (class, 219 lines): members: waitForPendingTasks, DEFAULT_BATCH_SIZE, tryScheduleUpdate, taskPerBatch.

net.minecraft.server.levelclass219 linesserverextends LevelLightEngine8.1 KB

Implements

API Surface (13)

void close()
int runLightUpdates()
void checkBlock(final BlockPos pos)
void updateChunkStatus(final ChunkPos pos)
void updateSectionStatus(final SectionPos pos, final boolean sectionEmpty)
void propagateLightSources(final ChunkPos pos)
void setLightEnabled(final ChunkPos pos, final boolean enable)
void queueSectionData(final LightLayer layer, final SectionPos pos, final @Nullable DataLayer data)
void retainData(final ChunkPos pos, final boolean retain)
CompletableFuture<ChunkAccess> initializeLight(final ChunkAccess chunk, final boolean lighted)
CompletableFuture<ChunkAccess> lightChunk(final ChunkAccess centerChunk, final boolean lighted)
void tryScheduleUpdate()
CompletableFuture<?> waitForPendingTasks(final int chunkX, final int chunkZ)

Fields & Constants (10)

DEFAULT_BATCH_SIZE
LOGGER
consecutiveExecutor
lightTasks
chunkMap
taskDispatcher
taskPerBatch
scheduled
lightChunkGetter
hasSkyLight

String literals (11)

"Ran automatically on a different thread!"
"checkBlock "
"updateChunkStatus "
" true"
"updateSectionStatus "
"propagateLight "
"enableLight "
"queueData "
"retainData "
"initializeLight: "
"lightChunk "

Full Source

ThreadedLevelLightEngine.java219 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.level;
2
3
import com.mojang.datafixers.util.Pair;
4
import com.mojang.logging.LogUtils;
5
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
6
import it.unimi.dsi.fastutil.objects.ObjectList;
7
import it.unimi.dsi.fastutil.objects.ObjectListIterator;
8
import java.util.concurrent.CompletableFuture;
9
import java.util.concurrent.atomic.AtomicBoolean;
10
import java.util.function.IntSupplier;
11
import net.minecraft.SharedConstants;
12
import net.minecraft.core.BlockPos;
13
import net.minecraft.core.SectionPos;
14
import net.minecraft.util.Util;
15
import net.minecraft.util.thread.ConsecutiveExecutor;
16
import net.minecraft.world.level.ChunkPos;
17
import net.minecraft.world.level.LightLayer;
18
import net.minecraft.world.level.chunk.ChunkAccess;
19
import net.minecraft.world.level.chunk.DataLayer;
20
import net.minecraft.world.level.chunk.LevelChunkSection;
21
import net.minecraft.world.level.chunk.LightChunkGetter;
22
import net.minecraft.world.level.lighting.LevelLightEngine;
23
import org.jspecify.annotations.Nullable;
24
import org.slf4j.Logger;
25
26
public class ThreadedLevelLightEngine extends LevelLightEngine implements AutoCloseable {
27
	public static final int DEFAULT_BATCH_SIZE = 1000;
28
	private static final Logger LOGGER = LogUtils.getLogger();
29
	private final ConsecutiveExecutor consecutiveExecutor;
30
	private final ObjectList<Pair<ThreadedLevelLightEngine.TaskType, Runnable>> lightTasks = new ObjectArrayList<>();
31
	private final ChunkMap chunkMap;
32
	private final ChunkTaskDispatcher taskDispatcher;
33
	private final int taskPerBatch = 1000;
34
	private final AtomicBoolean scheduled = new AtomicBoolean();
35
36
	public ThreadedLevelLightEngine(
37
		final LightChunkGetter lightChunkGetter,
38
		final ChunkMap chunkMap,
39
		final boolean hasSkyLight,
40
		final ConsecutiveExecutor consecutiveExecutor,
41
		final ChunkTaskDispatcher taskDispatcher
42
	) {
43
		super(lightChunkGetter, true, hasSkyLight);
44
		this.chunkMap = chunkMap;
45
		this.taskDispatcher = taskDispatcher;
46
		this.consecutiveExecutor = consecutiveExecutor;
47
	}
48
49
	@Override
50
	public void close() {
51
	}
52
53
	@Override
54
	public int runLightUpdates() {
55
		throw (UnsupportedOperationException)Util.pauseInIde(new UnsupportedOperationException("Ran automatically on a different thread!"));
56
	}
57
58
	@Override
59
	public void checkBlock(final BlockPos pos) {
60
		BlockPos immutable = pos.immutable();
61
		this.addTask(
62
			SectionPos.blockToSectionCoord(pos.getX()),
63
			SectionPos.blockToSectionCoord(pos.getZ()),
64
			ThreadedLevelLightEngine.TaskType.PRE_UPDATE,
65
			Util.name(() -> super.checkBlock(immutable), () -> "checkBlock " + immutable)
66
		);
67
	}
68
69
	protected void updateChunkStatus(final ChunkPos pos) {
70
		this.addTask(pos.x(), pos.z(), () -> 0, ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> {
71
			super.retainData(pos, false);
72
			super.setLightEnabled(pos, false);
73
74
			for (int sectionY = this.getMinLightSection(); sectionY < this.getMaxLightSection(); sectionY++) {
75
				super.queueSectionData(LightLayer.BLOCK, SectionPos.of(pos, sectionY), null);
76
				super.queueSectionData(LightLayer.SKY, SectionPos.of(pos, sectionY), null);
77
			}
78
79
			for (int sectionY = this.levelHeightAccessor.getMinSectionY(); sectionY <= this.levelHeightAccessor.getMaxSectionY(); sectionY++) {
80
				super.updateSectionStatus(SectionPos.of(pos, sectionY), true);
81
			}
82
		}, () -> "updateChunkStatus " + pos + " true"));
83
	}
84
85
	@Override
86
	public void updateSectionStatus(final SectionPos pos, final boolean sectionEmpty) {
87
		this.addTask(
88
			pos.x(),
89
			pos.z(),
90
			() -> 0,
91
			ThreadedLevelLightEngine.TaskType.PRE_UPDATE,
92
			Util.name(() -> super.updateSectionStatus(pos, sectionEmpty), () -> "updateSectionStatus " + pos + " " + sectionEmpty)
93
		);
94
	}
95
96
	@Override
97
	public void propagateLightSources(final ChunkPos pos) {
98
		this.addTask(pos.x(), pos.z(), ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> super.propagateLightSources(pos), () -> "propagateLight " + pos));
99
	}
100
101
	@Override
102
	public void setLightEnabled(final ChunkPos pos, final boolean enable) {
103
		this.addTask(
104
			pos.x(),
105
			pos.z(),
106
			ThreadedLevelLightEngine.TaskType.PRE_UPDATE,
107
			Util.name(() -> super.setLightEnabled(pos, enable), () -> "enableLight " + pos + " " + enable)
108
		);
109
	}
110
111
	@Override
112
	public void queueSectionData(final LightLayer layer, final SectionPos pos, final @Nullable DataLayer data) {
113
		this.addTask(
114
			pos.x(), pos.z(), () -> 0, ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> super.queueSectionData(layer, pos, data), () -> "queueData " + pos)
115
		);
116
	}
117
118
	private void addTask(final int chunkX, final int chunkZ, final ThreadedLevelLightEngine.TaskType type, final Runnable runnable) {
119
		this.addTask(chunkX, chunkZ, this.chunkMap.getChunkQueueLevel(ChunkPos.pack(chunkX, chunkZ)), type, runnable);
120
	}
121
122
	private void addTask(final int chunkX, final int chunkZ, final IntSupplier level, final ThreadedLevelLightEngine.TaskType type, final Runnable runnable) {
123
		this.taskDispatcher.submit(() -> {
124
			this.lightTasks.add(Pair.of(type, runnable));
125
			if (this.lightTasks.size() >= 1000) {
126
				this.runUpdate();
127
			}
128
		}, ChunkPos.pack(chunkX, chunkZ), level);
129
	}
130
131
	@Override
132
	public void retainData(final ChunkPos pos, final boolean retain) {
133
		this.addTask(
134
			pos.x(), pos.z(), () -> 0, ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> super.retainData(pos, retain), () -> "retainData " + pos)
135
		);
136
	}
137
138
	public CompletableFuture<ChunkAccess> initializeLight(final ChunkAccess chunk, final boolean lighted) {
139
		ChunkPos pos = chunk.getPos();
140
		this.addTask(pos.x(), pos.z(), ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> {
141
			LevelChunkSection[] sections = chunk.getSections();
142
143
			for (int sectionIndex = 0; sectionIndex < chunk.getSectionsCount(); sectionIndex++) {
144
				LevelChunkSection section = sections[sectionIndex];
145
				if (!section.hasOnlyAir()) {
146
					int sectionY = this.levelHeightAccessor.getSectionYFromSectionIndex(sectionIndex);
147
					super.updateSectionStatus(SectionPos.of(pos, sectionY), false);
148
				}
149
			}
150
		}, () -> "initializeLight: " + pos));
151
		return CompletableFuture.supplyAsync(() -> {
152
			super.setLightEnabled(pos, lighted);
153
			super.retainData(pos, false);
154
			return chunk;
155
		}, r -> this.addTask(pos.x(), pos.z(), ThreadedLevelLightEngine.TaskType.POST_UPDATE, r));
156
	}
157
158
	public CompletableFuture<ChunkAccess> lightChunk(final ChunkAccess centerChunk, final boolean lighted) {
159
		ChunkPos pos = centerChunk.getPos();
160
		centerChunk.setLightCorrect(false);
161
		this.addTask(pos.x(), pos.z(), ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> {
162
			if (!lighted) {
163
				super.propagateLightSources(pos);
164
			}
165
166
			if (SharedConstants.DEBUG_VERBOSE_SERVER_EVENTS) {
167
				LOGGER.debug("LIT {}", pos);
168
			}
169
		}, () -> "lightChunk " + pos + " " + lighted));
170
		return CompletableFuture.supplyAsync(() -> {
171
			centerChunk.setLightCorrect(true);
172
			return centerChunk;
173
		}, r -> this.addTask(pos.x(), pos.z(), ThreadedLevelLightEngine.TaskType.POST_UPDATE, r));
174
	}
175
176
	public void tryScheduleUpdate() {
177
		if ((!this.lightTasks.isEmpty() || super.hasLightWork()) && this.scheduled.compareAndSet(false, true)) {
178
			this.consecutiveExecutor.schedule(() -> {
179
				this.runUpdate();
180
				this.scheduled.set(false);
181
			});
182
		}
183
	}
184
185
	private void runUpdate() {
186
		int totalSize = Math.min(this.lightTasks.size(), 1000);
187
		ObjectListIterator<Pair<ThreadedLevelLightEngine.TaskType, Runnable>> iterator = this.lightTasks.iterator();
188
189
		int count;
190
		for (count = 0; iterator.hasNext() && count < totalSize; count++) {
191
			Pair<ThreadedLevelLightEngine.TaskType, Runnable> task = iterator.next();
192
			if (task.getFirst() == ThreadedLevelLightEngine.TaskType.PRE_UPDATE) {
193
				task.getSecond().run();
194
			}
195
		}
196
197
		iterator.back(count);
198
		super.runLightUpdates();
199
200
		for (int var5 = 0; iterator.hasNext() && var5 < totalSize; var5++) {
201
			Pair<ThreadedLevelLightEngine.TaskType, Runnable> task = iterator.next();
202
			if (task.getFirst() == ThreadedLevelLightEngine.TaskType.POST_UPDATE) {
203
				task.getSecond().run();
204
			}
205
206
			iterator.remove();
207
		}
208
	}
209
210
	public CompletableFuture<?> waitForPendingTasks(final int chunkX, final int chunkZ) {
211
		return CompletableFuture.runAsync(() -> {}, r -> this.addTask(chunkX, chunkZ, ThreadedLevelLightEngine.TaskType.POST_UPDATE, r));
212
	}
213
214
	private enum TaskType {
215
		PRE_UPDATE,
216
		POST_UPDATE;
217
	}
218
}
219

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