ChunkAccess .java

net.minecraft.world.level.chunk.ChunkAccess (class): members: getHighestFilledSectionIndex, getHighestGeneratedStatus, getHighestSectionPosition, unsaved.

net.minecraft.world.level.chunkclass485 linescommon15.7 KB

Implements

API Surface (52)

GameEventListenerRegistry getListenerRegistry(final int section)
int getHighestFilledSectionIndex()
int getHighestSectionPosition()
Set<BlockPos> getBlockEntitiesPos()
LevelChunkSection[] getSections()
LevelChunkSection getSection(final int sectionIndex)
Collection<Entry<Heightmap.Types, Heightmap>> getHeightmaps()
void setHeightmap(final Heightmap.Types key, final long[] data)
Heightmap getOrCreateHeightmapUnprimed(final Heightmap.Types type)
boolean hasPrimedHeightmap(final Heightmap.Types type)
int getHeight(final Heightmap.Types type, final int x, final int z)
ChunkPos getPos()
void setStartForStructure(final Structure structure, final StructureStart structureStart)
Map<Structure, StructureStart> getAllStarts()
void setAllStarts(final Map<Structure, StructureStart> starts)
LongSet getReferencesForStructure(final Structure structure)
void addReferenceForStructure(final Structure structure, final long reference)
Map<Structure, LongSet> getAllReferences()
void setAllReferences(final Map<Structure, LongSet> data)
boolean isYSpaceEmpty(int yStartInclusive, int yEndInclusive)
void markUnsaved()
boolean tryMarkSaved()
boolean isUnsaved()
ChunkStatus getHighestGeneratedStatus()
void markPosForPostProcessing(final BlockPos blockPos)
void addPackedPostProcess(final ShortList packedOffsets, final int sectionIndex)
void setBlockEntityNbt(final CompoundTag entityTag)
void findBlockLightSources(final BiConsumer<BlockPos, BlockState> consumer)
void findBlocks(final Predicate<BlockState> predicate, final BiConsumer<BlockPos, BlockState> consumer)
void collectBiomesInPalette(final Set<Holder<Biome>> output)
boolean canBeSerialized()
UpgradeData getUpgradeData()
boolean isOldNoiseGeneration()
long getInhabitedTime()
void incrementInhabitedTime()
void setInhabitedTime(final long inhabitedTime)
ShortList getOrCreateOffsetList(final @Nullable ShortList[] list, final int sectionIndex)
boolean isLightCorrect()
void setLightCorrect(final boolean isLightCorrect)
int getMinY()
int getHeight()
BiomeGenerationSettings carverBiome(final Supplier<BiomeGenerationSettings> source)
Holder<Biome> getNoiseBiome(final int quartX, final int quartY, final int quartZ)
void fillBiomesFromNoise(final BiomeResolver biomeResolver)
boolean isUpgrading()
LevelHeightAccessor getHeightAccessorForGeneration()
void initializeLightSources()
ChunkSkyLightSources getSkyLightSources()
ProblemReporter.PathElement problemPath(final ChunkPos pos)
ProblemReporter.PathElement problemPath()
String get()
record PackedTicks(List<SavedTick<Block>> blocks, List<SavedTick<Fluid>> fluids)

Fields & Constants (20)

NO_FILLED_SECTION
LOGGER
EMPTY_REFERENCE_SET
postProcessing
unsaved
isLightCorrect
chunkPos
inhabitedTime
carverBiomeSettings
upgradeData
blendingData
heightmaps
skyLightSources
structureStarts
structureReferences
pendingBlockEntities
blockEntities
levelHeightAccessor
sections
containerFactory

String literals (4)

"Getting biome"
"Biome being got"
"Location"
"chunk@"

Full Source

