World->getSafeSpawn() now throws if the target is in ungenerated terrain, instead of silently returning the default

this WILL cause crashes in some cases on slower machines (#2724), but it's
better than randomly spawning at the top of the map.
This commit is contained in:
Dylan K. Taylor 2020-11-26 19:36:33 +00:00
parent 4520e425c1
commit fd99445c5b

View File

@ -2371,6 +2371,9 @@ class World implements ChunkManager{
return abs($X - $spawnX) <= 1 and abs($Z - $spawnZ) <= 1; return abs($X - $spawnX) <= 1 and abs($Z - $spawnZ) <= 1;
} }
/**
* @throws WorldException if the terrain is not generated
*/
public function getSafeSpawn(?Vector3 $spawn = null) : Position{ public function getSafeSpawn(?Vector3 $spawn = null) : Position{
if(!($spawn instanceof Vector3) or $spawn->y < 1){ if(!($spawn instanceof Vector3) or $spawn->y < 1){
$spawn = $this->getSpawnLocation(); $spawn = $this->getSpawnLocation();
@ -2379,10 +2382,11 @@ class World implements ChunkManager{
$max = $this->worldHeight; $max = $this->worldHeight;
$v = $spawn->floor(); $v = $spawn->floor();
$chunk = $this->getOrLoadChunkAtPosition($v, false); $chunk = $this->getOrLoadChunkAtPosition($v, false);
if($chunk === null || !$chunk->isGenerated()){
throw new WorldException("Cannot find a safe spawn point in non-generated terrain");
}
$x = (int) $v->x; $x = (int) $v->x;
$y = $v->y;
$z = (int) $v->z; $z = (int) $v->z;
if($chunk !== null and $chunk->isGenerated()){
$y = (int) min($max - 2, $v->y); $y = (int) min($max - 2, $v->y);
$wasAir = $this->getBlockAt($x, $y - 1, $z)->getId() === BlockLegacyIds::AIR; //TODO: bad hack, clean up $wasAir = $this->getBlockAt($x, $y - 1, $z)->getId() === BlockLegacyIds::AIR; //TODO: bad hack, clean up
for(; $y > 0; --$y){ for(; $y > 0; --$y){
@ -2405,7 +2409,6 @@ class World implements ChunkManager{
++$y; ++$y;
} }
} }
}
return new Position($spawn->x, $y, $spawn->z, $this); return new Position($spawn->x, $y, $spawn->z, $this);
} }