| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.shorts.ShortOpenHashSet;
|
| 4 | import it.unimi.dsi.fastutil.shorts.ShortSet;
|
| 5 | import java.util.BitSet;
|
| 6 | import java.util.List;
|
| 7 | import java.util.concurrent.CompletableFuture;
|
| 8 | import java.util.concurrent.CompletionStage;
|
| 9 | import java.util.concurrent.Executor;
|
| 10 | import java.util.function.IntConsumer;
|
| 11 | import java.util.function.IntSupplier;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.SectionPos;
|
| 14 | import net.minecraft.network.protocol.Packet;
|
| 15 | import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundLightUpdatePacket;
|
| 17 | import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.level.ChunkPos;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 22 | import net.minecraft.world.level.LightLayer;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 26 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 27 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 28 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 29 | import net.minecraft.world.level.lighting.LevelLightEngine;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public class ChunkHolder extends GenerationChunkHolder {
|
| 33 | public static final ChunkResult<LevelChunk> UNLOADED_LEVEL_CHUNK = ChunkResult.error("Unloaded level chunk");
|
| 34 | private static final CompletableFuture<ChunkResult<LevelChunk>> UNLOADED_LEVEL_CHUNK_FUTURE = CompletableFuture.completedFuture(UNLOADED_LEVEL_CHUNK);
|
| 35 | private final LevelHeightAccessor levelHeightAccessor;
|
| 36 | private volatile CompletableFuture<ChunkResult<LevelChunk>> fullChunkFuture = UNLOADED_LEVEL_CHUNK_FUTURE;
|
| 37 | private volatile CompletableFuture<ChunkResult<LevelChunk>> tickingChunkFuture = UNLOADED_LEVEL_CHUNK_FUTURE;
|
| 38 | private volatile CompletableFuture<ChunkResult<LevelChunk>> entityTickingChunkFuture = UNLOADED_LEVEL_CHUNK_FUTURE;
|
| 39 | private int oldTicketLevel;
|
| 40 | private int ticketLevel;
|
| 41 | private int queueLevel;
|
| 42 | private boolean hasChangedSections;
|
| 43 | private final @Nullable ShortSet[] changedBlocksPerSection;
|
| 44 | private final BitSet blockChangedLightSectionFilter = new BitSet();
|
| 45 | private final BitSet skyChangedLightSectionFilter = new BitSet();
|
| 46 | private final LevelLightEngine lightEngine;
|
| 47 | private final ChunkHolder.LevelChangeListener onLevelChange;
|
| 48 | private final ChunkHolder.PlayerProvider playerProvider;
|
| 49 | private boolean wasAccessibleSinceLastSave;
|
| 50 | private CompletableFuture<?> pendingFullStateConfirmation = CompletableFuture.completedFuture(null);
|
| 51 | private CompletableFuture<?> sendSync = CompletableFuture.completedFuture(null);
|
| 52 | private CompletableFuture<?> saveSync = CompletableFuture.completedFuture(null);
|
| 53 |
|
| 54 | public ChunkHolder(
|
| 55 | final ChunkPos pos,
|
| 56 | final int ticketLevel,
|
| 57 | final LevelHeightAccessor levelHeightAccessor,
|
| 58 | final LevelLightEngine lightEngine,
|
| 59 | final ChunkHolder.LevelChangeListener onLevelChange,
|
| 60 | final ChunkHolder.PlayerProvider playerProvider
|
| 61 | ) {
|
| 62 | super(pos);
|
| 63 | this.levelHeightAccessor = levelHeightAccessor;
|
| 64 | this.lightEngine = lightEngine;
|
| 65 | this.onLevelChange = onLevelChange;
|
| 66 | this.playerProvider = playerProvider;
|
| 67 | this.oldTicketLevel = ChunkLevel.MAX_LEVEL + 1;
|
| 68 | this.ticketLevel = this.oldTicketLevel;
|
| 69 | this.queueLevel = this.oldTicketLevel;
|
| 70 | this.setTicketLevel(ticketLevel);
|
| 71 | this.changedBlocksPerSection = new ShortSet[levelHeightAccessor.getSectionsCount()];
|
| 72 | }
|
| 73 |
|
| 74 | public CompletableFuture<ChunkResult<LevelChunk>> getTickingChunkFuture() {
|
| 75 | return this.tickingChunkFuture;
|
| 76 | }
|
| 77 |
|
| 78 | public CompletableFuture<ChunkResult<LevelChunk>> getEntityTickingChunkFuture() {
|
| 79 | return this.entityTickingChunkFuture;
|
| 80 | }
|
| 81 |
|
| 82 | public CompletableFuture<ChunkResult<LevelChunk>> getFullChunkFuture() {
|
| 83 | return this.fullChunkFuture;
|
| 84 | }
|
| 85 |
|
| 86 | public @Nullable LevelChunk getTickingChunk() {
|
| 87 | return this.getTickingChunkFuture().getNow(UNLOADED_LEVEL_CHUNK).orElse(null);
|
| 88 | }
|
| 89 |
|
| 90 | public @Nullable LevelChunk getChunkToSend() {
|
| 91 | return !this.sendSync.isDone() ? null : this.getTickingChunk();
|
| 92 | }
|
| 93 |
|
| 94 | public CompletableFuture<?> getSendSyncFuture() {
|
| 95 | return this.sendSync;
|
| 96 | }
|
| 97 |
|
| 98 | public void addSendDependency(final CompletableFuture<?> sync) {
|
| 99 | if (this.sendSync.isDone()) {
|
| 100 | this.sendSync = sync;
|
| 101 | } else {
|
| 102 | this.sendSync = this.sendSync.thenCombine((CompletionStage<? extends Object>)sync, (a, b) -> null);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | public CompletableFuture<?> getSaveSyncFuture() {
|
| 107 | return this.saveSync;
|
| 108 | }
|
| 109 |
|
| 110 | public boolean isReadyForSaving() {
|
| 111 | return this.saveSync.isDone();
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | protected void addSaveDependency(final CompletableFuture<?> sync) {
|
| 116 | if (this.saveSync.isDone()) {
|
| 117 | this.saveSync = sync;
|
| 118 | } else {
|
| 119 | this.saveSync = this.saveSync.thenCombine((CompletionStage<? extends Object>)sync, (a, b) -> null);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public boolean blockChanged(final BlockPos pos) {
|
| 124 | LevelChunk chunk = this.getTickingChunk();
|
| 125 | if (chunk == null) {
|
| 126 | return false;
|
| 127 | }
|
| 128 |
|
| 129 | boolean hadChangedSections = this.hasChangedSections;
|
| 130 | int sectionIndex = this.levelHeightAccessor.getSectionIndex(pos.getY());
|
| 131 | ShortSet changedBlocksInSection = this.changedBlocksPerSection[sectionIndex];
|
| 132 | if (changedBlocksInSection == null) {
|
| 133 | this.hasChangedSections = true;
|
| 134 | changedBlocksInSection = new ShortOpenHashSet();
|
| 135 | this.changedBlocksPerSection[sectionIndex] = changedBlocksInSection;
|
| 136 | }
|
| 137 |
|
| 138 | changedBlocksInSection.add(SectionPos.sectionRelativePos(pos));
|
| 139 | return !hadChangedSections;
|
| 140 | }
|
| 141 |
|
| 142 | public boolean sectionLightChanged(final LightLayer layer, final int chunkY) {
|
| 143 | ChunkAccess chunk = this.getChunkIfPresent(ChunkStatus.INITIALIZE_LIGHT);
|
| 144 | if (chunk == null) {
|
| 145 | return false;
|
| 146 | }
|
| 147 |
|
| 148 | chunk.markUnsaved();
|
| 149 | LevelChunk tickingChunk = this.getTickingChunk();
|
| 150 | if (tickingChunk == null) {
|
| 151 | return false;
|
| 152 | }
|
| 153 |
|
| 154 | int minLightSection = this.lightEngine.getMinLightSection();
|
| 155 | int maxLightSection = this.lightEngine.getMaxLightSection();
|
| 156 | if (chunkY >= minLightSection && chunkY <= maxLightSection) {
|
| 157 | BitSet filter = layer == LightLayer.SKY ? this.skyChangedLightSectionFilter : this.blockChangedLightSectionFilter;
|
| 158 | int index = chunkY - minLightSection;
|
| 159 | if (!filter.get(index)) {
|
| 160 | filter.set(index);
|
| 161 | return true;
|
| 162 | } else {
|
| 163 | return false;
|
| 164 | }
|
| 165 | } else {
|
| 166 | return false;
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | public boolean hasChangesToBroadcast() {
|
| 171 | return this.hasChangedSections || !this.skyChangedLightSectionFilter.isEmpty() || !this.blockChangedLightSectionFilter.isEmpty();
|
| 172 | }
|
| 173 |
|
| 174 | public void broadcastChanges(final LevelChunk chunk) {
|
| 175 | if (this.hasChangesToBroadcast()) {
|
| 176 | Level level = chunk.getLevel();
|
| 177 | if (!this.skyChangedLightSectionFilter.isEmpty() || !this.blockChangedLightSectionFilter.isEmpty()) {
|
| 178 | List<ServerPlayer> borderPlayers = this.playerProvider.getPlayers(this.pos, true);
|
| 179 | if (!borderPlayers.isEmpty()) {
|
| 180 | ClientboundLightUpdatePacket lightPacket = new ClientboundLightUpdatePacket(
|
| 181 | chunk.getPos(), this.lightEngine, this.skyChangedLightSectionFilter, this.blockChangedLightSectionFilter
|
| 182 | );
|
| 183 | this.broadcast(borderPlayers, lightPacket);
|
| 184 | }
|
| 185 |
|
| 186 | this.skyChangedLightSectionFilter.clear();
|
| 187 | this.blockChangedLightSectionFilter.clear();
|
| 188 | }
|
| 189 |
|
| 190 | if (this.hasChangedSections) {
|
| 191 | List<ServerPlayer> players = this.playerProvider.getPlayers(this.pos, false);
|
| 192 |
|
| 193 | for (int sectionIndex = 0; sectionIndex < this.changedBlocksPerSection.length; sectionIndex++) {
|
| 194 | ShortSet changedBlocks = this.changedBlocksPerSection[sectionIndex];
|
| 195 | if (changedBlocks != null) {
|
| 196 | this.changedBlocksPerSection[sectionIndex] = null;
|
| 197 | if (!players.isEmpty()) {
|
| 198 | int sectionY = this.levelHeightAccessor.getSectionYFromSectionIndex(sectionIndex);
|
| 199 | SectionPos sectionPos = SectionPos.of(chunk.getPos(), sectionY);
|
| 200 | if (changedBlocks.size() == 1) {
|
| 201 | BlockPos pos = sectionPos.relativeToBlockPos(changedBlocks.iterator().nextShort());
|
| 202 | BlockState state = level.getBlockState(pos);
|
| 203 | this.broadcast(players, new ClientboundBlockUpdatePacket(pos, state));
|
| 204 | this.broadcastBlockEntityIfNeeded(players, level, pos, state);
|
| 205 | } else {
|
| 206 | LevelChunkSection section = chunk.getSection(sectionIndex);
|
| 207 | ClientboundSectionBlocksUpdatePacket packet = new ClientboundSectionBlocksUpdatePacket(sectionPos, changedBlocks, section);
|
| 208 | this.broadcast(players, packet);
|
| 209 | packet.runUpdates((pos, state) -> this.broadcastBlockEntityIfNeeded(players, level, pos, state));
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | this.hasChangedSections = false;
|
| 216 | }
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | private void broadcastBlockEntityIfNeeded(final List<ServerPlayer> players, final Level level, final BlockPos pos, final BlockState state) {
|
| 221 | if (state.hasBlockEntity()) {
|
| 222 | this.broadcastBlockEntity(players, level, pos);
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | private void broadcastBlockEntity(final List<ServerPlayer> players, final Level level, final BlockPos blockPos) {
|
| 227 | BlockEntity blockEntity = level.getBlockEntity(blockPos);
|
| 228 | if (blockEntity != null) {
|
| 229 | Packet<?> packet = blockEntity.getUpdatePacket();
|
| 230 | if (packet != null) {
|
| 231 | this.broadcast(players, packet);
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | private void broadcast(final List<ServerPlayer> players, final Packet<?> packet) {
|
| 237 | players.forEach(player -> player.connection.send(packet));
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public int getTicketLevel() {
|
| 242 | return this.ticketLevel;
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public int getQueueLevel() {
|
| 247 | return this.queueLevel;
|
| 248 | }
|
| 249 |
|
| 250 | private void setQueueLevel(final int queueLevel) {
|
| 251 | this.queueLevel = queueLevel;
|
| 252 | }
|
| 253 |
|
| 254 | public void setTicketLevel(final int ticketLevel) {
|
| 255 | this.ticketLevel = ticketLevel;
|
| 256 | }
|
| 257 |
|
| 258 | private void scheduleFullChunkPromotion(
|
| 259 | final ChunkMap scheduler, final CompletableFuture<ChunkResult<LevelChunk>> task, final Executor mainThreadExecutor, final FullChunkStatus status
|
| 260 | ) {
|
| 261 | this.pendingFullStateConfirmation.cancel(false);
|
| 262 | CompletableFuture<Void> confirmation = new CompletableFuture<>();
|
| 263 | confirmation.thenRunAsync(() -> scheduler.onFullChunkStatusChange(this.pos, status), mainThreadExecutor);
|
| 264 | this.pendingFullStateConfirmation = confirmation;
|
| 265 | task.thenAccept(r -> r.ifSuccess(l -> confirmation.complete(null)));
|
| 266 | }
|
| 267 |
|
| 268 | private void demoteFullChunk(final ChunkMap scheduler, final FullChunkStatus status) {
|
| 269 | this.pendingFullStateConfirmation.cancel(false);
|
| 270 | scheduler.onFullChunkStatusChange(this.pos, status);
|
| 271 | }
|
| 272 |
|
| 273 | protected void updateFutures(final ChunkMap scheduler, final Executor mainThreadExecutor) {
|
| 274 | FullChunkStatus oldFullStatus = ChunkLevel.fullStatus(this.oldTicketLevel);
|
| 275 | FullChunkStatus newFullStatus = ChunkLevel.fullStatus(this.ticketLevel);
|
| 276 | boolean wasAccessible = oldFullStatus.isOrAfter(FullChunkStatus.FULL);
|
| 277 | boolean isAccessible = newFullStatus.isOrAfter(FullChunkStatus.FULL);
|
| 278 | this.wasAccessibleSinceLastSave |= isAccessible;
|
| 279 | if (!wasAccessible && isAccessible) {
|
| 280 | this.fullChunkFuture = scheduler.prepareAccessibleChunk(this);
|
| 281 | this.scheduleFullChunkPromotion(scheduler, this.fullChunkFuture, mainThreadExecutor, FullChunkStatus.FULL);
|
| 282 | this.addSaveDependency(this.fullChunkFuture);
|
| 283 | }
|
| 284 |
|
| 285 | if (wasAccessible && !isAccessible) {
|
| 286 | this.fullChunkFuture.complete(UNLOADED_LEVEL_CHUNK);
|
| 287 | this.fullChunkFuture = UNLOADED_LEVEL_CHUNK_FUTURE;
|
| 288 | }
|
| 289 |
|
| 290 | boolean wasTicking = oldFullStatus.isOrAfter(FullChunkStatus.BLOCK_TICKING);
|
| 291 | boolean isTicking = newFullStatus.isOrAfter(FullChunkStatus.BLOCK_TICKING);
|
| 292 | if (!wasTicking && isTicking) {
|
| 293 | this.tickingChunkFuture = scheduler.prepareTickingChunk(this);
|
| 294 | this.scheduleFullChunkPromotion(scheduler, this.tickingChunkFuture, mainThreadExecutor, FullChunkStatus.BLOCK_TICKING);
|
| 295 | this.addSaveDependency(this.tickingChunkFuture);
|
| 296 | }
|
| 297 |
|
| 298 | if (wasTicking && !isTicking) {
|
| 299 | this.tickingChunkFuture.complete(UNLOADED_LEVEL_CHUNK);
|
| 300 | this.tickingChunkFuture = UNLOADED_LEVEL_CHUNK_FUTURE;
|
| 301 | }
|
| 302 |
|
| 303 | boolean wasEntityTicking = oldFullStatus.isOrAfter(FullChunkStatus.ENTITY_TICKING);
|
| 304 | boolean isEntityTicking = newFullStatus.isOrAfter(FullChunkStatus.ENTITY_TICKING);
|
| 305 | if (!wasEntityTicking && isEntityTicking) {
|
| 306 | if (this.entityTickingChunkFuture != UNLOADED_LEVEL_CHUNK_FUTURE) {
|
| 307 | throw (IllegalStateException)Util.pauseInIde(new IllegalStateException());
|
| 308 | }
|
| 309 |
|
| 310 | this.entityTickingChunkFuture = scheduler.prepareEntityTickingChunk(this);
|
| 311 | this.scheduleFullChunkPromotion(scheduler, this.entityTickingChunkFuture, mainThreadExecutor, FullChunkStatus.ENTITY_TICKING);
|
| 312 | this.addSaveDependency(this.entityTickingChunkFuture);
|
| 313 | }
|
| 314 |
|
| 315 | if (wasEntityTicking && !isEntityTicking) {
|
| 316 | this.entityTickingChunkFuture.complete(UNLOADED_LEVEL_CHUNK);
|
| 317 | this.entityTickingChunkFuture = UNLOADED_LEVEL_CHUNK_FUTURE;
|
| 318 | }
|
| 319 |
|
| 320 | if (!newFullStatus.isOrAfter(oldFullStatus)) {
|
| 321 | this.demoteFullChunk(scheduler, newFullStatus);
|
| 322 | }
|
| 323 |
|
| 324 | this.onLevelChange.onLevelChange(this.pos, this::getQueueLevel, this.ticketLevel, this::setQueueLevel);
|
| 325 | this.oldTicketLevel = this.ticketLevel;
|
| 326 | }
|
| 327 |
|
| 328 | public boolean wasAccessibleSinceLastSave() {
|
| 329 | return this.wasAccessibleSinceLastSave;
|
| 330 | }
|
| 331 |
|
| 332 | public void refreshAccessibility() {
|
| 333 | this.wasAccessibleSinceLastSave = ChunkLevel.fullStatus(this.ticketLevel).isOrAfter(FullChunkStatus.FULL);
|
| 334 | }
|
| 335 |
|
| 336 | @FunctionalInterface
|
| 337 | public interface LevelChangeListener {
|
| 338 | void onLevelChange(ChunkPos pos, IntSupplier oldLevel, int newLevel, IntConsumer setQueueLevel);
|
| 339 | }
|
| 340 |
|
| 341 | public interface PlayerProvider {
|
| 342 | List<ServerPlayer> getPlayers(ChunkPos pos, boolean borderOnly);
|
| 343 | }
|
| 344 | }
|
| 345 |
|