mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-13 01:09:44 +00:00
World: allow registering unload hooks
this will be used for cache management when worlds get unloaded.
This commit is contained in:
parent
bb11cbd89c
commit
3ce9a4801f
@ -246,6 +246,11 @@ class World implements ChunkManager{
|
||||
|
||||
/** @var bool */
|
||||
private $closed = false;
|
||||
/**
|
||||
* @var \Closure[]
|
||||
* @phpstan-var array<int, \Closure() : void>
|
||||
*/
|
||||
private $unloadCallbacks = [];
|
||||
|
||||
/** @var BlockLightUpdate|null */
|
||||
private $blockLightUpdate = null;
|
||||
@ -405,6 +410,11 @@ class World implements ChunkManager{
|
||||
throw new \InvalidStateException("Tried to close a world which is already closed");
|
||||
}
|
||||
|
||||
foreach($this->unloadCallbacks as $callback){
|
||||
$callback();
|
||||
}
|
||||
$this->unloadCallbacks = [];
|
||||
|
||||
foreach($this->chunks as $chunk){
|
||||
$this->unloadChunk($chunk->getX(), $chunk->getZ(), false);
|
||||
}
|
||||
@ -421,6 +431,14 @@ class World implements ChunkManager{
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
public function addOnUnloadCallback(\Closure $callback) : void{
|
||||
$this->unloadCallbacks[spl_object_id($callback)] = $callback;
|
||||
}
|
||||
|
||||
public function removeOnUnloadCallback(\Closure $callback) : void{
|
||||
unset($this->unloadCallbacks[spl_object_id($callback)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Player[]|null $players
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user