| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.mojang.datafixers.DataFixer;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 8 | import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
|
| 9 | import java.io.IOException;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Objects;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import java.util.concurrent.CompletionStage;
|
| 17 | import java.util.concurrent.Executor;
|
| 18 | import java.util.function.BooleanSupplier;
|
| 19 | import java.util.function.Consumer;
|
| 20 | import java.util.function.Predicate;
|
| 21 | import net.minecraft.core.BlockPos;
|
| 22 | import net.minecraft.core.SectionPos;
|
| 23 | import net.minecraft.network.protocol.Packet;
|
| 24 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 25 | import net.minecraft.util.FileUtil;
|
| 26 | import net.minecraft.util.Util;
|
| 27 | import net.minecraft.util.VisibleForDebug;
|
| 28 | import net.minecraft.util.profiling.Profiler;
|
| 29 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 30 | import net.minecraft.util.thread.BlockableEventLoop;
|
| 31 | import net.minecraft.world.entity.Entity;
|
| 32 | import net.minecraft.world.entity.MobCategory;
|
| 33 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 34 | import net.minecraft.world.level.ChunkPos;
|
| 35 | import net.minecraft.world.level.Level;
|
| 36 | import net.minecraft.world.level.LightLayer;
|
| 37 | import net.minecraft.world.level.LocalMobCapCalculator;
|
| 38 | import net.minecraft.world.level.NaturalSpawner;
|
| 39 | import net.minecraft.world.level.TicketStorage;
|
| 40 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 41 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 42 | import net.minecraft.world.level.chunk.ChunkGeneratorStructureState;
|
| 43 | import net.minecraft.world.level.chunk.ChunkSource;
|
| 44 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 45 | import net.minecraft.world.level.chunk.LightChunk;
|
| 46 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 47 | import net.minecraft.world.level.chunk.storage.ChunkScanAccess;
|
| 48 | import net.minecraft.world.level.entity.ChunkStatusUpdateListener;
|
| 49 | import net.minecraft.world.level.gamerules.GameRules;
|
| 50 | import net.minecraft.world.level.levelgen.RandomState;
|
| 51 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 52 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 53 | import net.minecraft.world.level.storage.SavedDataStorage;
|
| 54 | import org.jspecify.annotations.Nullable;
|
| 55 | import org.slf4j.Logger;
|
| 56 |
|
| 57 | public class ServerChunkCache extends ChunkSource {
|
| 58 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 59 | private final DistanceManager distanceManager;
|
| 60 | private final ServerLevel level;
|
| 61 | private final Thread mainThread;
|
| 62 | private final ThreadedLevelLightEngine lightEngine;
|
| 63 | private final ServerChunkCache.MainThreadExecutor mainThreadProcessor;
|
| 64 | public final ChunkMap chunkMap;
|
| 65 | private final SavedDataStorage savedDataStorage;
|
| 66 | private final TicketStorage ticketStorage;
|
| 67 | private boolean spawnEnemies = true;
|
| 68 | private static final int CACHE_SIZE = 4;
|
| 69 | private final long[] lastChunkPos = new long[4];
|
| 70 | private final @Nullable ChunkStatus[] lastChunkStatus = new ChunkStatus[4];
|
| 71 | private final @Nullable ChunkAccess[] lastChunk = new ChunkAccess[4];
|
| 72 | private final List<LevelChunk> spawningChunks = new ObjectArrayList<>();
|
| 73 | private final Set<ChunkHolder> chunkHoldersToBroadcast = new ReferenceOpenHashSet<>();
|
| 74 | @VisibleForDebug
|
| 75 | private NaturalSpawner.@Nullable SpawnState lastSpawnState;
|
| 76 |
|
| 77 | public ServerChunkCache(
|
| 78 | final ServerLevel level,
|
| 79 | final LevelStorageSource.LevelStorageAccess levelStorage,
|
| 80 | final DataFixer fixerUpper,
|
| 81 | final StructureTemplateManager structureTemplateManager,
|
| 82 | final Executor executor,
|
| 83 | final ChunkGenerator generator,
|
| 84 | final int viewDistance,
|
| 85 | final int simulationDistance,
|
| 86 | final boolean syncWrites,
|
| 87 | final ChunkStatusUpdateListener chunkStatusListener
|
| 88 | ) {
|
| 89 | this.level = level;
|
| 90 | this.mainThreadProcessor = new ServerChunkCache.MainThreadExecutor(level);
|
| 91 | this.mainThread = Thread.currentThread();
|
| 92 | Path dataFolder = levelStorage.getDimensionPath(level.dimension()).resolve("data");
|
| 93 |
|
| 94 | try {
|
| 95 | FileUtil.createDirectoriesSafe(dataFolder);
|
| 96 | } catch (IOException e) {
|
| 97 | LOGGER.error("Failed to create dimension data storage directory", e);
|
| 98 | }
|
| 99 |
|
| 100 | this.savedDataStorage = new SavedDataStorage(dataFolder, fixerUpper, level.registryAccess());
|
| 101 | this.ticketStorage = this.savedDataStorage.computeIfAbsent(TicketStorage.TYPE);
|
| 102 | this.chunkMap = new ChunkMap(
|
| 103 | level,
|
| 104 | levelStorage,
|
| 105 | fixerUpper,
|
| 106 | structureTemplateManager,
|
| 107 | executor,
|
| 108 | this.mainThreadProcessor,
|
| 109 | this,
|
| 110 | generator,
|
| 111 | chunkStatusListener,
|
| 112 | this.ticketStorage,
|
| 113 | viewDistance,
|
| 114 | syncWrites
|
| 115 | );
|
| 116 | this.lightEngine = this.chunkMap.getLightEngine();
|
| 117 | this.distanceManager = this.chunkMap.getDistanceManager();
|
| 118 | this.distanceManager.updateSimulationDistance(simulationDistance);
|
| 119 | this.clearCache();
|
| 120 | }
|
| 121 |
|
| 122 | public ThreadedLevelLightEngine getLightEngine() {
|
| 123 | return this.lightEngine;
|
| 124 | }
|
| 125 |
|
| 126 | private @Nullable ChunkHolder getVisibleChunkIfPresent(final long key) {
|
| 127 | return this.chunkMap.getVisibleChunkIfPresent(key);
|
| 128 | }
|
| 129 |
|
| 130 | private void storeInCache(final long pos, final @Nullable ChunkAccess chunk, final ChunkStatus status) {
|
| 131 | for (int i = 3; i > 0; i--) {
|
| 132 | this.lastChunkPos[i] = this.lastChunkPos[i - 1];
|
| 133 | this.lastChunkStatus[i] = this.lastChunkStatus[i - 1];
|
| 134 | this.lastChunk[i] = this.lastChunk[i - 1];
|
| 135 | }
|
| 136 |
|
| 137 | this.lastChunkPos[0] = pos;
|
| 138 | this.lastChunkStatus[0] = status;
|
| 139 | this.lastChunk[0] = chunk;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public @Nullable ChunkAccess getChunk(final int x, final int z, final ChunkStatus targetStatus, final boolean loadOrGenerate) {
|
| 144 | if (Thread.currentThread() != this.mainThread) {
|
| 145 | return CompletableFuture.<ChunkAccess>supplyAsync(() -> this.getChunk(x, z, targetStatus, loadOrGenerate), this.mainThreadProcessor).join();
|
| 146 | }
|
| 147 |
|
| 148 | ProfilerFiller profiler = Profiler.get();
|
| 149 | profiler.incrementCounter("getChunk");
|
| 150 | long pos = ChunkPos.pack(x, z);
|
| 151 |
|
| 152 | for (int i = 0; i < 4; i++) {
|
| 153 | if (pos == this.lastChunkPos[i] && targetStatus == this.lastChunkStatus[i]) {
|
| 154 | ChunkAccess chunkAccess = this.lastChunk[i];
|
| 155 | if (chunkAccess != null || !loadOrGenerate) {
|
| 156 | return chunkAccess;
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | profiler.incrementCounter("getChunkCacheMiss");
|
| 162 | CompletableFuture<ChunkResult<ChunkAccess>> serverFuture = this.getChunkFutureMainThread(x, z, targetStatus, loadOrGenerate);
|
| 163 | this.mainThreadProcessor.managedBlock(serverFuture::isDone);
|
| 164 | ChunkResult<ChunkAccess> chunkResult = serverFuture.join();
|
| 165 | ChunkAccess chunk = chunkResult.orElse(null);
|
| 166 | if (chunk == null && loadOrGenerate) {
|
| 167 | throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("Chunk not there when requested: " + chunkResult.getError()));
|
| 168 | }
|
| 169 |
|
| 170 | this.storeInCache(pos, chunk, targetStatus);
|
| 171 | return chunk;
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public @Nullable LevelChunk getChunkNow(final int x, final int z) {
|
| 176 | if (Thread.currentThread() != this.mainThread) {
|
| 177 | return null;
|
| 178 | }
|
| 179 |
|
| 180 | Profiler.get().incrementCounter("getChunkNow");
|
| 181 | long pos = ChunkPos.pack(x, z);
|
| 182 |
|
| 183 | for (int i = 0; i < 4; i++) {
|
| 184 | if (pos == this.lastChunkPos[i] && this.lastChunkStatus[i] == ChunkStatus.FULL) {
|
| 185 | return this.lastChunk[i] instanceof LevelChunk levelChunk ? levelChunk : null;
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | ChunkHolder chunkHolder = this.getVisibleChunkIfPresent(pos);
|
| 190 | if (chunkHolder == null) {
|
| 191 | return null;
|
| 192 | }
|
| 193 |
|
| 194 | ChunkAccess chunk = chunkHolder.getChunkIfPresent(ChunkStatus.FULL);
|
| 195 | if (chunk != null) {
|
| 196 | this.storeInCache(pos, chunk, ChunkStatus.FULL);
|
| 197 | if (chunk instanceof LevelChunk levelChunk) {
|
| 198 | return levelChunk;
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | return null;
|
| 203 | }
|
| 204 |
|
| 205 | private void clearCache() {
|
| 206 | Arrays.fill(this.lastChunkPos, ChunkPos.INVALID_CHUNK_POS);
|
| 207 | Arrays.fill(this.lastChunkStatus, null);
|
| 208 | Arrays.fill(this.lastChunk, null);
|
| 209 | }
|
| 210 |
|
| 211 | public CompletableFuture<ChunkResult<ChunkAccess>> getChunkFuture(final int x, final int z, final ChunkStatus targetStatus, final boolean loadOrGenerate) {
|
| 212 | boolean isMainThread = Thread.currentThread() == this.mainThread;
|
| 213 | CompletableFuture<ChunkResult<ChunkAccess>> serverFuture;
|
| 214 | if (isMainThread) {
|
| 215 | serverFuture = this.getChunkFutureMainThread(x, z, targetStatus, loadOrGenerate);
|
| 216 | this.mainThreadProcessor.managedBlock(serverFuture::isDone);
|
| 217 | } else {
|
| 218 | serverFuture = CompletableFuture.<CompletableFuture<ChunkResult<ChunkAccess>>>supplyAsync(
|
| 219 | () -> this.getChunkFutureMainThread(x, z, targetStatus, loadOrGenerate), this.mainThreadProcessor
|
| 220 | )
|
| 221 | .thenCompose(chunk -> (CompletionStage<ChunkResult<ChunkAccess>>)chunk);
|
| 222 | }
|
| 223 |
|
| 224 | return serverFuture;
|
| 225 | }
|
| 226 |
|
| 227 | private CompletableFuture<ChunkResult<ChunkAccess>> getChunkFutureMainThread(
|
| 228 | final int x, final int z, final ChunkStatus targetStatus, final boolean loadOrGenerate
|
| 229 | ) {
|
| 230 | ChunkPos pos = new ChunkPos(x, z);
|
| 231 | long key = pos.pack();
|
| 232 | int targetTicketLevel = ChunkLevel.byStatus(targetStatus);
|
| 233 | ChunkHolder chunkHolder = this.getVisibleChunkIfPresent(key);
|
| 234 | if (loadOrGenerate) {
|
| 235 | this.addTicket(new Ticket(TicketType.UNKNOWN, targetTicketLevel), pos);
|
| 236 | if (this.chunkAbsent(chunkHolder, targetTicketLevel)) {
|
| 237 | ProfilerFiller profiler = Profiler.get();
|
| 238 | profiler.push("chunkLoad");
|
| 239 | this.runDistanceManagerUpdates();
|
| 240 | chunkHolder = this.getVisibleChunkIfPresent(key);
|
| 241 | profiler.pop();
|
| 242 | if (this.chunkAbsent(chunkHolder, targetTicketLevel)) {
|
| 243 | throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("No chunk holder after ticket has been added"));
|
| 244 | }
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | return this.chunkAbsent(chunkHolder, targetTicketLevel)
|
| 249 | ? GenerationChunkHolder.UNLOADED_CHUNK_FUTURE
|
| 250 | : chunkHolder.scheduleChunkGenerationTask(targetStatus, this.chunkMap);
|
| 251 | }
|
| 252 |
|
| 253 | private boolean chunkAbsent(final @Nullable ChunkHolder chunkHolder, final int targetTicketLevel) {
|
| 254 | return chunkHolder == null || chunkHolder.getTicketLevel() > targetTicketLevel;
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public boolean hasChunk(final int x, final int z) {
|
| 259 | ChunkHolder chunkHolder = this.getVisibleChunkIfPresent(new ChunkPos(x, z).pack());
|
| 260 | int targetTicketLevel = ChunkLevel.byStatus(ChunkStatus.FULL);
|
| 261 | return !this.chunkAbsent(chunkHolder, targetTicketLevel);
|
| 262 | }
|
| 263 |
|
| 264 | @Override
|
| 265 | public @Nullable LightChunk getChunkForLighting(final int x, final int z) {
|
| 266 | long key = ChunkPos.pack(x, z);
|
| 267 | ChunkHolder chunkHolder = this.getVisibleChunkIfPresent(key);
|
| 268 | return chunkHolder == null ? null : chunkHolder.getChunkIfPresentUnchecked(ChunkStatus.INITIALIZE_LIGHT.getParent());
|
| 269 | }
|
| 270 |
|
| 271 | public Level getLevel() {
|
| 272 | return this.level;
|
| 273 | }
|
| 274 |
|
| 275 | public boolean pollTask() {
|
| 276 | return this.mainThreadProcessor.pollTask();
|
| 277 | }
|
| 278 |
|
| 279 | boolean runDistanceManagerUpdates() {
|
| 280 | boolean updated = this.distanceManager.runAllUpdates(this.chunkMap);
|
| 281 | boolean promoted = this.chunkMap.promoteChunkMap();
|
| 282 | this.chunkMap.runGenerationTasks();
|
| 283 | if (!updated && !promoted) {
|
| 284 | return false;
|
| 285 | }
|
| 286 |
|
| 287 | this.clearCache();
|
| 288 | return true;
|
| 289 | }
|
| 290 |
|
| 291 | public boolean isPositionTicking(final long chunkKey) {
|
| 292 | if (!this.level.shouldTickBlocksAt(chunkKey)) {
|
| 293 | return false;
|
| 294 | }
|
| 295 |
|
| 296 | ChunkHolder holder = this.getVisibleChunkIfPresent(chunkKey);
|
| 297 | return holder == null ? false : holder.getTickingChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK).isSuccess();
|
| 298 | }
|
| 299 |
|
| 300 | public void save(final boolean flushStorage) {
|
| 301 | this.runDistanceManagerUpdates();
|
| 302 | this.chunkMap.saveAllChunks(flushStorage);
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | public void close() throws IOException {
|
| 307 | this.save(true);
|
| 308 | this.savedDataStorage.close();
|
| 309 | this.lightEngine.close();
|
| 310 | this.chunkMap.close();
|
| 311 | }
|
| 312 |
|
| 313 | @Override
|
| 314 | public void tick(final BooleanSupplier haveTime, final boolean tickChunks) {
|
| 315 | ProfilerFiller profiler = Profiler.get();
|
| 316 | profiler.push("purge");
|
| 317 | if (this.level.tickRateManager().runsNormally() || !tickChunks) {
|
| 318 | this.ticketStorage.purgeStaleTickets(this.chunkMap);
|
| 319 | }
|
| 320 |
|
| 321 | this.runDistanceManagerUpdates();
|
| 322 | profiler.popPush("chunks");
|
| 323 | if (tickChunks) {
|
| 324 | this.tickChunks();
|
| 325 | this.chunkMap.tick();
|
| 326 | }
|
| 327 |
|
| 328 | profiler.popPush("unload");
|
| 329 | this.chunkMap.tick(haveTime);
|
| 330 | profiler.pop();
|
| 331 | this.clearCache();
|
| 332 | }
|
| 333 |
|
| 334 | private void tickChunks() {
|
| 335 | if (!this.level.isDebug()) {
|
| 336 | ProfilerFiller profiler = Profiler.get();
|
| 337 | profiler.push("pollingChunks");
|
| 338 | if (this.level.tickRateManager().runsNormally()) {
|
| 339 | profiler.push("tickingChunks");
|
| 340 | this.tickChunks(profiler);
|
| 341 | profiler.pop();
|
| 342 | }
|
| 343 |
|
| 344 | this.broadcastChangedChunks(profiler);
|
| 345 | profiler.pop();
|
| 346 | }
|
| 347 | }
|
| 348 |
|
| 349 | private void broadcastChangedChunks(final ProfilerFiller profiler) {
|
| 350 | profiler.push("broadcast");
|
| 351 |
|
| 352 | for (ChunkHolder chunkHolder : this.chunkHoldersToBroadcast) {
|
| 353 | LevelChunk chunk = chunkHolder.getTickingChunk();
|
| 354 | if (chunk != null) {
|
| 355 | chunkHolder.broadcastChanges(chunk);
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | this.chunkHoldersToBroadcast.clear();
|
| 360 | profiler.pop();
|
| 361 | }
|
| 362 |
|
| 363 | private void tickChunks(final ProfilerFiller profiler) {
|
| 364 | profiler.push("naturalSpawnCount");
|
| 365 | int chunkCount = this.distanceManager.getNaturalSpawnChunkCount();
|
| 366 | NaturalSpawner.SpawnState spawnCookie = NaturalSpawner.createState(chunkCount, this.level, this::getFullChunk, new LocalMobCapCalculator(this.chunkMap));
|
| 367 | this.lastSpawnState = spawnCookie;
|
| 368 | boolean doMobSpawning = this.level.getGameRules().get(GameRules.SPAWN_MOBS);
|
| 369 | int tickSpeed = this.level.getGameRules().get(GameRules.RANDOM_TICK_SPEED);
|
| 370 | List<MobCategory> spawningCategories;
|
| 371 | if (doMobSpawning) {
|
| 372 | boolean spawnPersistent = this.level.getGameTime() % 400L == 0L;
|
| 373 | spawningCategories = NaturalSpawner.getFilteredSpawningCategories(spawnCookie, this.spawnEnemies, spawnPersistent);
|
| 374 | } else {
|
| 375 | spawningCategories = List.of();
|
| 376 | }
|
| 377 |
|
| 378 | List<LevelChunk> spawningChunks = this.spawningChunks;
|
| 379 |
|
| 380 | try {
|
| 381 | profiler.popPush("filteringSpawningChunks");
|
| 382 | this.chunkMap.collectSpawningChunks(spawningChunks);
|
| 383 | profiler.popPush("shuffleSpawningChunks");
|
| 384 | Util.shuffle(spawningChunks, this.level.getRandom());
|
| 385 | profiler.popPush("tickSpawningChunks");
|
| 386 |
|
| 387 | for (LevelChunk chunk : spawningChunks) {
|
| 388 | this.tickSpawningChunk(chunk, spawningCategories, spawnCookie);
|
| 389 | }
|
| 390 | } finally {
|
| 391 | spawningChunks.clear();
|
| 392 | }
|
| 393 |
|
| 394 | profiler.popPush("tickTickingChunks");
|
| 395 | this.chunkMap.forEachBlockTickingChunk(chunkx -> this.level.tickChunk(chunkx, tickSpeed));
|
| 396 | if (doMobSpawning) {
|
| 397 | profiler.popPush("customSpawners");
|
| 398 | this.level.tickCustomSpawners(this.spawnEnemies);
|
| 399 | }
|
| 400 |
|
| 401 | profiler.pop();
|
| 402 | }
|
| 403 |
|
| 404 | private void tickSpawningChunk(final LevelChunk chunk, final List<MobCategory> spawningCategories, final NaturalSpawner.SpawnState spawnCookie) {
|
| 405 | ChunkPos chunkPos = chunk.getPos();
|
| 406 | chunk.incrementInhabitedTime();
|
| 407 | if (this.distanceManager.inEntityTickingRange(chunkPos.pack())) {
|
| 408 | this.level.tickThunder(chunk);
|
| 409 | }
|
| 410 |
|
| 411 | if (!spawningCategories.isEmpty()) {
|
| 412 | if (this.level.canSpawnEntitiesInChunk(chunkPos)) {
|
| 413 | NaturalSpawner.spawnForChunk(this.level, chunk, spawnCookie, spawningCategories);
|
| 414 | }
|
| 415 | }
|
| 416 | }
|
| 417 |
|
| 418 | private void getFullChunk(final long chunkKey, final Consumer<LevelChunk> output) {
|
| 419 | ChunkHolder chunkHolder = this.getVisibleChunkIfPresent(chunkKey);
|
| 420 | if (chunkHolder != null) {
|
| 421 | chunkHolder.getFullChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK).ifSuccess(output);
|
| 422 | }
|
| 423 | }
|
| 424 |
|
| 425 | @Override
|
| 426 | public String gatherStats() {
|
| 427 | return Integer.toString(this.getLoadedChunksCount());
|
| 428 | }
|
| 429 |
|
| 430 | @VisibleForTesting
|
| 431 | public int getPendingTasksCount() {
|
| 432 | return this.mainThreadProcessor.getPendingTasksCount();
|
| 433 | }
|
| 434 |
|
| 435 | public ChunkGenerator getGenerator() {
|
| 436 | return this.chunkMap.generator();
|
| 437 | }
|
| 438 |
|
| 439 | public ChunkGeneratorStructureState getGeneratorState() {
|
| 440 | return this.chunkMap.generatorState();
|
| 441 | }
|
| 442 |
|
| 443 | public RandomState randomState() {
|
| 444 | return this.chunkMap.randomState();
|
| 445 | }
|
| 446 |
|
| 447 | @Override
|
| 448 | public int getLoadedChunksCount() {
|
| 449 | return this.chunkMap.size();
|
| 450 | }
|
| 451 |
|
| 452 | public void blockChanged(final BlockPos pos) {
|
| 453 | int xc = SectionPos.blockToSectionCoord(pos.getX());
|
| 454 | int zc = SectionPos.blockToSectionCoord(pos.getZ());
|
| 455 | ChunkHolder chunk = this.getVisibleChunkIfPresent(ChunkPos.pack(xc, zc));
|
| 456 | if (chunk != null && chunk.blockChanged(pos)) {
|
| 457 | this.chunkHoldersToBroadcast.add(chunk);
|
| 458 | }
|
| 459 | }
|
| 460 |
|
| 461 | @Override
|
| 462 | public void onLightUpdate(final LightLayer layer, final SectionPos pos) {
|
| 463 | this.mainThreadProcessor.execute(() -> {
|
| 464 | ChunkHolder chunk = this.getVisibleChunkIfPresent(pos.chunk().pack());
|
| 465 | if (chunk != null && chunk.sectionLightChanged(layer, pos.y())) {
|
| 466 | this.chunkHoldersToBroadcast.add(chunk);
|
| 467 | }
|
| 468 | });
|
| 469 | }
|
| 470 |
|
| 471 | public boolean hasActiveTickets() {
|
| 472 | return this.ticketStorage.shouldKeepDimensionActive();
|
| 473 | }
|
| 474 |
|
| 475 | public void addTicket(final Ticket ticket, final ChunkPos pos) {
|
| 476 | this.ticketStorage.addTicket(ticket, pos);
|
| 477 | }
|
| 478 |
|
| 479 | public CompletableFuture<?> addTicketAndLoadWithRadius(final TicketType type, final ChunkPos pos, final int radius) {
|
| 480 | if (!type.doesLoad()) {
|
| 481 | throw new IllegalStateException("Ticket type " + type + " does not trigger chunk loading");
|
| 482 | }
|
| 483 |
|
| 484 | if (type.canExpireIfUnloaded()) {
|
| 485 | throw new IllegalStateException("Ticket type " + type + " can expire before it loads, cannot fetch asynchronously");
|
| 486 | }
|
| 487 |
|
| 488 | this.addTicketWithRadius(type, pos, radius);
|
| 489 | this.runDistanceManagerUpdates();
|
| 490 | ChunkHolder chunkHolder = this.getVisibleChunkIfPresent(pos.pack());
|
| 491 | Objects.requireNonNull(chunkHolder, "No chunk was scheduled for loading");
|
| 492 | return this.chunkMap.getChunkRangeFuture(chunkHolder, radius, distance -> ChunkStatus.FULL);
|
| 493 | }
|
| 494 |
|
| 495 | public void addTicketWithRadius(final TicketType type, final ChunkPos pos, final int radius) {
|
| 496 | this.ticketStorage.addTicketWithRadius(type, pos, radius);
|
| 497 | }
|
| 498 |
|
| 499 | public void removeTicketWithRadius(final TicketType type, final ChunkPos pos, final int radius) {
|
| 500 | this.ticketStorage.removeTicketWithRadius(type, pos, radius);
|
| 501 | }
|
| 502 |
|
| 503 | @Override
|
| 504 | public boolean updateChunkForced(final ChunkPos pos, final boolean forced) {
|
| 505 | return this.ticketStorage.updateChunkForced(pos, forced);
|
| 506 | }
|
| 507 |
|
| 508 | @Override
|
| 509 | public LongSet getForceLoadedChunks() {
|
| 510 | return this.ticketStorage.getForceLoadedChunks();
|
| 511 | }
|
| 512 |
|
| 513 | public void move(final ServerPlayer player) {
|
| 514 | if (!player.isRemoved()) {
|
| 515 | this.chunkMap.move(player);
|
| 516 | if (player.isReceivingWaypoints()) {
|
| 517 | this.level.getWaypointManager().updatePlayer(player);
|
| 518 | }
|
| 519 | }
|
| 520 | }
|
| 521 |
|
| 522 | public boolean hasEntityWithId(final int id) {
|
| 523 | return this.chunkMap.hasEntityWithId(id);
|
| 524 | }
|
| 525 |
|
| 526 | public void removeEntity(final Entity entity) {
|
| 527 | this.chunkMap.removeEntity(entity);
|
| 528 | }
|
| 529 |
|
| 530 | public void addEntity(final Entity entity) {
|
| 531 | this.chunkMap.addEntity(entity);
|
| 532 | }
|
| 533 |
|
| 534 | public void sendToTrackingPlayersAndSelf(final Entity entity, final Packet<? super ClientGamePacketListener> packet) {
|
| 535 | this.chunkMap.sendToTrackingPlayersAndSelf(entity, packet);
|
| 536 | }
|
| 537 |
|
| 538 | public void sendToTrackingPlayers(final Entity entity, final Packet<? super ClientGamePacketListener> packet) {
|
| 539 | this.chunkMap.sendToTrackingPlayers(entity, packet);
|
| 540 | }
|
| 541 |
|
| 542 | public void sendToTrackingPlayersFiltered(
|
| 543 | final Entity entity, final Packet<? super ClientGamePacketListener> packet, final Predicate<ServerPlayer> targetPredicate
|
| 544 | ) {
|
| 545 | this.chunkMap.sendToTrackingPlayersFiltered(entity, packet, targetPredicate);
|
| 546 | }
|
| 547 |
|
| 548 | public void setViewDistance(final int newDistance) {
|
| 549 | this.chunkMap.setServerViewDistance(newDistance);
|
| 550 | }
|
| 551 |
|
| 552 | public void setSimulationDistance(final int simulationDistance) {
|
| 553 | this.distanceManager.updateSimulationDistance(simulationDistance);
|
| 554 | }
|
| 555 |
|
| 556 | @Override
|
| 557 | public void setSpawnSettings(final boolean spawnEnemies) {
|
| 558 | this.spawnEnemies = spawnEnemies;
|
| 559 | }
|
| 560 |
|
| 561 | public String getChunkDebugData(final ChunkPos pos) {
|
| 562 | return this.chunkMap.getChunkDebugData(pos);
|
| 563 | }
|
| 564 |
|
| 565 | public SavedDataStorage getDataStorage() {
|
| 566 | return this.savedDataStorage;
|
| 567 | }
|
| 568 |
|
| 569 | public PoiManager getPoiManager() {
|
| 570 | return this.chunkMap.getPoiManager();
|
| 571 | }
|
| 572 |
|
| 573 | public ChunkScanAccess chunkScanner() {
|
| 574 | return this.chunkMap.chunkScanner();
|
| 575 | }
|
| 576 |
|
| 577 | @VisibleForDebug
|
| 578 | public NaturalSpawner.@Nullable SpawnState getLastSpawnState() {
|
| 579 | return this.lastSpawnState;
|
| 580 | }
|
| 581 |
|
| 582 | public void deactivateTicketsOnClosing() {
|
| 583 | this.ticketStorage.deactivateTicketsOnClosing();
|
| 584 | }
|
| 585 |
|
| 586 | public void onChunkReadyToSend(final ChunkHolder chunk) {
|
| 587 | if (chunk.hasChangesToBroadcast()) {
|
| 588 | this.chunkHoldersToBroadcast.add(chunk);
|
| 589 | }
|
| 590 | }
|
| 591 |
|
| 592 | private final class MainThreadExecutor extends BlockableEventLoop<Runnable> {
|
| 593 | private MainThreadExecutor(final Level level) {
|
| 594 | super("Chunk source main thread executor for " + level.dimension().identifier(), false);
|
| 595 | }
|
| 596 |
|
| 597 | @Override
|
| 598 | public Runnable wrapRunnable(final Runnable runnable) {
|
| 599 | return runnable;
|
| 600 | }
|
| 601 |
|
| 602 | @Override
|
| 603 | protected boolean shouldRun(final Runnable task) {
|
| 604 | return true;
|
| 605 | }
|
| 606 |
|
| 607 | @Override
|
| 608 | protected boolean scheduleExecutables() {
|
| 609 | return true;
|
| 610 | }
|
| 611 |
|
| 612 | @Override
|
| 613 | protected Thread getRunningThread() {
|
| 614 | return ServerChunkCache.this.mainThread;
|
| 615 | }
|
| 616 |
|
| 617 | @Override
|
| 618 | protected void doRunTask(final Runnable task) {
|
| 619 | Profiler.get().incrementCounter("runTask");
|
| 620 | super.doRunTask(task);
|
| 621 | }
|
| 622 |
|
| 623 | @Override
|
| 624 | protected boolean pollTask() {
|
| 625 | if (ServerChunkCache.this.runDistanceManagerUpdates()) {
|
| 626 | return true;
|
| 627 | }
|
| 628 |
|
| 629 | ServerChunkCache.this.lightEngine.tryScheduleUpdate();
|
| 630 | return super.pollTask();
|
| 631 | }
|
| 632 | }
|
| 633 | }
|
| 634 |
|