2024-12-11 22:26:55 +01:00
--- a/net/minecraft/server/level/ServerChunkCache.java
+++ b/net/minecraft/server/level/ServerChunkCache.java
2016-03-29 02:55:47 +02:00
@@ -74,6 +74,13 @@
@Nullable
@VisibleForDebug
private NaturalSpawner.SpawnState lastSpawnState;
+ // Paper start
+ private final ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<net.minecraft.world.level.chunk.LevelChunk> fullChunks = new ca.spottedleaf.concurrentutil.map.ConcurrentLong2ReferenceChainedHashTable<>();
+ public int getFullChunksCount() {
+ return this.fullChunks.size();
+ }
+ long chunkFutureAwaitCounter;
+ // Paper end
public ServerChunkCache(ServerLevel world, LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, StructureTemplateManager structureTemplateManager, Executor workerExecutor, ChunkGenerator chunkGenerator, int viewDistance, int simulationDistance, boolean dsync, ChunkProgressListener worldGenerationProgressListener, ChunkStatusUpdateListener chunkStatusChangeListener, Supplier<DimensionDataStorage> persistentStateManagerFactory) {
this.level = world;
2019-07-19 12:29:14 +02:00
@@ -95,6 +102,64 @@
2020-06-06 11:23:46 +02:00
this.clearCache();
2019-07-19 12:29:14 +02:00
}
2020-06-06 11:23:46 +02:00
+ // CraftBukkit start - properly implement isChunkLoaded
+ public boolean isChunkLoaded(int chunkX, int chunkZ) {
2024-12-11 22:26:55 +01:00
+ ChunkHolder chunk = this.chunkMap.getUpdatingChunkIfPresent(ChunkPos.asLong(chunkX, chunkZ));
2020-06-06 11:23:46 +02:00
+ if (chunk == null) {
+ return false;
+ }
2022-03-27 07:24:06 +02:00
+ return chunk.getFullChunkNow() != null;
2020-06-06 11:23:46 +02:00
+ }
+ // CraftBukkit end
2016-03-29 02:55:47 +02:00
+ // Paper start
+ public void addLoadedChunk(LevelChunk chunk) {
+ this.fullChunks.put(chunk.coordinateKey, chunk);
+ }
+
+ public void removeLoadedChunk(LevelChunk chunk) {
+ this.fullChunks.remove(chunk.coordinateKey);
+ }
+
+ @Nullable
+ public ChunkAccess getChunkAtImmediately(int x, int z) {
+ ChunkHolder holder = this.chunkMap.getVisibleChunkIfPresent(ChunkPos.asLong(x, z));
+ if (holder == null) {
+ return null;
+ }
+
+ return holder.getLatestChunk();
+ }
+
+ public <T> void addTicketAtLevel(TicketType<T> ticketType, ChunkPos chunkPos, int ticketLevel, T identifier) {
+ this.distanceManager.addTicket(ticketType, chunkPos, ticketLevel, identifier);
2019-07-19 12:29:14 +02:00
+ }
+
2016-03-29 02:55:47 +02:00
+ public <T> void removeTicketAtLevel(TicketType<T> ticketType, ChunkPos chunkPos, int ticketLevel, T identifier) {
+ this.distanceManager.removeTicket(ticketType, chunkPos, ticketLevel, identifier);
+ }
+
+ // "real" get chunk if loaded
+ // Note: Partially copied from the getChunkAt method below
+ @Nullable
+ public LevelChunk getChunkAtIfCachedImmediately(int x, int z) {
+ long k = ChunkPos.asLong(x, z);
+
+ // Note: Bypass cache since we need to check ticket level, and to make this MT-Safe
+
+ ChunkHolder playerChunk = this.getVisibleChunkIfPresent(k);
+ if (playerChunk == null) {
+ return null;
+ }
+
+ return playerChunk.getFullChunkNowUnchecked();
+ }
+
+ @Nullable
+ public LevelChunk getChunkAtIfLoadedImmediately(int x, int z) {
+ return this.fullChunks.get(ChunkPos.asLong(x, z));
+ }
+ // Paper end
2020-06-06 11:23:46 +02:00
+
@Override
2024-12-11 22:26:55 +01:00
public ThreadedLevelLightEngine getLightEngine() {
2020-06-06 11:23:46 +02:00
return this.lightEngine;
2016-03-03 11:00:11 +01:00
@@ -138,7 +203,7 @@
2024-12-11 22:26:55 +01:00
if (k == this.lastChunkPos[l] && leastStatus == this.lastChunkStatus[l]) {
ChunkAccess ichunkaccess = this.lastChunk[l];
2024-04-23 17:15:00 +02:00
2024-12-11 22:26:55 +01:00
- if (ichunkaccess != null || !create) {
2019-05-16 01:11:20 +02:00
+ 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;
}
}
2019-07-19 12:29:14 +02:00
@@ -150,8 +215,9 @@
2024-04-23 17:15:00 +02:00
Objects.requireNonNull(completablefuture);
chunkproviderserver_b.managedBlock(completablefuture::isDone);
2019-07-19 12:29:14 +02:00
+ // com.destroystokyo.paper.io.SyncLoadFinder.logSyncLoad(this.level, x, z); // Paper - Add debug for sync chunk loads
2024-12-11 22:26:55 +01:00
ChunkResult<ChunkAccess> chunkresult = (ChunkResult) completablefuture.join();
- ChunkAccess ichunkaccess1 = (ChunkAccess) chunkresult.orElse((Object) null);
+ ChunkAccess ichunkaccess1 = (ChunkAccess) chunkresult.orElse(null); // CraftBukkit - decompile error
2024-04-23 17:15:00 +02:00
2024-12-11 22:26:55 +01:00
if (ichunkaccess1 == null && create) {
throw (IllegalStateException) Util.pauseInIde(new IllegalStateException("Chunk not there when requested: " + chunkresult.getError()));
2019-07-19 12:29:14 +02:00
@@ -231,7 +297,15 @@
2024-12-11 22:26:55 +01:00
int l = ChunkLevel.byStatus(leastStatus);
ChunkHolder playerchunk = this.getVisibleChunkIfPresent(k);
2019-05-16 01:11:20 +02:00
2024-12-11 22:26:55 +01:00
- if (create) {
2019-05-16 01:11:20 +02:00
+ // CraftBukkit start - don't add new ticket for currently unloading chunk
+ boolean currentlyUnloading = false;
+ if (playerchunk != null) {
2023-06-07 17:30:00 +02:00
+ FullChunkStatus oldChunkState = ChunkLevel.fullStatus(playerchunk.oldTicketLevel);
+ FullChunkStatus currentChunkState = ChunkLevel.fullStatus(playerchunk.getTicketLevel());
+ currentlyUnloading = (oldChunkState.isOrAfter(FullChunkStatus.FULL) && !currentChunkState.isOrAfter(FullChunkStatus.FULL));
2019-05-16 01:11:20 +02:00
+ }
2024-12-11 22:26:55 +01:00
+ if (create && !currentlyUnloading) {
2019-05-16 01:11:20 +02:00
+ // CraftBukkit end
2021-11-21 23:00:00 +01:00
this.distanceManager.addTicket(TicketType.UNKNOWN, chunkcoordintpair, l, chunkcoordintpair);
if (this.chunkAbsent(playerchunk, l)) {
2024-12-11 22:26:55 +01:00
ProfilerFiller gameprofilerfiller = Profiler.get();
2019-07-19 12:29:14 +02:00
@@ -250,7 +324,7 @@
2024-06-15 08:47:43 +02:00
}
2024-12-11 22:26:55 +01:00
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
2024-06-15 08:47:43 +02:00
}
@Override
2019-07-19 12:29:14 +02:00
@@ -279,7 +353,7 @@
2016-03-29 02:55:47 +02:00
return this.mainThreadProcessor.pollTask();
}
- boolean runDistanceManagerUpdates() {
+ public boolean runDistanceManagerUpdates() { // Paper - public
boolean flag = this.distanceManager.runAllUpdates(this.chunkMap);
boolean flag1 = this.chunkMap.promoteChunkMap();
2019-07-19 12:29:14 +02:00
@@ -309,18 +383,40 @@
2019-07-29 08:36:51 +02:00
@Override
public void close() throws IOException {
- this.save(true);
+ // CraftBukkit start
2024-12-11 22:26:55 +01:00
+ this.close(true);
2019-07-29 08:36:51 +02:00
+ }
+
+ public void close(boolean save) throws IOException {
+ if (save) {
+ this.save(true);
+ }
+ // CraftBukkit end
2024-10-22 17:15:00 +02:00
this.dataStorage.close();
2019-07-29 08:36:51 +02:00
this.lightEngine.close();
2021-06-11 07:00:00 +02:00
this.chunkMap.close();
2018-01-14 23:36:02 +01:00
}
2019-04-23 04:00:00 +02:00
+ // CraftBukkit start - modelled on below
+ public void purgeUnload() {
2024-12-11 22:26:55 +01:00
+ ProfilerFiller gameprofilerfiller = Profiler.get();
2024-10-22 17:15:00 +02:00
+
+ gameprofilerfiller.push("purge");
2021-11-21 23:00:00 +01:00
+ this.distanceManager.purgeStaleTickets();
+ this.runDistanceManagerUpdates();
2024-10-22 17:15:00 +02:00
+ gameprofilerfiller.popPush("unload");
2021-11-21 23:00:00 +01:00
+ this.chunkMap.tick(() -> true);
2024-10-22 17:15:00 +02:00
+ gameprofilerfiller.pop();
2019-05-27 22:30:00 +02:00
+ this.clearCache();
2018-01-14 23:36:02 +01:00
+ }
2018-08-26 04:00:00 +02:00
+ // CraftBukkit end
2018-01-14 23:36:02 +01:00
+
2021-06-11 07:00:00 +02:00
@Override
2024-12-11 22:26:55 +01:00
public void tick(BooleanSupplier shouldKeepTicking, boolean tickChunks) {
2013-01-10 06:18:11 +01:00
ProfilerFiller gameprofilerfiller = Profiler.get();
gameprofilerfiller.push("purge");
2024-05-18 03:39:58 +02:00
- if (this.level.tickRateManager().runsNormally() || !tickChunks) {
+ if (this.level.tickRateManager().runsNormally() || !tickChunks || this.level.spigotConfig.unloadFrozenChunks) { // Spigot
2013-01-10 06:18:11 +01:00
this.distanceManager.purgeStaleTickets();
}
2019-07-19 12:29:14 +02:00
@@ -401,14 +497,22 @@
2024-10-22 17:15:00 +02:00
this.lastSpawnState = spawnercreature_d;
2024-12-11 22:26:55 +01:00
profiler.popPush("spawnAndTick");
2024-10-22 17:15:00 +02:00
- 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;
2021-11-21 23:00:00 +01:00
2024-10-22 17:15:00 +02:00
if (flag && (this.spawnEnemies || this.spawnFriendlies)) {
- boolean flag1 = this.level.getLevelData().getGameTime() % 400L == 0L;
2018-01-14 23:36:02 +01:00
+ // Paper start - PlayerNaturallySpawnCreaturesEvent
+ for (ServerPlayer entityPlayer : this.level.players()) {
+ int chunkRange = Math.min(level.spigotConfig.mobSpawnRange, entityPlayer.getBukkitEntity().getViewDistance());
+ chunkRange = Math.min(chunkRange, 8);
+ entityPlayer.playerNaturallySpawnedEvent = new com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent(entityPlayer.getBukkitEntity(), (byte) chunkRange);
+ entityPlayer.playerNaturallySpawnedEvent.callEvent();
+ }
+ // Paper end - PlayerNaturallySpawnCreaturesEvent
2024-10-22 17:15:00 +02:00
+ 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
2019-05-14 02:00:00 +02:00
2024-12-11 22:26:55 +01:00
- list1 = NaturalSpawner.getFilteredSpawningCategories(spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag1);
+ list1 = NaturalSpawner.getFilteredSpawningCategories(spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag1, this.level); // CraftBukkit
2024-10-22 17:15:00 +02:00
} else {
list1 = List.of();
}
2019-07-19 12:29:14 +02:00
@@ -420,7 +524,7 @@
2013-06-21 09:29:54 +02:00
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);
2013-01-10 06:18:11 +01:00
}
2019-07-19 12:29:14 +02:00
@@ -541,10 +645,16 @@
2024-10-22 17:15:00 +02:00
@Override
2024-12-11 22:26:55 +01:00
public void setSpawnSettings(boolean spawnMonsters) {
- this.spawnEnemies = spawnMonsters;
- this.spawnFriendlies = this.spawnFriendlies;
2024-10-22 17:15:00 +02:00
+ // CraftBukkit start
2024-12-11 22:26:55 +01:00
+ this.setSpawnSettings(spawnMonsters, this.spawnFriendlies);
}
2024-10-22 17:15:00 +02:00
+ public void setSpawnSettings(boolean flag, boolean spawnFriendlies) {
2024-12-11 22:26:55 +01:00
+ this.spawnEnemies = flag;
2024-10-22 17:15:00 +02:00
+ this.spawnFriendlies = spawnFriendlies;
+ // CraftBukkit end
2024-12-11 22:26:55 +01:00
+ }
+
public String getChunkDebugData(ChunkPos pos) {
return this.chunkMap.getChunkDebugData(pos);
2024-10-22 17:15:00 +02:00
}
2019-07-19 12:29:14 +02:00
@@ -618,14 +728,20 @@
2021-06-11 07:00:00 +02:00
}
2019-07-13 20:19:44 +02:00
@Override
2021-11-21 23:00:00 +01:00
- protected boolean pollTask() {
2019-07-13 20:19:44 +02:00
+ // CraftBukkit start - process pending Chunk loadCallback() and unloadCallback() after each run task
2021-11-21 23:00:00 +01:00
+ public boolean pollTask() {
2019-07-13 20:19:44 +02:00
+ try {
2024-12-11 22:26:55 +01:00
if (ServerChunkCache.this.runDistanceManagerUpdates()) {
2019-07-13 20:19:44 +02:00
return true;
} else {
2024-12-11 22:26:55 +01:00
ServerChunkCache.this.lightEngine.tryScheduleUpdate();
2021-11-21 23:00:00 +01:00
return super.pollTask();
2019-07-13 20:19:44 +02:00
}
+ } finally {
2024-12-11 22:26:55 +01:00
+ ServerChunkCache.this.chunkMap.callbackExecutor.run();
2019-07-13 20:19:44 +02:00
}
2024-12-11 22:26:55 +01:00
+ // CraftBukkit end
+ }
2019-07-13 20:19:44 +02:00
}
2021-11-21 23:00:00 +01:00
2024-12-11 22:26:55 +01:00
private static record ChunkAndHolder(LevelChunk chunk, ChunkHolder holder) {