ChunkAccess.java485 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.chunk;
2
3
import com.google.common.collect.Maps;
4
import com.google.common.collect.Sets;
5
import com.mojang.logging.LogUtils;
6
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
7
import it.unimi.dsi.fastutil.longs.LongSet;
8
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
9
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
10
import it.unimi.dsi.fastutil.shorts.ShortList;
11
import java.util.Collection;
12
import java.util.Collections;
13
import java.util.EnumSet;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17
import java.util.Map.Entry;
18
import java.util.function.BiConsumer;
19
import java.util.function.Predicate;
20
import java.util.function.Supplier;
21
import net.fabricmc.fabric.api.attachment.v1.AttachmentTarget;
22
import net.minecraft.CrashReport;
23
import net.minecraft.CrashReportCategory;
24
import net.minecraft.ReportedException;
25
import net.minecraft.SharedConstants;
26
import net.minecraft.core.BlockPos;
27
import net.minecraft.core.Holder;
28
import net.minecraft.core.HolderLookup;
29
import net.minecraft.core.QuartPos;
30
import net.minecraft.core.SectionPos;
31
import net.minecraft.nbt.CompoundTag;
32
import net.minecraft.util.Continuation;
33
import net.minecraft.util.Mth;
34
import net.minecraft.util.ProblemReporter;
35
import net.minecraft.world.entity.Entity;
36
import net.minecraft.world.level.ChunkPos;
37
import net.minecraft.world.level.LevelHeightAccessor;
38
import net.minecraft.world.level.biome.Biome;
39
import net.minecraft.world.level.biome.BiomeGenerationSettings;
40
import net.minecraft.world.level.biome.BiomeResolver;
41
import net.minecraft.world.level.block.Block;
42
import net.minecraft.world.level.block.entity.BlockEntity;
43
import net.minecraft.world.level.block.state.BlockState;
44
import net.minecraft.world.level.blockscan.BlockScanUtils;
45
import net.minecraft.world.level.blockscan.BlockStateConsumer;
46
import net.minecraft.world.level.chunk.status.ChunkStatus;
47
import net.minecraft.world.level.gameevent.GameEventListenerRegistry;
48
import net.minecraft.world.level.levelgen.BelowZeroRetrogen;
49
import net.minecraft.world.level.levelgen.Heightmap;
50
import net.minecraft.world.level.levelgen.blending.BlendingData;
51
import net.minecraft.world.level.levelgen.structure.Structure;
52
import net.minecraft.world.level.levelgen.structure.StructureStart;
53
import net.minecraft.world.level.lighting.ChunkSkyLightSources;
54
import net.minecraft.world.level.material.Fluid;
55
import net.minecraft.world.ticks.SavedTick;
56
import net.minecraft.world.ticks.TickContainerAccess;
57
import org.jspecify.annotations.Nullable;
58
import org.slf4j.Logger;
59
60
public abstract class ChunkAccess implements LightChunk, StructureAccess, BiomeResolver, AttachmentTarget {
61
	public static final int NO_FILLED_SECTION = -1;
62
	private static final Logger LOGGER = LogUtils.getLogger();
63
	private static final LongSet EMPTY_REFERENCE_SET = new LongOpenHashSet();
64
	protected final @Nullable ShortList[] postProcessing;
65
	private volatile boolean unsaved;
66
	private volatile boolean isLightCorrect;
67
	protected final ChunkPos chunkPos;
68
	private long inhabitedTime;
69
	@Deprecated
70
	private @Nullable BiomeGenerationSettings carverBiomeSettings;
71
	protected final UpgradeData upgradeData;
72
	protected final @Nullable BlendingData blendingData;
73
	protected final Map<Heightmap.Types, Heightmap> heightmaps = Maps.newEnumMap(Heightmap.Types.class);
74
	protected ChunkSkyLightSources skyLightSources;
75
	private final Map<Structure, StructureStart> structureStarts = Maps.newHashMap();
76
	private final Map<Structure, LongSet> structureReferences = Maps.newHashMap();
77
	protected final Map<BlockPos, CompoundTag> pendingBlockEntities = Maps.newHashMap();
78
	protected final Map<BlockPos, BlockEntity> blockEntities = new Object2ObjectOpenHashMap<>();
79
	protected final LevelHeightAccessor levelHeightAccessor;
80
	protected final LevelChunkSection[] sections;
81
82
	public ChunkAccess(
83
		final ChunkPos chunkPos,
84
		final UpgradeData upgradeData,
85
		final LevelHeightAccessor levelHeightAccessor,
86
		final PalettedContainerFactory containerFactory,
87
		final long inhabitedTime,
88
		final LevelChunkSection @Nullable [] sections,
89
		final @Nullable BlendingData blendingData
90
	) {
91
		this.chunkPos = chunkPos;
92
		this.upgradeData = upgradeData;
93
		this.levelHeightAccessor = levelHeightAccessor;
94
		this.sections = new LevelChunkSection[levelHeightAccessor.getSectionsCount()];
95
		this.inhabitedTime = inhabitedTime;
96
		this.postProcessing = new ShortList[levelHeightAccessor.getSectionsCount()];
97
		this.blendingData = blendingData;
98
		this.skyLightSources = new ChunkSkyLightSources(levelHeightAccessor);
99
		if (sections != null) {
100
			if (this.sections.length == sections.length) {
101
				System.arraycopy(sections, 0, this.sections, 0, this.sections.length);
102
			} else {
103
				LOGGER.warn("Could not set level chunk sections, array length is {} instead of {}", sections.length, this.sections.length);
104
			}
105
		}
106
107
		replaceMissingSections(containerFactory, this.sections);
108
	}
109
110
	private static void replaceMissingSections(final PalettedContainerFactory containerFactory, final LevelChunkSection[] sections) {
111
		for (int i = 0; i < sections.length; i++) {
112
			if (sections[i] == null) {
113
				sections[i] = new LevelChunkSection(containerFactory);
114
			}
115
		}
116
	}
117
118
	public GameEventListenerRegistry getListenerRegistry(final int section) {
119
		return GameEventListenerRegistry.NOOP;
120
	}
121
122
	public @Nullable BlockState setBlockState(final BlockPos pos, final BlockState state) {
123
		return this.setBlockState(pos, state, 3);
124
	}
125
126
	public abstract @Nullable BlockState setBlockState(BlockPos pos, BlockState state, @Block.UpdateFlags int flags);
127
128
	public abstract void setBlockEntity(BlockEntity blockEntity);
129
130
	public abstract void addEntity(Entity entity);
131
132
	public int getHighestFilledSectionIndex() {
133
		LevelChunkSection[] sections = this.getSections();
134
135
		for (int sectionIndex = sections.length - 1; sectionIndex >= 0; sectionIndex--) {
136
			LevelChunkSection section = sections[sectionIndex];
137
			if (!section.hasOnlyAir()) {
138
				return sectionIndex;
139
			}
140
		}
141
142
		return -1;
143
	}
144
145
	@Deprecated(forRemoval = true)
146
	public int getHighestSectionPosition() {
147
		int sectionIndex = this.getHighestFilledSectionIndex();
148
		return sectionIndex == -1 ? this.getMinY() : SectionPos.sectionToBlockCoord(this.getSectionYFromSectionIndex(sectionIndex));
149
	}
150
151
	public Set<BlockPos> getBlockEntitiesPos() {
152
		Set<BlockPos> result = Sets.newHashSet(this.pendingBlockEntities.keySet());
153
		result.addAll(this.blockEntities.keySet());
154
		return result;
155
	}
156
157
	public LevelChunkSection[] getSections() {
158
		return this.sections;
159
	}
160
161
	public LevelChunkSection getSection(final int sectionIndex) {
162
		return this.getSections()[sectionIndex];
163
	}
164
165
	public Collection<Entry<Heightmap.Types, Heightmap>> getHeightmaps() {
166
		return Collections.unmodifiableSet(this.heightmaps.entrySet());
167
	}
168
169
	public void setHeightmap(final Heightmap.Types key, final long[] data) {
170
		this.getOrCreateHeightmapUnprimed(key).setRawData(this, key, data);
171
	}
172
173
	public Heightmap getOrCreateHeightmapUnprimed(final Heightmap.Types type) {
174
		return this.heightmaps.computeIfAbsent(type, k -> new Heightmap(this, k));
175
	}
176
177
	public boolean hasPrimedHeightmap(final Heightmap.Types type) {
178
		return this.heightmaps.get(type) != null;
179
	}
180
181
	public int getHeight(final Heightmap.Types type, final int x, final int z) {
182
		Heightmap heightmap = this.heightmaps.get(type);
183
		if (heightmap == null) {
184
			if (SharedConstants.IS_RUNNING_IN_IDE && this instanceof LevelChunk) {
185
				LOGGER.error("Unprimed heightmap: {} {} {}", type, x, z);
186
			}
187
188
			Heightmap.primeHeightmaps(this, EnumSet.of(type));
189
			heightmap = this.heightmaps.get(type);
190
		}
191
192
		return heightmap.getFirstAvailable(x & 15, z & 15) - 1;
193
	}
194
195
	public ChunkPos getPos() {
196
		return this.chunkPos;
197
	}
198
199
	@Override
200
	public @Nullable StructureStart getStartForStructure(final Structure structure) {
201
		return this.structureStarts.get(structure);
202
	}
203
204
	@Override
205
	public void setStartForStructure(final Structure structure, final StructureStart structureStart) {
206
		this.structureStarts.put(structure, structureStart);
207
		this.markUnsaved();
208
	}
209
210
	public Map<Structure, StructureStart> getAllStarts() {
211
		return Collections.unmodifiableMap(this.structureStarts);
212
	}
213
214
	public void setAllStarts(final Map<Structure, StructureStart> starts) {
215
		this.structureStarts.clear();
216
		this.structureStarts.putAll(starts);
217
		this.markUnsaved();
218
	}
219
220
	@Override
221
	public LongSet getReferencesForStructure(final Structure structure) {
222
		return this.structureReferences.getOrDefault(structure, EMPTY_REFERENCE_SET);
223
	}
224
225
	@Override
226
	public void addReferenceForStructure(final Structure structure, final long reference) {
227
		this.structureReferences.computeIfAbsent(structure, k -> new LongOpenHashSet()).add(reference);
228
		this.markUnsaved();
229
	}
230
231
	@Override
232
	public Map<Structure, LongSet> getAllReferences() {
233
		return Collections.unmodifiableMap(this.structureReferences);
234
	}
235
236
	@Override
237
	public void setAllReferences(final Map<Structure, LongSet> data) {
238
		this.structureReferences.clear();
239
		this.structureReferences.putAll(data);
240
		this.markUnsaved();
241
	}
242
243
	public boolean isYSpaceEmpty(int yStartInclusive, int yEndInclusive) {
244
		if (yStartInclusive < this.getMinY()) {
245
			yStartInclusive = this.getMinY();
246
		}
247
248
		if (yEndInclusive > this.getMaxY()) {
249
			yEndInclusive = this.getMaxY();
250
		}
251
252
		for (int y = yStartInclusive; y <= yEndInclusive; y += 16) {
253
			if (!this.getSection(this.getSectionIndex(y)).hasOnlyAir()) {
254
				return false;
255
			}
256
		}
257
258
		return true;
259
	}
260
261
	public void markUnsaved() {
262
		this.unsaved = true;
263
	}
264
265
	public boolean tryMarkSaved() {
266
		if (this.unsaved) {
267
			this.unsaved = false;
268
			return true;
269
		} else {
270
			return false;
271
		}
272
	}
273
274
	public boolean isUnsaved() {
275
		return this.unsaved;
276
	}
277
278
	public abstract ChunkStatus getPersistedStatus();
279
280
	public ChunkStatus getHighestGeneratedStatus() {
281
		ChunkStatus status = this.getPersistedStatus();
282
		BelowZeroRetrogen belowZeroRetrogen = this.getBelowZeroRetrogen();
283
		if (belowZeroRetrogen != null) {
284
			ChunkStatus targetStatus = belowZeroRetrogen.targetStatus();
285
			return ChunkStatus.max(targetStatus, status);
286
		} else {
287
			return status;
288
		}
289
	}
290
291
	public abstract void removeBlockEntity(BlockPos pos);
292
293
	public void markPosForPostProcessing(final BlockPos blockPos) {
294
		LOGGER.warn("Trying to mark a block for post processing @ {}, but this operation is not supported.", blockPos);
295
	}
296
297
	public @Nullable ShortList[] getPostProcessing() {
298
		return this.postProcessing;
299
	}
300
301
	public void addPackedPostProcess(final ShortList packedOffsets, final int sectionIndex) {
302
		getOrCreateOffsetList(this.getPostProcessing(), sectionIndex).addAll(packedOffsets);
303
	}
304
305
	public void setBlockEntityNbt(final CompoundTag entityTag) {
306
		BlockPos posFromTag = BlockEntity.getPosFromTag(this.chunkPos, entityTag);
307
		if (!this.blockEntities.containsKey(posFromTag)) {
308
			this.pendingBlockEntities.put(posFromTag, entityTag);
309
		}
310
	}
311
312
	public @Nullable CompoundTag getBlockEntityNbt(final BlockPos blockPos) {
313
		return this.pendingBlockEntities.get(blockPos);
314
	}
315
316
	public abstract @Nullable CompoundTag getBlockEntityNbtForSaving(BlockPos blockPos, HolderLookup.Provider registryAccess);
317
318
	@Override
319
	public final void findBlockLightSources(final BiConsumer<BlockPos, BlockState> consumer) {
320
		this.findBlocks(state -> state.getLightEmission() != 0, consumer);
321
	}
322
323
	public void findBlocks(final Predicate<BlockState> predicate, final BiConsumer<BlockPos, BlockState> consumer) {
324
		BlockStateConsumer blockStateConsumer = (pos, state) -> {
325
			consumer.accept(pos, state);
326
			return Continuation.CONTINUE;
327
		};
328
		int max = 15;
329
330
		for (int sectionY = this.getMinSectionY(); sectionY <= this.getMaxSectionY(); sectionY++) {
331
			LevelChunkSection section = this.getSection(this.getSectionIndexFromSectionY(sectionY));
332
			if (section.maybeHas(predicate)) {
333
				BlockPos origin = SectionPos.of(this.chunkPos, sectionY).origin();
334
				BlockScanUtils.findBlocksInSection(section, origin, 0, 0, 0, 15, 15, 15, predicate, blockStateConsumer);
335
			}
336
		}
337
	}
338
339
	public abstract TickContainerAccess<Block> getBlockTicks();
340
341
	public abstract TickContainerAccess<Fluid> getFluidTicks();
342
343
	public void collectBiomesInPalette(final Set<Holder<Biome>> output) {
344
		for (LevelChunkSection section : this.sections) {
345
			section.getBiomes().forEachInPalette(output::add);
346
		}
347
	}
348
349
	public boolean canBeSerialized() {
350
		return true;
351
	}
352
353
	public abstract ChunkAccess.PackedTicks getTicksForSerialization(long currentTick);
354
355
	public UpgradeData getUpgradeData() {
356
		return this.upgradeData;
357
	}
358
359
	public boolean isOldNoiseGeneration() {
360
		return this.blendingData != null;
361
	}
362
363
	public @Nullable BlendingData getBlendingData() {
364
		return this.blendingData;
365
	}
366
367
	public long getInhabitedTime() {
368
		return this.inhabitedTime;
369
	}
370
371
	public void incrementInhabitedTime() {
372
		this.inhabitedTime++;
373
	}
374
375
	public void setInhabitedTime(final long inhabitedTime) {
376
		this.inhabitedTime = inhabitedTime;
377
	}
378
379
	public static ShortList getOrCreateOffsetList(final @Nullable ShortList[] list, final int sectionIndex) {
380
		ShortList result = list[sectionIndex];
381
		if (result == null) {
382
			result = new ShortArrayList();
383
			list[sectionIndex] = result;
384
		}
385
386
		return result;
387
	}
388
389
	public boolean isLightCorrect() {
390
		return this.isLightCorrect;
391
	}
392
393
	public void setLightCorrect(final boolean isLightCorrect) {
394
		this.isLightCorrect = isLightCorrect;
395
		this.markUnsaved();
396
	}
397
398
	@Override
399
	public int getMinY() {
400
		return this.levelHeightAccessor.getMinY();
401
	}
402
403
	@Override
404
	public int getHeight() {
405
		return this.levelHeightAccessor.getHeight();
406
	}
407
408
	@Deprecated
409
	public BiomeGenerationSettings carverBiome(final Supplier<BiomeGenerationSettings> source) {
410
		if (this.carverBiomeSettings == null) {
411
			this.carverBiomeSettings = source.get();
412
		}
413
414
		return this.carverBiomeSettings;
415
	}
416
417
	@Override
418
	public Holder<Biome> getNoiseBiome(final int quartX, final int quartY, final int quartZ) {
419
		try {
420
			int quartMinY = QuartPos.fromBlock(this.getMinY());
421
			int quartMaxY = quartMinY + QuartPos.fromBlock(this.getHeight()) - 1;
422
			int clampedQuartY = Mth.clamp(quartY, quartMinY, quartMaxY);
423
			int sectionIndex = this.getSectionIndex(QuartPos.toBlock(clampedQuartY));
424
			return this.sections[sectionIndex].getNoiseBiome(quartX & 3, clampedQuartY & 3, quartZ & 3);
425
		} catch (Throwable t) {
426
			CrashReport report = CrashReport.forThrowable(t, "Getting biome");
427
			CrashReportCategory category = report.addCategory("Biome being got");
428
			category.setDetail("Location", () -> CrashReportCategory.formatLocation(this, quartX, quartY, quartZ));
429
			throw new ReportedException(report);
430
		}
431
	}
432
433
	public void fillBiomesFromNoise(final BiomeResolver biomeResolver) {
434
		ChunkPos pos = this.getPos();
435
		int quartMinX = QuartPos.fromBlock(pos.getMinBlockX());
436
		int quartMinZ = QuartPos.fromBlock(pos.getMinBlockZ());
437
		LevelHeightAccessor heightAccessor = this.getHeightAccessorForGeneration();
438
439
		for (int sectionY = heightAccessor.getMinSectionY(); sectionY <= heightAccessor.getMaxSectionY(); sectionY++) {
440
			LevelChunkSection section = this.getSection(this.getSectionIndexFromSectionY(sectionY));
441
			int quartMinY = QuartPos.fromSection(sectionY);
442
			section.fillBiomesFromNoise(biomeResolver, quartMinX, quartMinY, quartMinZ);
443
		}
444
	}
445
446
	public @Nullable BelowZeroRetrogen getBelowZeroRetrogen() {
447
		return null;
448
	}
449
450
	public boolean isUpgrading() {
451
		return this.getBelowZeroRetrogen() != null;
452
	}
453
454
	public LevelHeightAccessor getHeightAccessorForGeneration() {
455
		return this;
456
	}
457
458
	public void initializeLightSources() {
459
		this.skyLightSources.fillFrom(this);
460
	}
461
462
	@Override
463
	public ChunkSkyLightSources getSkyLightSources() {
464
		return this.skyLightSources;
465
	}
466
467
	public static ProblemReporter.PathElement problemPath(final ChunkPos pos) {
468
		return new ChunkAccess.ChunkPathElement(pos);
469
	}
470
471
	public ProblemReporter.PathElement problemPath() {
472
		return problemPath(this.getPos());
473
	}
474
475
	private record ChunkPathElement(ChunkPos pos) implements ProblemReporter.PathElement {
476
		@Override
477
		public String get() {
478
			return "chunk@" + this.pos;
479
		}
480
	}
481
482
	public record PackedTicks(List<SavedTick<Block>> blocks, List<SavedTick<Fluid>> fluids) {
483
	}
484
}
485

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