NetworkSession: do not expose setPlayerInfo()

This commit is contained in:
Dylan K. Taylor 2020-04-16 01:55:52 +01:00
parent 9ba47f90d1
commit e86c243db5
2 changed files with 16 additions and 17 deletions

View File

@ -162,7 +162,11 @@ class NetworkSession{
$this->connectTime = time();
$this->setHandler(new LoginPacketHandler($this->server, $this));
$this->setHandler(new LoginPacketHandler($this->server, $this, function(PlayerInfo $info) : void{
$this->info = $info;
$this->logger->info("Player: " . TextFormat::AQUA . $info->getUsername() . TextFormat::RESET);
$this->logger->setPrefix($this->getLogPrefix());
}));
$this->manager->add($this);
$this->logger->info("Session opened");
@ -204,20 +208,6 @@ class NetworkSession{
return $this->info;
}
/**
* TODO: this shouldn't be accessible after the initial login phase
*
* @throws \InvalidStateException
*/
public function setPlayerInfo(PlayerInfo $info) : void{
if($this->info !== null){
throw new \InvalidStateException("Player info has already been set");
}
$this->info = $info;
$this->logger->info("Player: " . TextFormat::AQUA . $info->getUsername() . TextFormat::RESET);
$this->logger->setPrefix($this->getLogPrefix());
}
public function isConnected() : bool{
return $this->connected;
}

View File

@ -50,10 +50,19 @@ class LoginPacketHandler extends PacketHandler{
private $server;
/** @var NetworkSession */
private $session;
/**
* @var \Closure
* @phpstan-var \Closure(PlayerInfo) : void
*/
private $playerInfoConsumer;
public function __construct(Server $server, NetworkSession $session){
/**
* @phpstan-param \Closure(PlayerInfo) : void $playerInfoConsumer
*/
public function __construct(Server $server, NetworkSession $session, \Closure $playerInfoConsumer){
$this->session = $session;
$this->server = $server;
$this->playerInfoConsumer = $playerInfoConsumer;
}
public function handleLogin(LoginPacket $packet) : bool{
@ -117,7 +126,7 @@ class LoginPacketHandler extends PacketHandler{
}catch(\InvalidArgumentException $e){
throw BadPacketException::wrap($e, "Failed to parse login UUID");
}
$this->session->setPlayerInfo(new PlayerInfo(
($this->playerInfoConsumer)(new PlayerInfo(
$packet->extraData->displayName,
$uuid,
$skin,