/**
* @param string $message Message to be broadcasted
* @param string $reason Reason showed in console
*/
public function close($message = "", $reason = "generic reason")
{
foreach ($this->tasks as $task) {
$task->cancel();
}
$this->tasks = [];
if ($this->connected and !$this->closed) {
if ($reason != "") {
$pk = new DisconnectPacket();
$pk->message = $reason;
$this->directDataPacket($pk);
}
$this->connected = false;
if ($this->username != "") {
$this->server->getPluginManager()->callEvent($ev = new PlayerQuitEvent($this, $message));
if ($this->server->getAutoSave() and $this->loggedIn === true) {
$this->save();
}
}
foreach ($this->server->getOnlinePlayers() as $player) {
if (!$player->canSee($this)) {
$player->showPlayer($this);
}
}
$this->hiddenPlayers = [];
foreach ($this->windowIndex as $window) {
$this->removeWindow($window);
}
$this->interface->close($this, $reason);
$chunkX = $chunkZ = null;
foreach ($this->usedChunks as $index => $d) {
if (PHP_INT_SIZE === 8) {
$chunkX = $index >> 32 << 32 >> 32;
$chunkZ = ($index & 0xffffffff) << 32 >> 32;
} else {
list($chunkX, $chunkZ) = explode(":", $index);
$chunkX = (int) $chunkX;
$chunkZ = (int) $chunkZ;
}
$this->level->freeChunk($chunkX, $chunkZ, $this);
unset($this->usedChunks[$index]);
}
parent::close();
if ($this->loggedIn) {
$this->server->removeOnlinePlayer($this);
}
$this->loggedIn = false;
if (isset($ev) and $this->username != "" and $this->spawned !== false and $ev->getQuitMessage() != "") {
$this->server->broadcastMessage($ev->getQuitMessage());
}
$this->server->getPluginManager()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this);
$this->spawned = false;
$this->server->getLogger()->info(TextFormat::AQUA . $this->username . TextFormat::WHITE . "/" . $this->ip . " logged out due to " . str_replace(["\n", "\r"], [" ", ""], $reason));
$this->windows = new \SplObjectStorage();
$this->windowIndex = [];
$this->usedChunks = [];
$this->loadQueue = [];
$this->hasSpawned = [];
$this->spawnPosition = null;
unset($this->buffer);
}
$this->perm->clearPermissions();
$this->server->removePlayer($this);
}