World: split populateChunk() into two functions

requestChunkPopulation() respects the queue size, orderChunkPopulation() does not.
requestChunkPopulation() should be used for non-essential generation (which mainly includes generation for player use).
orderChunkPopulation() should probably be used by plugins.
This commit is contained in:
Dylan K. Taylor 2020-12-17 23:49:37 +00:00
parent 48623f4e79
commit 1e737644de
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 33 additions and 4 deletions

View File

@ -724,7 +724,7 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->getWorld()->registerChunkLoader($this->chunkLoader, $X, $Z, true);
$this->getWorld()->registerChunkListener($this, $X, $Z);
if(!$this->getWorld()->populateChunk($X, $Z)){
if(!$this->getWorld()->requestChunkPopulation($X, $Z)){
continue;
}

View File

@ -2589,8 +2589,37 @@ class World implements ChunkManager{
}
}
public function populateChunk(int $x, int $z, bool $force = false) : bool{
if(isset($this->chunkPopulationQueue[$index = World::chunkHash($x, $z)]) or (count($this->chunkPopulationQueue) >= $this->chunkPopulationQueueSize and !$force)){
/**
* Attempts to initiate asynchronous generation/population of the target chunk, if it's currently reasonable to do
* so (and if it isn't already generated/populated).
*
* This method can fail for the following reasons:
* - The generation queue for this world is currently full (intended to prevent CPU overload with non-essential generation)
* - The target chunk is already being generated/populated
* - The target chunk is locked for use by another async operation (usually population)
*
* @return bool whether the chunk has been successfully populated already
* TODO: the return values don't make a lot of sense, but currently stuff depends on them :<
*/
public function requestChunkPopulation(int $chunkX, int $chunkZ) : bool{
if(count($this->chunkPopulationQueue) >= $this->chunkPopulationQueueSize){
return false;
}
return $this->orderChunkPopulation($chunkX, $chunkZ);
}
/**
* Initiates asynchronous generation/population of the target chunk, if it's not already generated/populated.
*
* This method can fail for the following reasons:
* - The target chunk is already being generated/populated
* - The target chunk is locked for use by another async operation (usually population)
*
* @return bool whether the chunk has been successfully populated already
* TODO: the return values don't make sense, but currently stuff depends on them :<
*/
public function orderChunkPopulation(int $x, int $z) : bool{
if(isset($this->chunkPopulationQueue[$index = World::chunkHash($x, $z)])){
return false;
}
for($xx = -1; $xx <= 1; ++$xx){

View File

@ -284,7 +284,7 @@ class WorldManager{
foreach((new ChunkSelector())->selectChunks(3, $centerX, $centerZ) as $index){
World::getXZ($index, $chunkX, $chunkZ);
$world->populateChunk($chunkX, $chunkZ, true);
$world->orderChunkPopulation($chunkX, $chunkZ);
}
}