ChunkGenerationTask .java

net.minecraft.server.level.ChunkGenerationTask (class): members: markedForCancellation, markForCancellation, needsGeneration, scheduledStatus, getCenter.

net.minecraft.server.levelclass176 linesserver6.2 KB

API Surface (3)

ChunkGenerationTask create(final GeneratingChunkMap chunkMap, final ChunkStatus targetStatus, final ChunkPos pos)
void markForCancellation()
GenerationChunkHolder getCenter()

Fields & Constants (8)

chunkMap
pos
scheduledStatus
targetStatus
markedForCancellation
scheduledLayer
cache
needsGeneration

String literals (1)

"scheduleLayer"

Full Source

ChunkGenerationTask.java176 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.level;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.concurrent.CompletableFuture;
6
import net.minecraft.util.StaticCache2D;
7
import net.minecraft.util.profiling.Profiler;
8
import net.minecraft.util.profiling.Zone;
9
import net.minecraft.world.level.ChunkPos;
10
import net.minecraft.world.level.chunk.ChunkAccess;
11
import net.minecraft.world.level.chunk.status.ChunkDependencies;
12
import net.minecraft.world.level.chunk.status.ChunkPyramid;
13
import net.minecraft.world.level.chunk.status.ChunkStatus;
14
import org.jspecify.annotations.Nullable;
15
16
public class ChunkGenerationTask {
17
	private final GeneratingChunkMap chunkMap;
18
	private final ChunkPos pos;
19
	private @Nullable ChunkStatus scheduledStatus = null;
20
	public final ChunkStatus targetStatus;
21
	private volatile boolean markedForCancellation;
22
	private final List<CompletableFuture<ChunkResult<ChunkAccess>>> scheduledLayer = new ArrayList<>();
23
	private final StaticCache2D<GenerationChunkHolder> cache;
24
	private boolean needsGeneration;
25
26
	private ChunkGenerationTask(
27
		final GeneratingChunkMap chunkMap, final ChunkStatus targetStatus, final ChunkPos pos, final StaticCache2D<GenerationChunkHolder> cache
28
	) {
29
		this.chunkMap = chunkMap;
30
		this.targetStatus = targetStatus;
31
		this.pos = pos;
32
		this.cache = cache;
33
	}
34
35
	public static ChunkGenerationTask create(final GeneratingChunkMap chunkMap, final ChunkStatus targetStatus, final ChunkPos pos) {
36
		int worstCaseRadius = ChunkPyramid.GENERATION_PYRAMID.getStepTo(targetStatus).getAccumulatedRadiusOf(ChunkStatus.EMPTY);
37
		StaticCache2D<GenerationChunkHolder> cache = StaticCache2D.create(
38
			pos.x(), pos.z(), worstCaseRadius, (x, z) -> chunkMap.acquireGeneration(ChunkPos.pack(x, z))
39
		);
40
		return new ChunkGenerationTask(chunkMap, targetStatus, pos, cache);
41
	}
42
43
	public @Nullable CompletableFuture<?> runUntilWait() {
44
		while (true) {
45
			CompletableFuture<?> waitingFor = this.waitForScheduledLayer();
46
			if (waitingFor != null) {
47
				return waitingFor;
48
			}
49
50
			if (this.markedForCancellation || this.scheduledStatus == this.targetStatus) {
51
				this.releaseClaim();
52
				return null;
53
			}
54
55
			this.scheduleNextLayer();
56
		}
57
	}
58
59
	private void scheduleNextLayer() {
60
		ChunkStatus statusToSchedule;
61
		if (this.scheduledStatus == null) {
62
			statusToSchedule = ChunkStatus.EMPTY;
63
		} else if (!this.needsGeneration && this.scheduledStatus == ChunkStatus.EMPTY && !this.canLoadWithoutGeneration()) {
64
			this.needsGeneration = true;
65
			statusToSchedule = ChunkStatus.EMPTY;
66
		} else {
67
			statusToSchedule = ChunkStatus.getStatusList().get(this.scheduledStatus.getIndex() + 1);
68
		}
69
70
		this.scheduleLayer(statusToSchedule, this.needsGeneration);
71
		this.scheduledStatus = statusToSchedule;
72
	}
73
74
	public void markForCancellation() {
75
		this.markedForCancellation = true;
76
	}
77
78
	private void releaseClaim() {
79
		GenerationChunkHolder chunkHolder = this.cache.get(this.pos.x(), this.pos.z());
80
		chunkHolder.removeTask(this);
81
		this.cache.forEach(this.chunkMap::releaseGeneration);
82
	}
83
84
	private boolean canLoadWithoutGeneration() {
85
		if (this.targetStatus == ChunkStatus.EMPTY) {
86
			return true;
87
		}
88
89
		ChunkStatus highestGeneratedStatus = this.cache.get(this.pos.x(), this.pos.z()).getPersistedStatus();
90
		if (highestGeneratedStatus != null && !highestGeneratedStatus.isBefore(this.targetStatus)) {
91
			ChunkDependencies dependencies = ChunkPyramid.LOADING_PYRAMID.getStepTo(this.targetStatus).accumulatedDependencies();
92
			int range = dependencies.getRadius();
93
94
			for (int x = this.pos.x() - range; x <= this.pos.x() + range; x++) {
95
				for (int z = this.pos.z() - range; z <= this.pos.z() + range; z++) {
96
					int distance = this.pos.getChessboardDistance(x, z);
97
					ChunkStatus requiredStatus = dependencies.get(distance);
98
					ChunkStatus persistedStatus = this.cache.get(x, z).getPersistedStatus();
99
					if (persistedStatus == null || persistedStatus.isBefore(requiredStatus)) {
100
						return false;
101
					}
102
				}
103
			}
104
105
			return true;
106
		} else {
107
			return false;
108
		}
109
	}
110
111
	public GenerationChunkHolder getCenter() {
112
		return this.cache.get(this.pos.x(), this.pos.z());
113
	}
114
115
	private void scheduleLayer(final ChunkStatus status, final boolean needsGeneration) {
116
		try (Zone zone = Profiler.get().zone("scheduleLayer")) {
117
			zone.addText(status::getName);
118
			int radius = this.getRadiusForLayer(status, needsGeneration);
119
120
			for (int x = this.pos.x() - radius; x <= this.pos.x() + radius; x++) {
121
				for (int z = this.pos.z() - radius; z <= this.pos.z() + radius; z++) {
122
					GenerationChunkHolder chunkHolder = this.cache.get(x, z);
123
					if (this.markedForCancellation || !this.scheduleChunkInLayer(status, needsGeneration, chunkHolder)) {
124
						return;
125
					}
126
				}
127
			}
128
		}
129
	}
130
131
	private int getRadiusForLayer(final ChunkStatus status, final boolean needsGeneration) {
132
		ChunkPyramid pyramid = needsGeneration ? ChunkPyramid.GENERATION_PYRAMID : ChunkPyramid.LOADING_PYRAMID;
133
		return pyramid.getStepTo(this.targetStatus).getAccumulatedRadiusOf(status);
134
	}
135
136
	private boolean scheduleChunkInLayer(final ChunkStatus status, final boolean needsGeneration, final GenerationChunkHolder chunkHolder) {
137
		ChunkStatus persistedStatus = chunkHolder.getPersistedStatus();
138
		boolean generate = persistedStatus != null && status.isAfter(persistedStatus);
139
		ChunkPyramid pyramid = generate ? ChunkPyramid.GENERATION_PYRAMID : ChunkPyramid.LOADING_PYRAMID;
140
		if (generate && !needsGeneration) {
141
			throw new IllegalStateException("Can't load chunk, but didn't expect to need to generate");
142
		}
143
144
		CompletableFuture<ChunkResult<ChunkAccess>> future = chunkHolder.applyStep(pyramid.getStepTo(status), this.chunkMap, this.cache);
145
		ChunkResult<ChunkAccess> now = future.getNow(null);
146
		if (now == null) {
147
			this.scheduledLayer.add(future);
148
			return true;
149
		}
150
151
		if (now.isSuccess()) {
152
			return true;
153
		}
154
155
		this.markForCancellation();
156
		return false;
157
	}
158
159
	private @Nullable CompletableFuture<?> waitForScheduledLayer() {
160
		while (!this.scheduledLayer.isEmpty()) {
161
			CompletableFuture<ChunkResult<ChunkAccess>> lastFuture = this.scheduledLayer.getLast();
162
			ChunkResult<ChunkAccess> resultNow = lastFuture.getNow(null);
163
			if (resultNow == null) {
164
				return lastFuture;
165
			}
166
167
			this.scheduledLayer.removeLast();
168
			if (!resultNow.isSuccess()) {
169
				this.markForCancellation();
170
			}
171
		}
172
173
		return null;
174
	}
175
}
176

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