Fixed server crash on invalid gamemode in SetPlayerGameTypePacket

this really ought to be detected at the decode layer, but right now that's a bit difficult ...
This commit is contained in:
Dylan K. Taylor 2021-03-28 21:11:07 +01:00
parent 7fe6815f7c
commit 25998720ce
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 4 additions and 4 deletions

View File

@ -91,7 +91,7 @@ class TypeConverter{
} }
} }
public function protocolGameModeToCore(int $gameMode) : GameMode{ public function protocolGameModeToCore(int $gameMode) : ?GameMode{
switch($gameMode){ switch($gameMode){
case ProtocolGameMode::SURVIVAL: case ProtocolGameMode::SURVIVAL:
return GameMode::SURVIVAL(); return GameMode::SURVIVAL();
@ -103,7 +103,7 @@ class TypeConverter{
case ProtocolGameMode::SURVIVAL_VIEWER: case ProtocolGameMode::SURVIVAL_VIEWER:
return GameMode::SPECTATOR(); return GameMode::SPECTATOR();
default: default:
throw new \UnexpectedValueException("Unmapped protocol game mode $gameMode"); return null;
} }
} }

View File

@ -667,8 +667,8 @@ class InGamePacketHandler extends PacketHandler{
} }
public function handleSetPlayerGameType(SetPlayerGameTypePacket $packet) : bool{ public function handleSetPlayerGameType(SetPlayerGameTypePacket $packet) : bool{
$converter = TypeConverter::getInstance(); $gameMode = TypeConverter::getInstance()->protocolGameModeToCore($packet->gamemode);
if(!$converter->protocolGameModeToCore($packet->gamemode)->equals($this->player->getGamemode())){ if($gameMode === null || !$gameMode->equals($this->player->getGamemode())){
//Set this back to default. TODO: handle this properly //Set this back to default. TODO: handle this properly
$this->session->syncGameMode($this->player->getGamemode(), true); $this->session->syncGameMode($this->player->getGamemode(), true);
} }