public function unloadChunk($x, $z, $safe = true)
{
if ($safe === true and $this->isChunkInUse($x, $z)) {
return false;
}
$this->timings->doChunkUnload->startTiming();
$index = PHP_INT_SIZE === 8 ? ($x & 4294967295.0) << 32 | $z & 4294967295.0 : $x . ":" . $z;
$chunk = $this->getChunk($x, $z);
if ($chunk !== null) {
$this->server->getPluginManager()->callEvent($ev = new ChunkUnloadEvent($chunk));
if ($ev->isCancelled()) {
return false;
}
}
try {
if ($chunk !== null and $this->getAutoSave()) {
$this->provider->setChunk($x, $z, $chunk);
$this->provider->saveChunk($x, $z);
}
$this->provider->unloadChunk($x, $z, $safe);
} catch (\Exception $e) {
$logger = $this->server->getLogger();
$logger->error("Error when unloading a chunk: " . $e->getMessage());
if ($logger instanceof MainLogger) {
$logger->logException($e);
}
}
unset($this->chunks[$index]);
unset($this->usedChunks[$index]);
unset($this->chunkTickList[$index]);
Cache::remove("world:" . $this->getId() . ":{$index}");
$this->timings->doChunkUnload->stopTiming();
return true;
}