From e0307411dac9e161c569ca34fe7e88677847cb59 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 19 Aug 2017 19:36:15 +0100 Subject: [PATCH] Cleaned up PlayerList handling --- src/pocketmine/Server.php | 30 +++++-- .../mcpe/protocol/PlayerListPacket.php | 51 ++++++------ .../mcpe/protocol/types/PlayerListEntry.php | 81 +++++++++++++++++++ 3 files changed, 130 insertions(+), 32 deletions(-) create mode 100644 src/pocketmine/network/mcpe/protocol/types/PlayerListEntry.php diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index f361343ee..73f1d34e7 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -82,6 +82,7 @@ use pocketmine\network\mcpe\protocol\BatchPacket; use pocketmine\network\mcpe\protocol\DataPacket; use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\PlayerListPacket; +use pocketmine\network\mcpe\protocol\types\PlayerListEntry; use pocketmine\network\mcpe\RakLibInterface; use pocketmine\network\Network; use pocketmine\network\query\QueryHandler; @@ -2252,32 +2253,45 @@ class Server{ if(isset($this->playerList[$player->getRawUniqueId()])){ unset($this->playerList[$player->getRawUniqueId()]); - $pk = new PlayerListPacket(); - $pk->type = PlayerListPacket::TYPE_REMOVE; - $pk->entries[] = [$player->getUniqueId()]; - $this->broadcastPacket($this->playerList, $pk); + $this->removePlayerListData($player->getUniqueId()); } } - public function updatePlayerListData(UUID $uuid, $entityId, $name, $skinId, $skinData, array $players = null){ + /** + * @param UUID $uuid + * @param int $entityId + * @param string $name + * @param string $skinId + * @param string $skinData + * @param Player[]|null $players + */ + public function updatePlayerListData(UUID $uuid, int $entityId, string $name, string $skinId, string $skinData, array $players = null){ $pk = new PlayerListPacket(); $pk->type = PlayerListPacket::TYPE_ADD; - $pk->entries[] = [$uuid, $entityId, $name, $skinId, $skinData]; + + $pk->entries[] = PlayerListEntry::createAdditionEntry($uuid, $entityId, $name, $skinId, $skinData); $this->broadcastPacket($players ?? $this->playerList, $pk); } + /** + * @param UUID $uuid + * @param Player[]|null $players + */ public function removePlayerListData(UUID $uuid, array $players = null){ $pk = new PlayerListPacket(); $pk->type = PlayerListPacket::TYPE_REMOVE; - $pk->entries[] = [$uuid]; + $pk->entries[] = PlayerListEntry::createRemovalEntry($uuid); $this->broadcastPacket($players ?? $this->playerList, $pk); } + /** + * @param Player $p + */ public function sendFullPlayerListData(Player $p){ $pk = new PlayerListPacket(); $pk->type = PlayerListPacket::TYPE_ADD; foreach($this->playerList as $player){ - $pk->entries[] = [$player->getUniqueId(), $player->getId(), $player->getDisplayName(), $player->getSkinId(), $player->getSkinData()]; + $pk->entries[] = PlayerListEntry::createAdditionEntry($player->getUniqueId(), $player->getId(), $player->getDisplayName(), $player->getSkinId(), $player->getSkinData()); } $p->dataPacket($pk); diff --git a/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php b/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php index 47b74f203..3f6760c42 100644 --- a/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php +++ b/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php @@ -27,6 +27,7 @@ namespace pocketmine\network\mcpe\protocol; use pocketmine\network\mcpe\NetworkSession; +use pocketmine\network\mcpe\protocol\types\PlayerListEntry; class PlayerListPacket extends DataPacket{ const NETWORK_ID = ProtocolInfo::PLAYER_LIST_PACKET; @@ -34,8 +35,7 @@ class PlayerListPacket extends DataPacket{ const TYPE_ADD = 0; const TYPE_REMOVE = 1; - //REMOVE: UUID, ADD: UUID, entity id, name, skinId, skin, geometric model, geometry data - /** @var array[] */ + /** @var PlayerListEntry[] */ public $entries = []; /** @var int */ public $type; @@ -49,39 +49,42 @@ class PlayerListPacket extends DataPacket{ $this->type = $this->getByte(); $count = $this->getUnsignedVarInt(); for($i = 0; $i < $count; ++$i){ - $this->entries[$i] = []; + $entry = new PlayerListEntry(); + if($this->type === self::TYPE_ADD){ - $this->entries[$i][0] = $this->getUUID(); - $this->entries[$i][1] = $this->getEntityUniqueId(); - $this->entries[$i][2] = $this->getString(); //name - $this->entries[$i][3] = $this->getString(); //skin id - $this->entries[$i][4] = $this->getString(); //skin data - $this->entries[$i][5] = $this->getString(); //cape data - $this->entries[$i][6] = $this->getString(); //geometric model - $this->entries[$i][7] = $this->getString(); //geometry data (json) - $this->entries[$i][8] = $this->getString(); //??? + $entry->uuid = $this->getUUID(); + $entry->entityUniqueId = $this->getEntityUniqueId(); + $entry->username = $this->getString(); + $entry->skinId = $this->getString(); + $entry->skinData = $this->getString(); + $entry->capeData = $this->getString(); + $entry->geometryModel = $this->getString(); + $entry->geometryData = $this->getString(); + $entry->xboxUserId = $this->getString(); }else{ - $this->entries[$i][0] = $this->getUUID(); + $entry->uuid = $this->getUUID(); } + + $this->entries[$i] = $entry; } } protected function encodePayload(){ $this->putByte($this->type); $this->putUnsignedVarInt(count($this->entries)); - foreach($this->entries as $d){ + foreach($this->entries as $entry){ if($this->type === self::TYPE_ADD){ - $this->putUUID($d[0]); - $this->putEntityUniqueId($d[1]); - $this->putString($d[2]); //name - $this->putString($d[3]); //skin id - $this->putString($d[4]); //skin data - $this->putString($d[5] ?? ""); //cape data - $this->putString($d[6] ?? ""); //geometric model - $this->putString($d[7] ?? ""); //geometry data (json) - $this->putString($d[8] ?? ""); //??? + $this->putUUID($entry->uuid); + $this->putEntityUniqueId($entry->entityUniqueId); + $this->putString($entry->username); + $this->putString($entry->skinId); + $this->putString($entry->skinData); + $this->putString($entry->capeData); + $this->putString($entry->geometryModel); + $this->putString($entry->geometryData); + $this->putString($entry->xboxUserId); }else{ - $this->putUUID($d[0]); + $this->putUUID($entry->uuid); } } } diff --git a/src/pocketmine/network/mcpe/protocol/types/PlayerListEntry.php b/src/pocketmine/network/mcpe/protocol/types/PlayerListEntry.php new file mode 100644 index 000000000..c334368b1 --- /dev/null +++ b/src/pocketmine/network/mcpe/protocol/types/PlayerListEntry.php @@ -0,0 +1,81 @@ +uuid = $uuid; + + return $entry; + } + + public static function createAdditionEntry( + UUID $uuid, + int $entityUniqueId, + string $username, + string $skinId, + string $skinData, + string $capeData = "", + string $geometryModel = "", + string $geometryData = "", + string $xboxUserId = "" + ) : PlayerListEntry{ + $entry = new PlayerListEntry(); + $entry->uuid = $uuid; + $entry->entityUniqueId = $entityUniqueId; + $entry->username = $username; + $entry->skinId = $skinId; + $entry->skinData = $skinData; + $entry->capeData = $capeData; + $entry->geometryModel = $geometryModel; + $entry->geometryData = $geometryData; + $entry->xboxUserId = $xboxUserId; + + return $entry; + } + +} \ No newline at end of file