diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 8ca984e42..a8edf2a27 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2820,91 +2820,80 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ */ final public function close($message = "", string $reason = "generic reason", bool $notify = true) : void{ if($this->isConnected() and !$this->closed){ + $ip = $this->networkSession->getIp(); + $port = $this->networkSession->getPort(); + $this->networkSession->onPlayerDestroyed($reason, $notify); + $this->networkSession = null; - try{ - $ip = $this->networkSession->getIp(); - $port = $this->networkSession->getPort(); - $this->networkSession->onPlayerDestroyed($reason, $notify); - $this->networkSession = null; + PermissionManager::getInstance()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this); + PermissionManager::getInstance()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this); - PermissionManager::getInstance()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this); - PermissionManager::getInstance()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this); + $this->stopSleep(); - $this->stopSleep(); + if($this->spawned){ + $ev = new PlayerQuitEvent($this, $message, $reason); + $ev->call(); + if($ev->getQuitMessage() != ""){ + $this->server->broadcastMessage($ev->getQuitMessage()); + } - if($this->spawned){ - $ev = new PlayerQuitEvent($this, $message, $reason); - $ev->call(); - if($ev->getQuitMessage() != ""){ - $this->server->broadcastMessage($ev->getQuitMessage()); + $this->save(); + } + + if($this->isValid()){ + foreach($this->usedChunks as $index => $d){ + Level::getXZ($index, $chunkX, $chunkZ); + $this->level->unregisterChunkLoader($this, $chunkX, $chunkZ); + foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){ + $entity->despawnFrom($this); } + unset($this->usedChunks[$index]); + } + } + $this->usedChunks = []; + $this->loadQueue = []; - try{ - $this->save(); - }catch(\Throwable $e){ - $this->server->getLogger()->critical("Failed to save player data for " . $this->getName()); - $this->server->getLogger()->logException($e); + if($this->loggedIn){ + $this->server->onPlayerLogout($this); + foreach($this->server->getOnlinePlayers() as $player){ + if(!$player->canSee($this)){ + $player->showPlayer($this); } } + $this->hiddenPlayers = []; + } - if($this->isValid()){ - foreach($this->usedChunks as $index => $d){ - Level::getXZ($index, $chunkX, $chunkZ); - $this->level->unregisterChunkLoader($this, $chunkX, $chunkZ); - foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){ - $entity->despawnFrom($this); - } - unset($this->usedChunks[$index]); - } - } - $this->usedChunks = []; - $this->loadQueue = []; + $this->removeAllWindows(true); + $this->windows = []; + $this->windowIndex = []; + $this->cursorInventory = null; + $this->craftingGrid = null; - if($this->loggedIn){ - $this->server->onPlayerLogout($this); - foreach($this->server->getOnlinePlayers() as $player){ - if(!$player->canSee($this)){ - $player->showPlayer($this); - } - } - $this->hiddenPlayers = []; - } + if($this->constructed){ + parent::close(); + }else{ + $this->closed = true; + } + $this->spawned = false; - $this->removeAllWindows(true); - $this->windows = []; - $this->windowIndex = []; - $this->cursorInventory = null; - $this->craftingGrid = null; + if($this->loggedIn){ + $this->loggedIn = false; + $this->server->removeOnlinePlayer($this); + } + $this->server->removePlayer($this); - if($this->constructed){ - parent::close(); - }else{ - $this->closed = true; - } - $this->spawned = false; + $this->server->getLogger()->info($this->getServer()->getLanguage()->translateString("pocketmine.player.logOut", [ + TextFormat::AQUA . $this->getName() . TextFormat::WHITE, + $ip, + $port, + $this->getServer()->getLanguage()->translateString($reason) + ])); - if($this->loggedIn){ - $this->loggedIn = false; - $this->server->removeOnlinePlayer($this); - } + $this->spawnPosition = null; - $this->server->getLogger()->info($this->getServer()->getLanguage()->translateString("pocketmine.player.logOut", [ - TextFormat::AQUA . $this->getName() . TextFormat::WHITE, - $ip, - $port, - $this->getServer()->getLanguage()->translateString($reason) - ])); - - $this->spawnPosition = null; - - if($this->perm !== null){ - $this->perm->clearPermissions(); - $this->perm = null; - } - }catch(\Throwable $e){ - $this->server->getLogger()->logException($e); - }finally{ - $this->server->removePlayer($this); + if($this->perm !== null){ + $this->perm->clearPermissions(); + $this->perm = null; } } }