From 7d59bafd834d03f45fa41b45bdab9607c539ccab Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 14 Jan 2023 17:42:17 +0000 Subject: [PATCH] World: added requestSafeSpawn() (async) this simplifies usages of safe spawns, since the caller doesn't need to know which chunks will be needed for the spawn to be selected. We'll need this in the future, because safe spawns may also get diverted horizontally as well as vertically, which might require loading adjacent chunks as well as the chunk the position is actually in. --- src/world/World.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/world/World.php b/src/world/World.php index f5e51b799..ced320550 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -2836,6 +2836,36 @@ class World implements ChunkManager{ } /** + * Requests a safe spawn position near the given position, or near the world's spawn position if not provided. + * Terrain near the position will be loaded or generated as needed. + * + * @return Promise Resolved to a Position object, or rejected if the world is unloaded. + * @phpstan-return Promise + */ + public function requestSafeSpawn(?Vector3 $spawn = null) : Promise{ + $resolver = new PromiseResolver(); + $spawn ??= $this->getSpawnLocation(); + /* + * TODO: this relies on the assumption that getSafeSpawn() will only alter the Y coordinate of the provided + * position, which is currently OK, but might be a problem in the future. + */ + $this->requestChunkPopulation($spawn->getFloorX() >> Chunk::COORD_BIT_SIZE, $spawn->getFloorZ() >> Chunk::COORD_BIT_SIZE, null)->onCompletion( + function() use ($spawn, $resolver) : void{ + $spawn = $this->getSafeSpawn($spawn); + $resolver->resolve($spawn); + }, + function() use ($resolver) : void{ + $resolver->reject(); + } + ); + + return $resolver->getPromise(); + } + + /** + * Returns a safe spawn position near the given position, or near the world's spawn position if not provided. + * This function will throw an exception if the terrain is not already generated in advance. + * * @throws WorldException if the terrain is not generated */ public function getSafeSpawn(?Vector3 $spawn = null) : Position{