mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-06 18:50:51 +01:00
f179cfaff2
This patch adds a tool to find calls to getChunkAt which would load chunks, however it must be enabled by setting the startup flag -Dpaper.debug-sync-loads=true - To get a debug log for sync loads, the command is /paper syncloadinfo - To clear clear the currently stored sync load info, use /paper syncloadinfo clear
255 lines
11 KiB
Diff
255 lines
11 KiB
Diff
--- a/net/minecraft/server/level/ServerChunkCache.java
|
|
+++ b/net/minecraft/server/level/ServerChunkCache.java
|
|
@@ -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;
|
|
@@ -95,6 +102,64 @@
|
|
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
|
|
+ // 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);
|
|
+ }
|
|
+
|
|
+ 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
|
|
+
|
|
@Override
|
|
public ThreadedLevelLightEngine getLightEngine() {
|
|
return this.lightEngine;
|
|
@@ -138,7 +203,7 @@
|
|
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;
|
|
}
|
|
}
|
|
@@ -150,8 +215,9 @@
|
|
|
|
Objects.requireNonNull(completablefuture);
|
|
chunkproviderserver_b.managedBlock(completablefuture::isDone);
|
|
+ // com.destroystokyo.paper.io.SyncLoadFinder.logSyncLoad(this.level, x, z); // Paper - Add debug for sync chunk loads
|
|
ChunkResult<ChunkAccess> 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 +297,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 +324,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
|
|
@@ -279,7 +353,7 @@
|
|
return this.mainThreadProcessor.pollTask();
|
|
}
|
|
|
|
- boolean runDistanceManagerUpdates() {
|
|
+ public boolean runDistanceManagerUpdates() { // Paper - public
|
|
boolean flag = this.distanceManager.runAllUpdates(this.chunkMap);
|
|
boolean flag1 = this.chunkMap.promoteChunkMap();
|
|
|
|
@@ -309,18 +383,40 @@
|
|
|
|
@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");
|
|
- if (this.level.tickRateManager().runsNormally() || !tickChunks) {
|
|
+ if (this.level.tickRateManager().runsNormally() || !tickChunks || this.level.spigotConfig.unloadFrozenChunks) { // Spigot
|
|
this.distanceManager.purgeStaleTickets();
|
|
}
|
|
|
|
@@ -401,14 +497,22 @@
|
|
|
|
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;
|
|
+ // 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
|
|
+ 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,7 +524,7 @@
|
|
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);
|
|
}
|
|
|
|
@@ -541,10 +645,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 +728,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) {
|