--- a/net/minecraft/server/level/ServerChunkCache.java +++ b/net/minecraft/server/level/ServerChunkCache.java @@ -95,6 +95,16 @@ this.clearCache(); } + // CraftBukkit start - properly implement isChunkLoaded + public boolean isChunkLoaded(int chunkX, int chunkZ) { + ChunkHolder chunk = this.chunkMap.getUpdatingChunkIfPresent(ChunkPos.asLong(chunkX, chunkZ)); + if (chunk == null) { + return false; + } + return chunk.getFullChunkNow() != null; + } + // CraftBukkit end + @Override public ThreadedLevelLightEngine getLightEngine() { return this.lightEngine; @@ -138,20 +148,22 @@ if (k == this.lastChunkPos[l] && leastStatus == this.lastChunkStatus[l]) { ChunkAccess ichunkaccess = this.lastChunk[l]; - if (ichunkaccess != null || !create) { + if (ichunkaccess != null) { // CraftBukkit - the chunk can become accessible in the meantime TODO for non-null chunks it might also make sense to check that the chunk's state hasn't changed in the meantime return ichunkaccess; } } } gameprofilerfiller.incrementCounter("getChunkCacheMiss"); + this.level.timings.syncChunkLoadTimer.startTiming(); // Spigot CompletableFuture> completablefuture = this.getChunkFutureMainThread(x, z, leastStatus, create); ServerChunkCache.MainThreadExecutor chunkproviderserver_b = this.mainThreadProcessor; Objects.requireNonNull(completablefuture); chunkproviderserver_b.managedBlock(completablefuture::isDone); + this.level.timings.syncChunkLoadTimer.stopTiming(); // Spigot ChunkResult chunkresult = (ChunkResult) completablefuture.join(); - ChunkAccess ichunkaccess1 = (ChunkAccess) chunkresult.orElse((Object) null); + ChunkAccess ichunkaccess1 = (ChunkAccess) chunkresult.orElse(null); // CraftBukkit - decompile error if (ichunkaccess1 == null && create) { throw (IllegalStateException) Util.pauseInIde(new IllegalStateException("Chunk not there when requested: " + chunkresult.getError())); @@ -231,7 +243,15 @@ int l = ChunkLevel.byStatus(leastStatus); ChunkHolder playerchunk = this.getVisibleChunkIfPresent(k); - if (create) { + // CraftBukkit start - don't add new ticket for currently unloading chunk + boolean currentlyUnloading = false; + if (playerchunk != null) { + FullChunkStatus oldChunkState = ChunkLevel.fullStatus(playerchunk.oldTicketLevel); + FullChunkStatus currentChunkState = ChunkLevel.fullStatus(playerchunk.getTicketLevel()); + currentlyUnloading = (oldChunkState.isOrAfter(FullChunkStatus.FULL) && !currentChunkState.isOrAfter(FullChunkStatus.FULL)); + } + if (create && !currentlyUnloading) { + // CraftBukkit end this.distanceManager.addTicket(TicketType.UNKNOWN, chunkcoordintpair, l, chunkcoordintpair); if (this.chunkAbsent(playerchunk, l)) { ProfilerFiller gameprofilerfiller = Profiler.get(); @@ -250,7 +270,7 @@ } private boolean chunkAbsent(@Nullable ChunkHolder holder, int maxLevel) { - return holder == null || holder.getTicketLevel() > maxLevel; + return holder == null || holder.oldTicketLevel > maxLevel; // CraftBukkit using oldTicketLevel for isLoaded checks } @Override @@ -309,30 +329,58 @@ @Override public void close() throws IOException { - this.save(true); + // CraftBukkit start + this.close(true); + } + + public void close(boolean save) throws IOException { + if (save) { + this.save(true); + } + // CraftBukkit end this.dataStorage.close(); this.lightEngine.close(); this.chunkMap.close(); + } + + // CraftBukkit start - modelled on below + public void purgeUnload() { + ProfilerFiller gameprofilerfiller = Profiler.get(); + + gameprofilerfiller.push("purge"); + this.distanceManager.purgeStaleTickets(); + this.runDistanceManagerUpdates(); + gameprofilerfiller.popPush("unload"); + this.chunkMap.tick(() -> true); + gameprofilerfiller.pop(); + this.clearCache(); } + // CraftBukkit end @Override public void tick(BooleanSupplier shouldKeepTicking, boolean tickChunks) { ProfilerFiller gameprofilerfiller = Profiler.get(); gameprofilerfiller.push("purge"); + this.level.timings.doChunkMap.startTiming(); // Spigot if (this.level.tickRateManager().runsNormally() || !tickChunks) { this.distanceManager.purgeStaleTickets(); } this.runDistanceManagerUpdates(); + this.level.timings.doChunkMap.stopTiming(); // Spigot gameprofilerfiller.popPush("chunks"); if (tickChunks) { this.tickChunks(); + this.level.timings.tracker.startTiming(); // Spigot this.chunkMap.tick(); + this.level.timings.tracker.stopTiming(); // Spigot } + this.level.timings.doChunkUnload.startTiming(); // Spigot gameprofilerfiller.popPush("unload"); this.chunkMap.tick(shouldKeepTicking); + this.level.timings.doChunkUnload.stopTiming(); // Spigot gameprofilerfiller.pop(); this.clearCache(); } @@ -401,14 +449,14 @@ this.lastSpawnState = spawnercreature_d; profiler.popPush("spawnAndTick"); - boolean flag = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING); + boolean flag = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING) && !this.level.players().isEmpty(); // CraftBukkit int k = this.level.getGameRules().getInt(GameRules.RULE_RANDOMTICKING); List list1; if (flag && (this.spawnEnemies || this.spawnFriendlies)) { - boolean flag1 = this.level.getLevelData().getGameTime() % 400L == 0L; + boolean flag1 = this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) != 0L && this.level.getLevelData().getGameTime() % this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) == 0L; // CraftBukkit - list1 = NaturalSpawner.getFilteredSpawningCategories(spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag1); + list1 = NaturalSpawner.getFilteredSpawningCategories(spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag1, this.level); // CraftBukkit } else { list1 = List.of(); } @@ -420,12 +468,14 @@ ChunkPos chunkcoordintpair = chunk.getPos(); chunk.incrementInhabitedTime(timeDelta); - if (!list1.isEmpty() && this.level.getWorldBorder().isWithinBounds(chunkcoordintpair)) { + if (!list1.isEmpty() && this.level.getWorldBorder().isWithinBounds(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkcoordintpair, true)) { // Spigot NaturalSpawner.spawnForChunk(this.level, chunk, spawnercreature_d, list1); } if (this.level.shouldTickBlocksAt(chunkcoordintpair.toLong())) { + this.level.timings.doTickTiles.startTiming(); // Spigot this.level.tickChunk(chunk, k); + this.level.timings.doTickTiles.stopTiming(); // Spigot } } @@ -541,10 +591,16 @@ @Override public void setSpawnSettings(boolean spawnMonsters) { - this.spawnEnemies = spawnMonsters; - this.spawnFriendlies = this.spawnFriendlies; + // CraftBukkit start + this.setSpawnSettings(spawnMonsters, this.spawnFriendlies); } + public void setSpawnSettings(boolean flag, boolean spawnFriendlies) { + this.spawnEnemies = flag; + this.spawnFriendlies = spawnFriendlies; + // CraftBukkit end + } + public String getChunkDebugData(ChunkPos pos) { return this.chunkMap.getChunkDebugData(pos); } @@ -618,14 +674,20 @@ } @Override - protected boolean pollTask() { + // CraftBukkit start - process pending Chunk loadCallback() and unloadCallback() after each run task + public boolean pollTask() { + try { if (ServerChunkCache.this.runDistanceManagerUpdates()) { return true; } else { ServerChunkCache.this.lightEngine.tryScheduleUpdate(); return super.pollTask(); } + } finally { + ServerChunkCache.this.chunkMap.callbackExecutor.run(); } + // CraftBukkit end + } } private static record ChunkAndHolder(LevelChunk chunk, ChunkHolder holder) {