public function getSafeSpawn($spawn = null)
{
if (!$spawn instanceof Vector3 or $spawn->y < 1) {
$spawn = $this->getSpawnLocation();
}
if ($spawn instanceof Vector3) {
$v = $spawn->floor();
$chunk = $this->getChunk($v->x >> 4, $v->z >> 4, false);
$x = $v->x & 0xf;
$z = $v->z & 0xf;
if ($chunk !== null) {
$y = (int) min(126, $v->y);
$wasAir = $chunk->getBlockId($x, $y - 1, $z) === 0;
for (; $y > 0; --$y) {
$b = $chunk->getFullBlock($x, $y, $z);
$block = Block::get($b >> 4, $b & 0xf);
if ($this->isFullBlock($block)) {
if ($wasAir) {
$y++;
break;
}
} else {
$wasAir = true;
}
}
for (; $y >= 0 and $y < 128; ++$y) {
$b = $chunk->getFullBlock($x, $y + 1, $z);
$block = Block::get($b >> 4, $b & 0xf);
if (!$this->isFullBlock($block)) {
$b = $chunk->getFullBlock($x, $y, $z);
$block = Block::get($b >> 4, $b & 0xf);
if (!$this->isFullBlock($block)) {
return new Position($spawn->x, $y === (int) $spawn->y ? $spawn->y : $y, $spawn->z, $this);
}
} else {
++$y;
}
}
$v->y = $y;
}
return new Position($spawn->x, $v->y, $spawn->z, $this);
}
return false;
}