| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import it.unimi.dsi.fastutil.shorts.ShortList;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.EnumSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.HolderLookup;
|
| 13 | import net.minecraft.core.SectionPos;
|
| 14 | import net.minecraft.nbt.CompoundTag;
|
| 15 | import net.minecraft.util.ProblemReporter;
|
| 16 | import net.minecraft.world.entity.Entity;
|
| 17 | import net.minecraft.world.level.ChunkPos;
|
| 18 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 19 | import net.minecraft.world.level.biome.Biome;
|
| 20 | import net.minecraft.world.level.block.Block;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 23 | import net.minecraft.world.level.block.state.BlockState;
|
| 24 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 25 | import net.minecraft.world.level.levelgen.BelowZeroRetrogen;
|
| 26 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 27 | import net.minecraft.world.level.levelgen.blending.BlendingData;
|
| 28 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 29 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 30 | import net.minecraft.world.level.levelgen.structure.StructureStart;
|
| 31 | import net.minecraft.world.level.lighting.LevelLightEngine;
|
| 32 | import net.minecraft.world.level.lighting.LightEngine;
|
| 33 | import net.minecraft.world.level.material.Fluid;
|
| 34 | import net.minecraft.world.level.material.FluidState;
|
| 35 | import net.minecraft.world.level.material.Fluids;
|
| 36 | import net.minecraft.world.level.storage.TagValueOutput;
|
| 37 | import net.minecraft.world.ticks.LevelChunkTicks;
|
| 38 | import net.minecraft.world.ticks.ProtoChunkTicks;
|
| 39 | import net.minecraft.world.ticks.TickContainerAccess;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 | import org.slf4j.Logger;
|
| 42 |
|
| 43 | public class ProtoChunk extends ChunkAccess {
|
| 44 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 45 | private volatile @Nullable LevelLightEngine lightEngine;
|
| 46 | private volatile ChunkStatus status = ChunkStatus.EMPTY;
|
| 47 | private final List<CompoundTag> entities = Lists.newArrayList();
|
| 48 | private @Nullable BelowZeroRetrogen belowZeroRetrogen;
|
| 49 | private final ProtoChunkTicks<Block> blockTicks;
|
| 50 | private final ProtoChunkTicks<Fluid> fluidTicks;
|
| 51 |
|
| 52 | public ProtoChunk(
|
| 53 | final ChunkPos chunkPos,
|
| 54 | final UpgradeData upgradeData,
|
| 55 | final LevelHeightAccessor levelHeightAccessor,
|
| 56 | final PalettedContainerFactory containerFactory,
|
| 57 | final @Nullable BlendingData blendingData
|
| 58 | ) {
|
| 59 | this(chunkPos, upgradeData, null, new ProtoChunkTicks<>(), new ProtoChunkTicks<>(), levelHeightAccessor, containerFactory, blendingData);
|
| 60 | }
|
| 61 |
|
| 62 | public ProtoChunk(
|
| 63 | final ChunkPos chunkPos,
|
| 64 | final UpgradeData upgradeData,
|
| 65 | final LevelChunkSection @Nullable [] sections,
|
| 66 | final ProtoChunkTicks<Block> blockTicks,
|
| 67 | final ProtoChunkTicks<Fluid> fluidTicks,
|
| 68 | final LevelHeightAccessor levelHeightAccessor,
|
| 69 | final PalettedContainerFactory containerFactory,
|
| 70 | final @Nullable BlendingData blendingData
|
| 71 | ) {
|
| 72 | super(chunkPos, upgradeData, levelHeightAccessor, containerFactory, 0L, sections, blendingData);
|
| 73 | this.blockTicks = blockTicks;
|
| 74 | this.fluidTicks = fluidTicks;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public TickContainerAccess<Block> getBlockTicks() {
|
| 79 | return this.blockTicks;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public TickContainerAccess<Fluid> getFluidTicks() {
|
| 84 | return this.fluidTicks;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public ChunkAccess.PackedTicks getTicksForSerialization(final long currentTick) {
|
| 89 | return new ChunkAccess.PackedTicks(this.blockTicks.pack(currentTick), this.fluidTicks.pack(currentTick));
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public BlockState getBlockState(final BlockPos pos) {
|
| 94 | int y = pos.getY();
|
| 95 | if (this.isOutsideBuildHeight(y)) {
|
| 96 | return Blocks.VOID_AIR.defaultBlockState();
|
| 97 | }
|
| 98 |
|
| 99 | LevelChunkSection section = this.getSection(this.getSectionIndex(y));
|
| 100 | return section.hasOnlyAir() ? Blocks.AIR.defaultBlockState() : section.getBlockState(pos.getX() & 15, y & 15, pos.getZ() & 15);
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public FluidState getFluidState(final BlockPos pos) {
|
| 105 | int y = pos.getY();
|
| 106 | if (this.isOutsideBuildHeight(y)) {
|
| 107 | return Fluids.EMPTY.defaultFluidState();
|
| 108 | }
|
| 109 |
|
| 110 | LevelChunkSection section = this.getSection(this.getSectionIndex(y));
|
| 111 | return section.hasOnlyAir() ? Fluids.EMPTY.defaultFluidState() : section.getFluidState(pos.getX() & 15, y & 15, pos.getZ() & 15);
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public @Nullable BlockState setBlockState(final BlockPos pos, final BlockState state, final @Block.UpdateFlags int flags) {
|
| 116 | int x = pos.getX();
|
| 117 | int y = pos.getY();
|
| 118 | int z = pos.getZ();
|
| 119 | if (this.isOutsideBuildHeight(y)) {
|
| 120 | return Blocks.VOID_AIR.defaultBlockState();
|
| 121 | }
|
| 122 |
|
| 123 | int sectionIndex = this.getSectionIndex(y);
|
| 124 | LevelChunkSection section = this.getSection(sectionIndex);
|
| 125 | boolean wasEmpty = section.hasOnlyAir();
|
| 126 | if (wasEmpty && state.is(Blocks.AIR)) {
|
| 127 | return state;
|
| 128 | }
|
| 129 |
|
| 130 | int localX = SectionPos.sectionRelative(x);
|
| 131 | int localY = SectionPos.sectionRelative(y);
|
| 132 | int localZ = SectionPos.sectionRelative(z);
|
| 133 | BlockState oldState = section.setBlockState(localX, localY, localZ, state);
|
| 134 | if (this.status.isOrAfter(ChunkStatus.INITIALIZE_LIGHT)) {
|
| 135 | boolean isEmpty = section.hasOnlyAir();
|
| 136 | if (isEmpty != wasEmpty) {
|
| 137 | this.lightEngine.updateSectionStatus(pos, isEmpty);
|
| 138 | }
|
| 139 |
|
| 140 | if (LightEngine.hasDifferentLightProperties(oldState, state)) {
|
| 141 | this.skyLightSources.update(this, localX, y, localZ);
|
| 142 | this.lightEngine.checkBlock(pos);
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | EnumSet<Heightmap.Types> heightmapsAfter = this.getPersistedStatus().heightmapsAfter();
|
| 147 | EnumSet<Heightmap.Types> toPrime = null;
|
| 148 |
|
| 149 | for (Heightmap.Types type : heightmapsAfter) {
|
| 150 | Heightmap heightmap = this.heightmaps.get(type);
|
| 151 | if (heightmap == null) {
|
| 152 | if (toPrime == null) {
|
| 153 | toPrime = EnumSet.noneOf(Heightmap.Types.class);
|
| 154 | }
|
| 155 |
|
| 156 | toPrime.add(type);
|
| 157 | } else {
|
| 158 | heightmap.update(localX, y, localZ, state);
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | if (toPrime != null) {
|
| 163 | Heightmap.primeHeightmaps(this, toPrime);
|
| 164 | }
|
| 165 |
|
| 166 | return oldState;
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public void setBlockEntity(final BlockEntity blockEntity) {
|
| 171 | this.pendingBlockEntities.remove(blockEntity.getBlockPos());
|
| 172 | this.blockEntities.put(blockEntity.getBlockPos(), blockEntity);
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public @Nullable BlockEntity getBlockEntity(final BlockPos pos) {
|
| 177 | return this.blockEntities.get(pos);
|
| 178 | }
|
| 179 |
|
| 180 | public Map<BlockPos, BlockEntity> getBlockEntities() {
|
| 181 | return this.blockEntities;
|
| 182 | }
|
| 183 |
|
| 184 | public void addEntity(final CompoundTag tag) {
|
| 185 | this.entities.add(tag);
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public void addEntity(final Entity entity) {
|
| 190 | if (!entity.isPassenger()) {
|
| 191 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(entity.problemPath(), LOGGER)) {
|
| 192 | TagValueOutput output = TagValueOutput.createWithContext(reporter, entity.registryAccess());
|
| 193 | entity.save(output);
|
| 194 | this.addEntity(output.buildResult());
|
| 195 | }
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public void setStartForStructure(final Structure structure, final StructureStart structureStart) {
|
| 201 | BelowZeroRetrogen belowZeroRetrogen = this.getBelowZeroRetrogen();
|
| 202 | if (belowZeroRetrogen != null && structureStart.isValid()) {
|
| 203 | BoundingBox boundingBox = structureStart.getBoundingBox();
|
| 204 | LevelHeightAccessor heightAccessor = this.getHeightAccessorForGeneration();
|
| 205 | if (boundingBox.minY() < heightAccessor.getMinY() || boundingBox.maxY() > heightAccessor.getMaxY()) {
|
| 206 | return;
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | super.setStartForStructure(structure, structureStart);
|
| 211 | }
|
| 212 |
|
| 213 | public List<CompoundTag> getEntities() {
|
| 214 | return this.entities;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public ChunkStatus getPersistedStatus() {
|
| 219 | return this.status;
|
| 220 | }
|
| 221 |
|
| 222 | public void setPersistedStatus(final ChunkStatus status) {
|
| 223 | this.status = status;
|
| 224 | if (this.belowZeroRetrogen != null && status.isOrAfter(this.belowZeroRetrogen.targetStatus())) {
|
| 225 | this.setBelowZeroRetrogen(null);
|
| 226 | }
|
| 227 |
|
| 228 | this.markUnsaved();
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | public Holder<Biome> getNoiseBiome(final int quartX, final int quartY, final int quartZ) {
|
| 233 | if (this.getHighestGeneratedStatus().isOrAfter(ChunkStatus.BIOMES)) {
|
| 234 | return super.getNoiseBiome(quartX, quartY, quartZ);
|
| 235 | } else {
|
| 236 | throw new IllegalStateException("Asking for biomes before we have biomes");
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | public static short packOffsetCoordinates(final BlockPos pos) {
|
| 241 | int x = pos.getX();
|
| 242 | int y = pos.getY();
|
| 243 | int z = pos.getZ();
|
| 244 | int dx = x & 15;
|
| 245 | int dy = y & 15;
|
| 246 | int dz = z & 15;
|
| 247 | return (short)(dx | dy << 4 | dz << 8);
|
| 248 | }
|
| 249 |
|
| 250 | public static BlockPos unpackOffsetCoordinates(final short packedData, final int sectionY, final ChunkPos chunkPos) {
|
| 251 | int posX = SectionPos.sectionToBlockCoord(chunkPos.x(), packedData & 15);
|
| 252 | int posY = SectionPos.sectionToBlockCoord(sectionY, packedData >>> 4 & 15);
|
| 253 | int posZ = SectionPos.sectionToBlockCoord(chunkPos.z(), packedData >>> 8 & 15);
|
| 254 | return new BlockPos(posX, posY, posZ);
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public void markPosForPostProcessing(final BlockPos blockPos) {
|
| 259 | if (this.isInsideBuildHeight(blockPos)) {
|
| 260 | ChunkAccess.getOrCreateOffsetList(this.postProcessing, this.getSectionIndex(blockPos.getY())).add(packOffsetCoordinates(blockPos));
|
| 261 | }
|
| 262 | }
|
| 263 |
|
| 264 | @Override
|
| 265 | public void addPackedPostProcess(final ShortList packedOffsets, final int sectionIndex) {
|
| 266 | ChunkAccess.getOrCreateOffsetList(this.postProcessing, sectionIndex).addAll(packedOffsets);
|
| 267 | }
|
| 268 |
|
| 269 | public Map<BlockPos, CompoundTag> getBlockEntityNbts() {
|
| 270 | return Collections.unmodifiableMap(this.pendingBlockEntities);
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public @Nullable CompoundTag getBlockEntityNbtForSaving(final BlockPos blockPos, final HolderLookup.Provider registryAccess) {
|
| 275 | BlockEntity blockEntity = this.getBlockEntity(blockPos);
|
| 276 | return blockEntity != null ? blockEntity.saveWithFullMetadata(registryAccess) : this.pendingBlockEntities.get(blockPos);
|
| 277 | }
|
| 278 |
|
| 279 | @Override
|
| 280 | public void removeBlockEntity(final BlockPos pos) {
|
| 281 | this.blockEntities.remove(pos);
|
| 282 | this.pendingBlockEntities.remove(pos);
|
| 283 | }
|
| 284 |
|
| 285 | public void setLightEngine(final LevelLightEngine lightEngine) {
|
| 286 | this.lightEngine = lightEngine;
|
| 287 | }
|
| 288 |
|
| 289 | public void setBelowZeroRetrogen(final @Nullable BelowZeroRetrogen belowZeroRetrogen) {
|
| 290 | this.belowZeroRetrogen = belowZeroRetrogen;
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | public @Nullable BelowZeroRetrogen getBelowZeroRetrogen() {
|
| 295 | return this.belowZeroRetrogen;
|
| 296 | }
|
| 297 |
|
| 298 | private static <T> LevelChunkTicks<T> unpackTicks(final ProtoChunkTicks<T> ticks) {
|
| 299 | return new LevelChunkTicks<>(ticks.scheduledTicks());
|
| 300 | }
|
| 301 |
|
| 302 | public LevelChunkTicks<Block> unpackBlockTicks() {
|
| 303 | return unpackTicks(this.blockTicks);
|
| 304 | }
|
| 305 |
|
| 306 | public LevelChunkTicks<Fluid> unpackFluidTicks() {
|
| 307 | return unpackTicks(this.fluidTicks);
|
| 308 | }
|
| 309 |
|
| 310 | @Override
|
| 311 | public LevelHeightAccessor getHeightAccessorForGeneration() {
|
| 312 | return this.isUpgrading() ? BelowZeroRetrogen.UPGRADE_HEIGHT_ACCESSOR : this;
|
| 313 | }
|
| 314 | }
|
| 315 |
|