private function unloadChunk($x, $z, Level $level = null)
{
$level = $level === null ? $this->level : $level;
$index = Level::chunkHash($x, $z);
if (isset($this->usedChunks[$index])) {
foreach ($level->getChunkEntities($x, $z) as $entity) {
if ($entity !== $this) {
$entity->despawnFrom($this);
}
}
unset($this->usedChunks[$index]);
}
$level->unregisterChunkLoader($this, $x, $z);
unset($this->loadQueue[$index]);
}