Skip to content

Commit

Permalink
fix: fix deadlock in finding world spawn point
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Oct 28, 2024
1 parent 0114e0b commit 024ec3c
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions server/src/main/java/org/allaymc/server/world/AllayWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,24 @@ protected void checkFirstTick() {
}
isFirstTick = true;

// Check spawn point
if (!isSafeStandingPos(new Position3i(worldData.getSpawnPoint(), getOverWorld()))) {
var newSpawnPoint = getOverWorld().findSuitableGroundPosAround(this::isSafeStandingPos, 0, 0, 32);
if (newSpawnPoint == null) {
log.warn("Cannot find a safe spawn point in the overworld dimension of world {}", worldData.getName());
newSpawnPoint = new Vector3i(0, getOverWorld().getHeight(0, 0) + 1, 0);
}
worldData.setSpawnPoint(newSpawnPoint);
}

if (Server.SETTINGS.worldSettings().loadSpawnPointChunks()) {
// Add spawn point chunk loader
getOverWorld().getChunkService().addChunkLoader(new SpawnPointChunkLoader());
}

// Shouldn't block world tick poll here, otherwise there will be a deadlock because
// Dimension#findSuitableGroundPosAround() -wait-> Dimension#getBlockState() -wait-> ChunkService#getOrLoadChunkSync()
// -wait-> WorldGenerator#generateChunk(), and WorldGenerator should be ticked in order to generate chunk normally
Thread.ofVirtual().name("World " + worldData.getName() + " Spawn Point Finding Thread").start(() -> {
if (!isSafeStandingPos(new Position3i(worldData.getSpawnPoint(), getOverWorld()))) {
var newSpawnPoint = getOverWorld().findSuitableGroundPosAround(this::isSafeStandingPos, 0, 0, 32);
if (newSpawnPoint == null) {
log.warn("Cannot find a safe spawn point in the overworld dimension of world {}", worldData.getName());
newSpawnPoint = new Vector3i(0, getOverWorld().getHeight(0, 0) + 1, 0);
}
worldData.setSpawnPoint(newSpawnPoint);
}
});
}

protected boolean isSafeStandingPos(Position3ic pos) {
Expand Down

0 comments on commit 024ec3c

Please sign in to comment.