/**
* Sends, if available, the next ordered chunk to the client
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
*/
public function sendNextChunk()
{
if ($this->connected === false or !isset($this->chunkLoadTask)) {
return;
}
if (count($this->loadQueue) === 0) {
$this->chunkLoadTask->setNextRun($this->chunkLoadTask->getNextRun() + 30);
} else {
$count = 0;
$limit = (int) $this->server->getProperty("chunk-sending.per-tick", 4);
foreach ($this->loadQueue as $index => $distance) {
if ($count >= $limit) {
break;
}
++$count;
$X = null;
$Z = null;
Level::getXZ($index, $X, $Z);
if (!$this->getLevel()->isChunkPopulated($X, $Z)) {
continue;
}
unset($this->loadQueue[$index]);
$this->usedChunks[$index] = [false, 0];
$this->getLevel()->useChunk($X, $Z, $this);
$this->getLevel()->requestChunk($X, $Z, $this, LevelProvider::ORDER_ZXY);
}
}
if (count($this->usedChunks) < 16 and $this->spawned === true) {
$this->blocked = true;
} elseif ($this->spawned === true) {
$this->blocked = false;
//TODO: reason of block to revert
}
if (count($this->usedChunks) >= 56 and $this->spawned === false) {
$spawned = 0;
foreach ($this->usedChunks as $d) {
if ($d[0] === true) {
$spawned++;
}
}
if ($spawned < 56) {
return;
}
$this->spawned = true;
$this->blocked = false;
$pk = new SetTimePacket();
$pk->time = $this->getLevel()->getTime();
$pk->started = $this->getLevel()->stopTime == false;
$this->dataPacket($pk);
$pos = new Position($this->x, $this->y, $this->z, $this->getLevel());
$pos = $this->getLevel()->getSafeSpawn($pos);
$this->server->getPluginManager()->callEvent($ev = new PlayerRespawnEvent($this, $pos));
$this->teleport($ev->getRespawnPosition());
$this->sendSettings();
$this->inventory->sendContents($this);
$this->inventory->sendArmorContents($this);
$this->spawnToAll();
$this->server->getPluginManager()->callEvent($ev = new PlayerJoinEvent($this, TextFormat::YELLOW . $this->getName() . " joined the game"));
if (strlen(trim($ev->getJoinMessage())) > 0) {
$this->server->broadcastMessage($ev->getJoinMessage());
}
if ($this->server->getUpdater()->hasUpdate() and $this->hasPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE)) {
$this->server->getUpdater()->showPlayerUpdate($this);
}
}
}