| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 7 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 8 | import it.unimi.dsi.fastutil.shorts.ShortList;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.Collections;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Map.Entry;
|
| 14 | import java.util.function.Supplier;
|
| 15 | import net.minecraft.CrashReport;
|
| 16 | import net.minecraft.CrashReportCategory;
|
| 17 | import net.minecraft.ReportedException;
|
| 18 | import net.minecraft.core.BlockPos;
|
| 19 | import net.minecraft.core.HolderLookup;
|
| 20 | import net.minecraft.core.SectionPos;
|
| 21 | import net.minecraft.nbt.CompoundTag;
|
| 22 | import net.minecraft.network.FriendlyByteBuf;
|
| 23 | import net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData;
|
| 24 | import net.minecraft.server.level.FullChunkStatus;
|
| 25 | import net.minecraft.server.level.ServerLevel;
|
| 26 | import net.minecraft.util.ProblemReporter;
|
| 27 | import net.minecraft.util.debug.DebugStructureInfo;
|
| 28 | import net.minecraft.util.debug.DebugSubscriptions;
|
| 29 | import net.minecraft.util.debug.DebugValueSource;
|
| 30 | import net.minecraft.util.profiling.Profiler;
|
| 31 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 32 | import net.minecraft.world.entity.Entity;
|
| 33 | import net.minecraft.world.level.ChunkPos;
|
| 34 | import net.minecraft.world.level.Level;
|
| 35 | import net.minecraft.world.level.block.BaseRailBlock;
|
| 36 | import net.minecraft.world.level.block.Block;
|
| 37 | import net.minecraft.world.level.block.Blocks;
|
| 38 | import net.minecraft.world.level.block.EntityBlock;
|
| 39 | import net.minecraft.world.level.block.LiquidBlock;
|
| 40 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 41 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 42 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 43 | import net.minecraft.world.level.block.entity.TickingBlockEntity;
|
| 44 | import net.minecraft.world.level.block.state.BlockState;
|
| 45 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 46 | import net.minecraft.world.level.gameevent.EuclideanGameEventListenerRegistry;
|
| 47 | import net.minecraft.world.level.gameevent.GameEventListener;
|
| 48 | import net.minecraft.world.level.gameevent.GameEventListenerRegistry;
|
| 49 | import net.minecraft.world.level.levelgen.DebugLevelSource;
|
| 50 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 51 | import net.minecraft.world.level.levelgen.blending.BlendingData;
|
| 52 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 53 | import net.minecraft.world.level.levelgen.structure.StructurePiece;
|
| 54 | import net.minecraft.world.level.levelgen.structure.StructureStart;
|
| 55 | import net.minecraft.world.level.lighting.LightEngine;
|
| 56 | import net.minecraft.world.level.material.Fluid;
|
| 57 | import net.minecraft.world.level.material.FluidState;
|
| 58 | import net.minecraft.world.level.material.Fluids;
|
| 59 | import net.minecraft.world.level.storage.TagValueInput;
|
| 60 | import net.minecraft.world.ticks.LevelChunkTicks;
|
| 61 | import net.minecraft.world.ticks.TickContainerAccess;
|
| 62 | import org.jspecify.annotations.Nullable;
|
| 63 | import org.slf4j.Logger;
|
| 64 |
|
| 65 | public class LevelChunk extends ChunkAccess implements DebugValueSource {
|
| 66 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 67 | private static final TickingBlockEntity NULL_TICKER = new TickingBlockEntity() {
|
| 68 | @Override
|
| 69 | public void tick() {
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public boolean isRemoved() {
|
| 74 | return true;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public BlockPos getPos() {
|
| 79 | return BlockPos.ZERO;
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public String getType() {
|
| 84 | return "<null>";
|
| 85 | }
|
| 86 | };
|
| 87 | private final Map<BlockPos, LevelChunk.RebindableTickingBlockEntityWrapper> tickersInLevel = Maps.newHashMap();
|
| 88 | private boolean loaded;
|
| 89 | private final Level level;
|
| 90 | private @Nullable Supplier<FullChunkStatus> fullStatus;
|
| 91 | private LevelChunk.@Nullable PostLoadProcessor postLoad;
|
| 92 | private final Int2ObjectMap<GameEventListenerRegistry> gameEventListenerRegistrySections;
|
| 93 | private final LevelChunkTicks<Block> blockTicks;
|
| 94 | private final LevelChunkTicks<Fluid> fluidTicks;
|
| 95 | private LevelChunk.UnsavedListener unsavedListener = chunkPos -> {};
|
| 96 |
|
| 97 | public LevelChunk(final Level level, final ChunkPos pos) {
|
| 98 | this(level, pos, UpgradeData.EMPTY, new LevelChunkTicks<>(), new LevelChunkTicks<>(), 0L, null, null, null);
|
| 99 | }
|
| 100 |
|
| 101 | public LevelChunk(
|
| 102 | final Level level,
|
| 103 | final ChunkPos pos,
|
| 104 | final UpgradeData upgradeData,
|
| 105 | final LevelChunkTicks<Block> blockTicks,
|
| 106 | final LevelChunkTicks<Fluid> fluidTicks,
|
| 107 | final long inhabitedTime,
|
| 108 | final LevelChunkSection @Nullable [] sections,
|
| 109 | final LevelChunk.@Nullable PostLoadProcessor postLoad,
|
| 110 | final @Nullable BlendingData blendingData
|
| 111 | ) {
|
| 112 | super(pos, upgradeData, level, level.palettedContainerFactory(), inhabitedTime, sections, blendingData);
|
| 113 | this.level = level;
|
| 114 | this.gameEventListenerRegistrySections = new Int2ObjectOpenHashMap<>();
|
| 115 |
|
| 116 | for (Heightmap.Types type : Heightmap.Types.values()) {
|
| 117 | if (ChunkStatus.FULL.heightmapsAfter().contains(type)) {
|
| 118 | this.heightmaps.put(type, new Heightmap(this, type));
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | this.postLoad = postLoad;
|
| 123 | this.blockTicks = blockTicks;
|
| 124 | this.fluidTicks = fluidTicks;
|
| 125 | }
|
| 126 |
|
| 127 | public LevelChunk(final ServerLevel level, final ProtoChunk protoChunk, final LevelChunk.@Nullable PostLoadProcessor postLoad) {
|
| 128 | this(
|
| 129 | level,
|
| 130 | protoChunk.getPos(),
|
| 131 | protoChunk.getUpgradeData(),
|
| 132 | protoChunk.unpackBlockTicks(),
|
| 133 | protoChunk.unpackFluidTicks(),
|
| 134 | protoChunk.getInhabitedTime(),
|
| 135 | protoChunk.getSections(),
|
| 136 | postLoad,
|
| 137 | protoChunk.getBlendingData()
|
| 138 | );
|
| 139 | if (!Collections.disjoint(protoChunk.pendingBlockEntities.keySet(), protoChunk.blockEntities.keySet())) {
|
| 140 | LOGGER.error("Chunk at {} contains duplicated block entities", protoChunk.getPos());
|
| 141 | }
|
| 142 |
|
| 143 | for (BlockEntity blockEntity : protoChunk.getBlockEntities().values()) {
|
| 144 | this.setBlockEntity(blockEntity);
|
| 145 | }
|
| 146 |
|
| 147 | this.pendingBlockEntities.putAll(protoChunk.getBlockEntityNbts());
|
| 148 |
|
| 149 | for (int i = 0; i < protoChunk.getPostProcessing().length; i++) {
|
| 150 | this.postProcessing[i] = protoChunk.getPostProcessing()[i];
|
| 151 | }
|
| 152 |
|
| 153 | this.setAllStarts(protoChunk.getAllStarts());
|
| 154 | this.setAllReferences(protoChunk.getAllReferences());
|
| 155 |
|
| 156 | for (Entry<Heightmap.Types, Heightmap> entry : protoChunk.getHeightmaps()) {
|
| 157 | if (ChunkStatus.FULL.heightmapsAfter().contains(entry.getKey())) {
|
| 158 | this.setHeightmap(entry.getKey(), entry.getValue().getRawData());
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | this.skyLightSources = protoChunk.skyLightSources;
|
| 163 | this.setLightCorrect(protoChunk.isLightCorrect());
|
| 164 | this.markUnsaved();
|
| 165 | }
|
| 166 |
|
| 167 | public void setUnsavedListener(final LevelChunk.UnsavedListener unsavedListener) {
|
| 168 | this.unsavedListener = unsavedListener;
|
| 169 | if (this.isUnsaved()) {
|
| 170 | unsavedListener.setUnsaved(this.chunkPos);
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public void markUnsaved() {
|
| 176 | boolean wasUnsaved = this.isUnsaved();
|
| 177 | super.markUnsaved();
|
| 178 | if (!wasUnsaved) {
|
| 179 | this.unsavedListener.setUnsaved(this.chunkPos);
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public TickContainerAccess<Block> getBlockTicks() {
|
| 185 | return this.blockTicks;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public TickContainerAccess<Fluid> getFluidTicks() {
|
| 190 | return this.fluidTicks;
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public ChunkAccess.PackedTicks getTicksForSerialization(final long currentTick) {
|
| 195 | return new ChunkAccess.PackedTicks(this.blockTicks.pack(currentTick), this.fluidTicks.pack(currentTick));
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public GameEventListenerRegistry getListenerRegistry(final int section) {
|
| 200 | return this.level instanceof ServerLevel serverLevel
|
| 201 | ? this.gameEventListenerRegistrySections
|
| 202 | .computeIfAbsent(section, key -> new EuclideanGameEventListenerRegistry(serverLevel, section, this::removeGameEventListenerRegistry))
|
| 203 | : super.getListenerRegistry(section);
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public BlockState getBlockState(final BlockPos pos) {
|
| 208 | int x = pos.getX();
|
| 209 | int y = pos.getY();
|
| 210 | int z = pos.getZ();
|
| 211 | if (this.level.isDebug()) {
|
| 212 | BlockState blockState = null;
|
| 213 | if (y == 60) {
|
| 214 | blockState = Blocks.BARRIER.defaultBlockState();
|
| 215 | }
|
| 216 |
|
| 217 | if (y == 70) {
|
| 218 | blockState = DebugLevelSource.getBlockStateFor(x, z);
|
| 219 | }
|
| 220 |
|
| 221 | return blockState == null ? Blocks.AIR.defaultBlockState() : blockState;
|
| 222 | } else {
|
| 223 | try {
|
| 224 | int sectionIndex = this.getSectionIndex(y);
|
| 225 | if (sectionIndex >= 0 && sectionIndex < this.sections.length) {
|
| 226 | LevelChunkSection currentSection = this.sections[sectionIndex];
|
| 227 | if (!currentSection.hasOnlyAir()) {
|
| 228 | return currentSection.getBlockState(x & 15, y & 15, z & 15);
|
| 229 | }
|
| 230 | }
|
| 231 |
|
| 232 | return Blocks.AIR.defaultBlockState();
|
| 233 | } catch (Throwable t) {
|
| 234 | CrashReport report = CrashReport.forThrowable(t, "Getting block state");
|
| 235 | CrashReportCategory category = report.addCategory("Block being got");
|
| 236 | category.setDetail("Location", () -> CrashReportCategory.formatLocation(this, x, y, z));
|
| 237 | throw new ReportedException(report);
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 |
|
| 242 | @Override
|
| 243 | public FluidState getFluidState(final BlockPos pos) {
|
| 244 | return this.getFluidState(pos.getX(), pos.getY(), pos.getZ());
|
| 245 | }
|
| 246 |
|
| 247 | public FluidState getFluidState(final int x, final int y, final int z) {
|
| 248 | try {
|
| 249 | int sectionIndex = this.getSectionIndex(y);
|
| 250 | if (sectionIndex >= 0 && sectionIndex < this.sections.length) {
|
| 251 | LevelChunkSection currentSection = this.sections[sectionIndex];
|
| 252 | if (!currentSection.hasOnlyAir()) {
|
| 253 | return currentSection.getFluidState(x & 15, y & 15, z & 15);
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | return Fluids.EMPTY.defaultFluidState();
|
| 258 | } catch (Throwable t) {
|
| 259 | CrashReport report = CrashReport.forThrowable(t, "Getting fluid state");
|
| 260 | CrashReportCategory category = report.addCategory("Block being got");
|
| 261 | category.setDetail("Location", () -> CrashReportCategory.formatLocation(this, x, y, z));
|
| 262 | throw new ReportedException(report);
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | @Override
|
| 267 | public @Nullable BlockState setBlockState(final BlockPos pos, final BlockState state, final @Block.UpdateFlags int flags) {
|
| 268 | int y = pos.getY();
|
| 269 | LevelChunkSection section = this.getSection(this.getSectionIndex(y));
|
| 270 | boolean wasEmpty = section.hasOnlyAir();
|
| 271 | if (wasEmpty && state.isAir()) {
|
| 272 | return null;
|
| 273 | }
|
| 274 |
|
| 275 | int localX = pos.getX() & 15;
|
| 276 | int localY = y & 15;
|
| 277 | int localZ = pos.getZ() & 15;
|
| 278 | BlockState oldState = section.setBlockState(localX, localY, localZ, state);
|
| 279 | if (oldState == state) {
|
| 280 | return null;
|
| 281 | }
|
| 282 |
|
| 283 | Block newBlock = state.getBlock();
|
| 284 | this.heightmaps.get(Heightmap.Types.MOTION_BLOCKING).update(localX, y, localZ, state);
|
| 285 | this.heightmaps.get(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES).update(localX, y, localZ, state);
|
| 286 | this.heightmaps.get(Heightmap.Types.OCEAN_FLOOR).update(localX, y, localZ, state);
|
| 287 | this.heightmaps.get(Heightmap.Types.WORLD_SURFACE).update(localX, y, localZ, state);
|
| 288 | boolean isEmpty = section.hasOnlyAir();
|
| 289 | if (wasEmpty != isEmpty) {
|
| 290 | this.level.getChunkSource().getLightEngine().updateSectionStatus(pos, isEmpty);
|
| 291 | this.level.getChunkSource().onSectionEmptinessChanged(this.chunkPos.x(), SectionPos.blockToSectionCoord(y), this.chunkPos.z(), isEmpty);
|
| 292 | }
|
| 293 |
|
| 294 | if (LightEngine.hasDifferentLightProperties(oldState, state)) {
|
| 295 | ProfilerFiller profiler = Profiler.get();
|
| 296 | profiler.push("updateSkyLightSources");
|
| 297 | this.skyLightSources.update(this, localX, y, localZ);
|
| 298 | profiler.popPush("queueCheckLight");
|
| 299 | this.level.getChunkSource().getLightEngine().checkBlock(pos);
|
| 300 | profiler.pop();
|
| 301 | }
|
| 302 |
|
| 303 | boolean blockChanged = !oldState.is(newBlock);
|
| 304 | boolean movedByPiston = (flags & 64) != 0;
|
| 305 | boolean sideEffects = (flags & 256) == 0;
|
| 306 | if (blockChanged && oldState.hasBlockEntity() && !state.shouldChangedStateKeepBlockEntity(oldState)) {
|
| 307 | if (!this.level.isClientSide() && sideEffects) {
|
| 308 | BlockEntity blockEntity = this.level.getBlockEntity(pos);
|
| 309 | if (blockEntity != null) {
|
| 310 | blockEntity.preRemoveSideEffects(pos, oldState);
|
| 311 | }
|
| 312 | }
|
| 313 |
|
| 314 | this.removeBlockEntity(pos);
|
| 315 | }
|
| 316 |
|
| 317 | if ((blockChanged || newBlock instanceof BaseRailBlock) && this.level instanceof ServerLevel serverLevel && ((flags & 1) != 0 || movedByPiston)) {
|
| 318 | oldState.affectNeighborsAfterRemoval(serverLevel, pos, movedByPiston);
|
| 319 | }
|
| 320 |
|
| 321 | if (!section.getBlockState(localX, localY, localZ).is(newBlock)) {
|
| 322 | return null;
|
| 323 | }
|
| 324 |
|
| 325 | if (!this.level.isClientSide() && (flags & 512) == 0) {
|
| 326 | state.onPlace(this.level, pos, oldState, movedByPiston);
|
| 327 | }
|
| 328 |
|
| 329 | if (state.hasBlockEntity() && section.getBlockState(localX, localY, localZ).is(newBlock)) {
|
| 330 | BlockEntity blockEntity = this.getBlockEntity(pos, LevelChunk.EntityCreationType.CHECK);
|
| 331 | if (blockEntity != null && !blockEntity.isValidBlockState(state)) {
|
| 332 | LOGGER.warn("Found mismatched block entity @ {}: type = {}, state = {}", pos, blockEntity.typeHolder().getRegisteredName(), state);
|
| 333 | this.removeBlockEntity(pos);
|
| 334 | blockEntity = null;
|
| 335 | }
|
| 336 |
|
| 337 | if (blockEntity == null) {
|
| 338 | blockEntity = ((EntityBlock)newBlock).newBlockEntity(pos, state);
|
| 339 | if (blockEntity != null) {
|
| 340 | this.addAndRegisterBlockEntity(blockEntity);
|
| 341 | }
|
| 342 | } else {
|
| 343 | blockEntity.setBlockState(state);
|
| 344 | this.updateBlockEntityTicker(blockEntity);
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | this.markUnsaved();
|
| 349 | return oldState;
|
| 350 | }
|
| 351 |
|
| 352 | @Deprecated
|
| 353 | @Override
|
| 354 | public void addEntity(final Entity entity) {
|
| 355 | }
|
| 356 |
|
| 357 | private @Nullable BlockEntity createBlockEntity(final BlockPos pos) {
|
| 358 | BlockState state = this.getBlockState(pos);
|
| 359 | return !state.hasBlockEntity() ? null : ((EntityBlock)state.getBlock()).newBlockEntity(pos, state);
|
| 360 | }
|
| 361 |
|
| 362 | @Override
|
| 363 | public @Nullable BlockEntity getBlockEntity(final BlockPos pos) {
|
| 364 | return this.getBlockEntity(pos, LevelChunk.EntityCreationType.CHECK);
|
| 365 | }
|
| 366 |
|
| 367 | public @Nullable BlockEntity getBlockEntity(final BlockPos pos, final LevelChunk.EntityCreationType creationType) {
|
| 368 | BlockEntity blockEntity = this.blockEntities.get(pos);
|
| 369 | if (blockEntity == null) {
|
| 370 | CompoundTag tag = this.pendingBlockEntities.remove(pos);
|
| 371 | if (tag != null) {
|
| 372 | BlockEntity promoted = this.promotePendingBlockEntity(pos, tag);
|
| 373 | if (promoted != null) {
|
| 374 | return promoted;
|
| 375 | }
|
| 376 | }
|
| 377 | }
|
| 378 |
|
| 379 | if (blockEntity == null) {
|
| 380 | if (creationType == LevelChunk.EntityCreationType.IMMEDIATE) {
|
| 381 | blockEntity = this.createBlockEntity(pos);
|
| 382 | if (blockEntity != null) {
|
| 383 | this.addAndRegisterBlockEntity(blockEntity);
|
| 384 | }
|
| 385 | }
|
| 386 | } else if (blockEntity.isRemoved()) {
|
| 387 | this.blockEntities.remove(pos);
|
| 388 | return null;
|
| 389 | }
|
| 390 |
|
| 391 | return blockEntity;
|
| 392 | }
|
| 393 |
|
| 394 | public void addAndRegisterBlockEntity(final BlockEntity blockEntity) {
|
| 395 | this.setBlockEntity(blockEntity);
|
| 396 | if (this.isInLevel()) {
|
| 397 | if (this.level instanceof ServerLevel serverLevel) {
|
| 398 | this.addGameEventListener(blockEntity, serverLevel);
|
| 399 | }
|
| 400 |
|
| 401 | this.level.onBlockEntityAdded(blockEntity);
|
| 402 | this.updateBlockEntityTicker(blockEntity);
|
| 403 | }
|
| 404 | }
|
| 405 |
|
| 406 | private boolean isInLevel() {
|
| 407 | return this.loaded || this.level.isClientSide();
|
| 408 | }
|
| 409 |
|
| 410 | private boolean isTicking(final BlockPos pos) {
|
| 411 | if (!this.level.getWorldBorder().isWithinBounds(pos)) {
|
| 412 | return false;
|
| 413 | } else {
|
| 414 | return !(this.level instanceof ServerLevel serverLevel)
|
| 415 | ? true
|
| 416 | : this.getFullStatus().isOrAfter(FullChunkStatus.BLOCK_TICKING) && serverLevel.areEntitiesLoaded(ChunkPos.pack(pos));
|
| 417 | }
|
| 418 | }
|
| 419 |
|
| 420 | @Override
|
| 421 | public void setBlockEntity(final BlockEntity blockEntity) {
|
| 422 | BlockPos pos = blockEntity.getBlockPos();
|
| 423 | BlockState blockState = this.getBlockState(pos);
|
| 424 | if (!blockState.hasBlockEntity()) {
|
| 425 | LOGGER.warn("Trying to set block entity {} at position {}, but state {} does not allow it", blockEntity, pos, blockState);
|
| 426 | } else {
|
| 427 | BlockState cachedBlockState = blockEntity.getBlockState();
|
| 428 | if (blockState != cachedBlockState) {
|
| 429 | if (!blockEntity.getType().isValid(blockState)) {
|
| 430 | LOGGER.warn("Trying to set block entity {} at position {}, but state {} does not allow it", blockEntity, pos, blockState);
|
| 431 | return;
|
| 432 | }
|
| 433 |
|
| 434 | if (blockState.getBlock() != cachedBlockState.getBlock()) {
|
| 435 | LOGGER.warn("Block state mismatch on block entity {} in position {}, {} != {}, updating", blockEntity, pos, blockState, cachedBlockState);
|
| 436 | }
|
| 437 |
|
| 438 | blockEntity.setBlockState(blockState);
|
| 439 | }
|
| 440 |
|
| 441 | blockEntity.setLevel(this.level);
|
| 442 | blockEntity.clearRemoved();
|
| 443 | BlockEntity previousEntry = this.blockEntities.put(pos.immutable(), blockEntity);
|
| 444 | if (previousEntry != null && previousEntry != blockEntity) {
|
| 445 | previousEntry.setRemoved();
|
| 446 | }
|
| 447 | }
|
| 448 | }
|
| 449 |
|
| 450 | @Override
|
| 451 | public @Nullable CompoundTag getBlockEntityNbtForSaving(final BlockPos blockPos, final HolderLookup.Provider registryAccess) {
|
| 452 | BlockEntity blockEntity = this.getBlockEntity(blockPos);
|
| 453 | if (blockEntity != null && !blockEntity.isRemoved()) {
|
| 454 | CompoundTag result = blockEntity.saveWithFullMetadata(this.level.registryAccess());
|
| 455 | result.putBoolean("keepPacked", false);
|
| 456 | return result;
|
| 457 | }
|
| 458 |
|
| 459 | CompoundTag result = this.pendingBlockEntities.get(blockPos);
|
| 460 | if (result != null) {
|
| 461 | result = result.copy();
|
| 462 | result.putBoolean("keepPacked", true);
|
| 463 | }
|
| 464 |
|
| 465 | return result;
|
| 466 | }
|
| 467 |
|
| 468 | @Override
|
| 469 | public void removeBlockEntity(final BlockPos pos) {
|
| 470 | if (this.isInLevel()) {
|
| 471 | BlockEntity removeThis = this.blockEntities.remove(pos);
|
| 472 | if (removeThis != null) {
|
| 473 | if (this.level instanceof ServerLevel serverLevel) {
|
| 474 | this.removeGameEventListener(removeThis, serverLevel);
|
| 475 | serverLevel.debugSynchronizers().dropBlockEntity(pos);
|
| 476 | }
|
| 477 |
|
| 478 | removeThis.setRemoved();
|
| 479 | }
|
| 480 | }
|
| 481 |
|
| 482 | this.removeBlockEntityTicker(pos);
|
| 483 | }
|
| 484 |
|
| 485 | private <T extends BlockEntity> void removeGameEventListener(final T blockEntity, final ServerLevel level) {
|
| 486 | if (blockEntity.getBlockState().getBlock() instanceof EntityBlock entityBlock) {
|
| 487 | GameEventListener listener = entityBlock.getListener(level, blockEntity);
|
| 488 | if (listener != null) {
|
| 489 | int section = SectionPos.blockToSectionCoord(blockEntity.getBlockPos().getY());
|
| 490 | GameEventListenerRegistry listenerRegistry = this.getListenerRegistry(section);
|
| 491 | listenerRegistry.unregister(listener);
|
| 492 | }
|
| 493 | }
|
| 494 | }
|
| 495 |
|
| 496 | private void removeGameEventListenerRegistry(final int sectionY) {
|
| 497 | this.gameEventListenerRegistrySections.remove(sectionY);
|
| 498 | }
|
| 499 |
|
| 500 | private void removeBlockEntityTicker(final BlockPos pos) {
|
| 501 | LevelChunk.RebindableTickingBlockEntityWrapper ticker = this.tickersInLevel.remove(pos);
|
| 502 | if (ticker != null) {
|
| 503 | ticker.rebind(NULL_TICKER);
|
| 504 | }
|
| 505 | }
|
| 506 |
|
| 507 | public void runPostLoad() {
|
| 508 | if (this.postLoad != null) {
|
| 509 | this.postLoad.run(this);
|
| 510 | this.postLoad = null;
|
| 511 | }
|
| 512 | }
|
| 513 |
|
| 514 | public boolean isEmpty() {
|
| 515 | return false;
|
| 516 | }
|
| 517 |
|
| 518 | public void replaceWithPacketData(final int chunkX, final int chunkZ, final ClientboundLevelChunkPacketData chunkData) {
|
| 519 | FriendlyByteBuf buffer = chunkData.getReadBuffer();
|
| 520 | Map<Heightmap.Types, long[]> heightmaps = chunkData.getHeightmaps();
|
| 521 | this.clearAllBlockEntities();
|
| 522 |
|
| 523 | for (LevelChunkSection section : this.sections) {
|
| 524 | section.read(buffer);
|
| 525 | }
|
| 526 |
|
| 527 | heightmaps.forEach(this::setHeightmap);
|
| 528 | this.initializeLightSources();
|
| 529 |
|
| 530 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this.problemPath(), LOGGER)) {
|
| 531 | chunkData.forEachBlockEntityTag(chunkX, chunkZ, (pos, type, tag) -> {
|
| 532 | BlockEntity blockEntity = this.getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
|
| 533 | if (blockEntity != null && tag != null && blockEntity.getType() == type) {
|
| 534 | blockEntity.loadWithComponents(TagValueInput.create(reporter.forChild(blockEntity.problemPath()), this.level.registryAccess(), tag));
|
| 535 | }
|
| 536 | });
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | public void replaceBiomes(final FriendlyByteBuf buffer) {
|
| 541 | for (LevelChunkSection section : this.sections) {
|
| 542 | section.readBiomes(buffer);
|
| 543 | }
|
| 544 | }
|
| 545 |
|
| 546 | public void setLoaded(final boolean loaded) {
|
| 547 | this.loaded = loaded;
|
| 548 | }
|
| 549 |
|
| 550 | public Level getLevel() {
|
| 551 | return this.level;
|
| 552 | }
|
| 553 |
|
| 554 | public Map<BlockPos, BlockEntity> getBlockEntities() {
|
| 555 | return this.blockEntities;
|
| 556 | }
|
| 557 |
|
| 558 | public void postProcessGeneration(final ServerLevel level) {
|
| 559 | ChunkPos chunkPos = this.getPos();
|
| 560 |
|
| 561 | for (int sectionIndex = 0; sectionIndex < this.postProcessing.length; sectionIndex++) {
|
| 562 | ShortList postProcessingSection = this.postProcessing[sectionIndex];
|
| 563 | if (postProcessingSection != null) {
|
| 564 | for (Short packedOffset : postProcessingSection) {
|
| 565 | BlockPos blockPos = ProtoChunk.unpackOffsetCoordinates(packedOffset, this.getSectionYFromSectionIndex(sectionIndex), chunkPos);
|
| 566 | BlockState blockState = this.getBlockState(blockPos);
|
| 567 | FluidState fluidState = blockState.getFluidState();
|
| 568 | if (!fluidState.isEmpty()) {
|
| 569 | fluidState.tick(level, blockPos, blockState);
|
| 570 | }
|
| 571 |
|
| 572 | if (blockState.getBlock() instanceof LiquidBlock) {
|
| 573 | blockState.tick(level, blockPos, level.getRandom());
|
| 574 | } else {
|
| 575 | BlockState blockStateNew = Block.updateFromNeighbourShapes(blockState, level, blockPos);
|
| 576 | if (blockStateNew != blockState) {
|
| 577 | level.setBlock(blockPos, blockStateNew, 276);
|
| 578 | }
|
| 579 | }
|
| 580 | }
|
| 581 |
|
| 582 | postProcessingSection.clear();
|
| 583 | }
|
| 584 | }
|
| 585 |
|
| 586 | for (BlockPos pos : ImmutableList.copyOf(this.pendingBlockEntities.keySet())) {
|
| 587 | this.getBlockEntity(pos);
|
| 588 | }
|
| 589 |
|
| 590 | this.pendingBlockEntities.clear();
|
| 591 | this.upgradeData.upgrade(this);
|
| 592 | }
|
| 593 |
|
| 594 | private @Nullable BlockEntity promotePendingBlockEntity(final BlockPos pos, final CompoundTag tag) {
|
| 595 | BlockState state = this.getBlockState(pos);
|
| 596 | BlockEntity blockEntity;
|
| 597 | if ("DUMMY".equals(tag.getStringOr("id", ""))) {
|
| 598 | if (state.hasBlockEntity()) {
|
| 599 | blockEntity = ((EntityBlock)state.getBlock()).newBlockEntity(pos, state);
|
| 600 | } else {
|
| 601 | blockEntity = null;
|
| 602 | LOGGER.warn("Tried to load a DUMMY block entity @ {} but found not block entity block {} at location", pos, state);
|
| 603 | }
|
| 604 | } else {
|
| 605 | blockEntity = BlockEntity.loadStatic(pos, state, tag, this.level.registryAccess());
|
| 606 | }
|
| 607 |
|
| 608 | if (blockEntity != null) {
|
| 609 | blockEntity.setLevel(this.level);
|
| 610 | this.addAndRegisterBlockEntity(blockEntity);
|
| 611 | } else {
|
| 612 | LOGGER.warn("Tried to load a block entity for block {} but failed at location {}", state, pos);
|
| 613 | }
|
| 614 |
|
| 615 | return blockEntity;
|
| 616 | }
|
| 617 |
|
| 618 | public void unpackTicks(final long currentTick) {
|
| 619 | this.blockTicks.unpack(currentTick);
|
| 620 | this.fluidTicks.unpack(currentTick);
|
| 621 | }
|
| 622 |
|
| 623 | public void registerTickContainerInLevel(final ServerLevel level) {
|
| 624 | level.getBlockTicks().addContainer(this.chunkPos, this.blockTicks);
|
| 625 | level.getFluidTicks().addContainer(this.chunkPos, this.fluidTicks);
|
| 626 | }
|
| 627 |
|
| 628 | public void unregisterTickContainerFromLevel(final ServerLevel level) {
|
| 629 | level.getBlockTicks().removeContainer(this.chunkPos);
|
| 630 | level.getFluidTicks().removeContainer(this.chunkPos);
|
| 631 | }
|
| 632 |
|
| 633 | @Override
|
| 634 | public void registerDebugValues(final ServerLevel level, final DebugValueSource.Registration registration) {
|
| 635 | if (!this.getAllStarts().isEmpty()) {
|
| 636 | registration.register(DebugSubscriptions.STRUCTURES, () -> {
|
| 637 | List<DebugStructureInfo> structures = new ArrayList<>();
|
| 638 |
|
| 639 | for (StructureStart start : this.getAllStarts().values()) {
|
| 640 | BoundingBox boundingBox = start.getBoundingBox();
|
| 641 | List<StructurePiece> pieces = start.getPieces();
|
| 642 | List<DebugStructureInfo.Piece> pieceInfos = new ArrayList<>(pieces.size());
|
| 643 |
|
| 644 | for (int i = 0; i < pieces.size(); i++) {
|
| 645 | boolean isStart = i == 0;
|
| 646 | pieceInfos.add(new DebugStructureInfo.Piece(pieces.get(i).getBoundingBox(), isStart));
|
| 647 | }
|
| 648 |
|
| 649 | structures.add(new DebugStructureInfo(boundingBox, pieceInfos));
|
| 650 | }
|
| 651 |
|
| 652 | return structures;
|
| 653 | });
|
| 654 | }
|
| 655 |
|
| 656 | registration.register(DebugSubscriptions.RAIDS, () -> level.getRaids().getRaidCentersInChunk(this.chunkPos));
|
| 657 | }
|
| 658 |
|
| 659 | @Override
|
| 660 | public ChunkStatus getPersistedStatus() {
|
| 661 | return ChunkStatus.FULL;
|
| 662 | }
|
| 663 |
|
| 664 | public FullChunkStatus getFullStatus() {
|
| 665 | return this.fullStatus == null ? FullChunkStatus.FULL : this.fullStatus.get();
|
| 666 | }
|
| 667 |
|
| 668 | public void setFullStatus(final Supplier<FullChunkStatus> fullStatus) {
|
| 669 | this.fullStatus = fullStatus;
|
| 670 | }
|
| 671 |
|
| 672 | public void clearAllBlockEntities() {
|
| 673 | this.blockEntities.values().forEach(BlockEntity::setRemoved);
|
| 674 | this.blockEntities.clear();
|
| 675 | this.tickersInLevel.values().forEach(ticker -> ticker.rebind(NULL_TICKER));
|
| 676 | this.tickersInLevel.clear();
|
| 677 | }
|
| 678 |
|
| 679 | public void registerAllBlockEntitiesAfterLevelLoad() {
|
| 680 | this.blockEntities.values().forEach(blockEntity -> {
|
| 681 | if (this.level instanceof ServerLevel serverLevel) {
|
| 682 | this.addGameEventListener(blockEntity, serverLevel);
|
| 683 | }
|
| 684 |
|
| 685 | this.level.onBlockEntityAdded(blockEntity);
|
| 686 | this.updateBlockEntityTicker(blockEntity);
|
| 687 | });
|
| 688 | }
|
| 689 |
|
| 690 | private <T extends BlockEntity> void addGameEventListener(final T blockEntity, final ServerLevel level) {
|
| 691 | if (blockEntity.getBlockState().getBlock() instanceof EntityBlock entityBlock) {
|
| 692 | GameEventListener listener = entityBlock.getListener(level, blockEntity);
|
| 693 | if (listener != null) {
|
| 694 | this.getListenerRegistry(SectionPos.blockToSectionCoord(blockEntity.getBlockPos().getY())).register(listener);
|
| 695 | }
|
| 696 | }
|
| 697 | }
|
| 698 |
|
| 699 | private <T extends BlockEntity> void updateBlockEntityTicker(final T blockEntity) {
|
| 700 | BlockState state = blockEntity.getBlockState();
|
| 701 | BlockEntityTicker<T> ticker = state.getTicker(this.level, (BlockEntityType<T>)blockEntity.getType());
|
| 702 | if (ticker == null) {
|
| 703 | this.removeBlockEntityTicker(blockEntity.getBlockPos());
|
| 704 | } else {
|
| 705 | this.tickersInLevel.compute(blockEntity.getBlockPos(), (blockPos, existingTicker) -> {
|
| 706 | TickingBlockEntity actualTicker = this.createTicker(blockEntity, ticker);
|
| 707 | if (existingTicker != null) {
|
| 708 | existingTicker.rebind(actualTicker);
|
| 709 | return (LevelChunk.RebindableTickingBlockEntityWrapper)existingTicker;
|
| 710 | } else if (this.isInLevel()) {
|
| 711 | LevelChunk.RebindableTickingBlockEntityWrapper result = new LevelChunk.RebindableTickingBlockEntityWrapper(actualTicker);
|
| 712 | this.level.addBlockEntityTicker(result);
|
| 713 | return result;
|
| 714 | } else {
|
| 715 | return null;
|
| 716 | }
|
| 717 | });
|
| 718 | }
|
| 719 | }
|
| 720 |
|
| 721 | private <T extends BlockEntity> TickingBlockEntity createTicker(final T blockEntity, final BlockEntityTicker<T> ticker) {
|
| 722 | return new LevelChunk.BoundTickingBlockEntity<>(blockEntity, ticker);
|
| 723 | }
|
| 724 |
|
| 725 | private class BoundTickingBlockEntity<T extends BlockEntity> implements TickingBlockEntity {
|
| 726 | private final T blockEntity;
|
| 727 | private final BlockEntityTicker<T> ticker;
|
| 728 | private boolean loggedInvalidBlockState;
|
| 729 |
|
| 730 | private BoundTickingBlockEntity(final T blockEntity, final BlockEntityTicker<T> ticker) {
|
| 731 | this.blockEntity = blockEntity;
|
| 732 | this.ticker = ticker;
|
| 733 | }
|
| 734 |
|
| 735 | @Override
|
| 736 | public void tick() {
|
| 737 | if (!this.blockEntity.isRemoved() && this.blockEntity.hasLevel()) {
|
| 738 | BlockPos pos = this.blockEntity.getBlockPos();
|
| 739 | if (LevelChunk.this.isTicking(pos)) {
|
| 740 | try {
|
| 741 | ProfilerFiller profiler = Profiler.get();
|
| 742 | profiler.push(this::getType);
|
| 743 | BlockState blockState = LevelChunk.this.getBlockState(pos);
|
| 744 | if (this.blockEntity.getType().isValid(blockState)) {
|
| 745 | this.ticker.tick(LevelChunk.this.level, this.blockEntity.getBlockPos(), blockState, this.blockEntity);
|
| 746 | this.loggedInvalidBlockState = false;
|
| 747 | } else if (!this.loggedInvalidBlockState) {
|
| 748 | this.loggedInvalidBlockState = true;
|
| 749 | LevelChunk.LOGGER.warn("Block entity {} @ {} state {} invalid for ticking:", LogUtils.defer(this::getType), LogUtils.defer(this::getPos), blockState);
|
| 750 | }
|
| 751 |
|
| 752 | profiler.pop();
|
| 753 | } catch (Throwable t) {
|
| 754 | CrashReport report = CrashReport.forThrowable(t, "Ticking block entity");
|
| 755 | CrashReportCategory category = report.addCategory("Block entity being ticked");
|
| 756 | this.blockEntity.fillCrashReportCategory(category);
|
| 757 | throw new ReportedException(report);
|
| 758 | }
|
| 759 | }
|
| 760 | }
|
| 761 | }
|
| 762 |
|
| 763 | @Override
|
| 764 | public boolean isRemoved() {
|
| 765 | return this.blockEntity.isRemoved();
|
| 766 | }
|
| 767 |
|
| 768 | @Override
|
| 769 | public BlockPos getPos() {
|
| 770 | return this.blockEntity.getBlockPos();
|
| 771 | }
|
| 772 |
|
| 773 | @Override
|
| 774 | public String getType() {
|
| 775 | return this.blockEntity.typeHolder().getRegisteredName();
|
| 776 | }
|
| 777 |
|
| 778 | @Override
|
| 779 | public String toString() {
|
| 780 | return "Level ticker for " + this.getType() + "@" + this.getPos();
|
| 781 | }
|
| 782 | }
|
| 783 |
|
| 784 | public enum EntityCreationType {
|
| 785 | IMMEDIATE,
|
| 786 | QUEUED,
|
| 787 | CHECK;
|
| 788 | }
|
| 789 |
|
| 790 | @FunctionalInterface
|
| 791 | public interface PostLoadProcessor {
|
| 792 | void run(LevelChunk levelChunk);
|
| 793 | }
|
| 794 |
|
| 795 | private static class RebindableTickingBlockEntityWrapper implements TickingBlockEntity {
|
| 796 | private TickingBlockEntity ticker;
|
| 797 |
|
| 798 | private RebindableTickingBlockEntityWrapper(final TickingBlockEntity ticker) {
|
| 799 | this.ticker = ticker;
|
| 800 | }
|
| 801 |
|
| 802 | private void rebind(final TickingBlockEntity ticker) {
|
| 803 | this.ticker = ticker;
|
| 804 | }
|
| 805 |
|
| 806 | @Override
|
| 807 | public void tick() {
|
| 808 | this.ticker.tick();
|
| 809 | }
|
| 810 |
|
| 811 | @Override
|
| 812 | public boolean isRemoved() {
|
| 813 | return this.ticker.isRemoved();
|
| 814 | }
|
| 815 |
|
| 816 | @Override
|
| 817 | public BlockPos getPos() {
|
| 818 | return this.ticker.getPos();
|
| 819 | }
|
| 820 |
|
| 821 | @Override
|
| 822 | public String getType() {
|
| 823 | return this.ticker.getType();
|
| 824 | }
|
| 825 |
|
| 826 | @Override
|
| 827 | public String toString() {
|
| 828 | return this.ticker + " <wrapped>";
|
| 829 | }
|
| 830 | }
|
| 831 |
|
| 832 | @FunctionalInterface
|
| 833 | public interface UnsavedListener {
|
| 834 | void setUnsaved(ChunkPos chunkPos);
|
| 835 | }
|
| 836 | }
|
| 837 |
|