Cleaned up PlayerList handling

This commit is contained in:
Dylan K. Taylor 2017-08-19 19:36:15 +01:00
parent e5e76d4c93
commit e0307411da
3 changed files with 130 additions and 32 deletions

View File

@ -82,6 +82,7 @@ use pocketmine\network\mcpe\protocol\BatchPacket;
use pocketmine\network\mcpe\protocol\DataPacket; use pocketmine\network\mcpe\protocol\DataPacket;
use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\PlayerListPacket; use pocketmine\network\mcpe\protocol\PlayerListPacket;
use pocketmine\network\mcpe\protocol\types\PlayerListEntry;
use pocketmine\network\mcpe\RakLibInterface; use pocketmine\network\mcpe\RakLibInterface;
use pocketmine\network\Network; use pocketmine\network\Network;
use pocketmine\network\query\QueryHandler; use pocketmine\network\query\QueryHandler;
@ -2252,32 +2253,45 @@ class Server{
if(isset($this->playerList[$player->getRawUniqueId()])){ if(isset($this->playerList[$player->getRawUniqueId()])){
unset($this->playerList[$player->getRawUniqueId()]); unset($this->playerList[$player->getRawUniqueId()]);
$pk = new PlayerListPacket(); $this->removePlayerListData($player->getUniqueId());
$pk->type = PlayerListPacket::TYPE_REMOVE;
$pk->entries[] = [$player->getUniqueId()];
$this->broadcastPacket($this->playerList, $pk);
} }
} }
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 = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_ADD; $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); $this->broadcastPacket($players ?? $this->playerList, $pk);
} }
/**
* @param UUID $uuid
* @param Player[]|null $players
*/
public function removePlayerListData(UUID $uuid, array $players = null){ public function removePlayerListData(UUID $uuid, array $players = null){
$pk = new PlayerListPacket(); $pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_REMOVE; $pk->type = PlayerListPacket::TYPE_REMOVE;
$pk->entries[] = [$uuid]; $pk->entries[] = PlayerListEntry::createRemovalEntry($uuid);
$this->broadcastPacket($players ?? $this->playerList, $pk); $this->broadcastPacket($players ?? $this->playerList, $pk);
} }
/**
* @param Player $p
*/
public function sendFullPlayerListData(Player $p){ public function sendFullPlayerListData(Player $p){
$pk = new PlayerListPacket(); $pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_ADD; $pk->type = PlayerListPacket::TYPE_ADD;
foreach($this->playerList as $player){ 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); $p->dataPacket($pk);

View File

@ -27,6 +27,7 @@ namespace pocketmine\network\mcpe\protocol;
use pocketmine\network\mcpe\NetworkSession; use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\types\PlayerListEntry;
class PlayerListPacket extends DataPacket{ class PlayerListPacket extends DataPacket{
const NETWORK_ID = ProtocolInfo::PLAYER_LIST_PACKET; const NETWORK_ID = ProtocolInfo::PLAYER_LIST_PACKET;
@ -34,8 +35,7 @@ class PlayerListPacket extends DataPacket{
const TYPE_ADD = 0; const TYPE_ADD = 0;
const TYPE_REMOVE = 1; const TYPE_REMOVE = 1;
//REMOVE: UUID, ADD: UUID, entity id, name, skinId, skin, geometric model, geometry data /** @var PlayerListEntry[] */
/** @var array[] */
public $entries = []; public $entries = [];
/** @var int */ /** @var int */
public $type; public $type;
@ -49,39 +49,42 @@ class PlayerListPacket extends DataPacket{
$this->type = $this->getByte(); $this->type = $this->getByte();
$count = $this->getUnsignedVarInt(); $count = $this->getUnsignedVarInt();
for($i = 0; $i < $count; ++$i){ for($i = 0; $i < $count; ++$i){
$this->entries[$i] = []; $entry = new PlayerListEntry();
if($this->type === self::TYPE_ADD){ if($this->type === self::TYPE_ADD){
$this->entries[$i][0] = $this->getUUID(); $entry->uuid = $this->getUUID();
$this->entries[$i][1] = $this->getEntityUniqueId(); $entry->entityUniqueId = $this->getEntityUniqueId();
$this->entries[$i][2] = $this->getString(); //name $entry->username = $this->getString();
$this->entries[$i][3] = $this->getString(); //skin id $entry->skinId = $this->getString();
$this->entries[$i][4] = $this->getString(); //skin data $entry->skinData = $this->getString();
$this->entries[$i][5] = $this->getString(); //cape data $entry->capeData = $this->getString();
$this->entries[$i][6] = $this->getString(); //geometric model $entry->geometryModel = $this->getString();
$this->entries[$i][7] = $this->getString(); //geometry data (json) $entry->geometryData = $this->getString();
$this->entries[$i][8] = $this->getString(); //??? $entry->xboxUserId = $this->getString();
}else{ }else{
$this->entries[$i][0] = $this->getUUID(); $entry->uuid = $this->getUUID();
} }
$this->entries[$i] = $entry;
} }
} }
protected function encodePayload(){ protected function encodePayload(){
$this->putByte($this->type); $this->putByte($this->type);
$this->putUnsignedVarInt(count($this->entries)); $this->putUnsignedVarInt(count($this->entries));
foreach($this->entries as $d){ foreach($this->entries as $entry){
if($this->type === self::TYPE_ADD){ if($this->type === self::TYPE_ADD){
$this->putUUID($d[0]); $this->putUUID($entry->uuid);
$this->putEntityUniqueId($d[1]); $this->putEntityUniqueId($entry->entityUniqueId);
$this->putString($d[2]); //name $this->putString($entry->username);
$this->putString($d[3]); //skin id $this->putString($entry->skinId);
$this->putString($d[4]); //skin data $this->putString($entry->skinData);
$this->putString($d[5] ?? ""); //cape data $this->putString($entry->capeData);
$this->putString($d[6] ?? ""); //geometric model $this->putString($entry->geometryModel);
$this->putString($d[7] ?? ""); //geometry data (json) $this->putString($entry->geometryData);
$this->putString($d[8] ?? ""); //??? $this->putString($entry->xboxUserId);
}else{ }else{
$this->putUUID($d[0]); $this->putUUID($entry->uuid);
} }
} }
} }

View File

@ -0,0 +1,81 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\protocol\types;
use pocketmine\utils\UUID;
class PlayerListEntry{
/** @var UUID */
public $uuid;
/** @var int */
public $entityUniqueId;
/** @var string */
public $username;
/** @var string */
public $skinId;
/** @var string */
public $skinData;
/** @var string */
public $capeData; //TODO
/** @var string */
public $geometryModel; //TODO
/** @var string */
public $geometryData; //TODO
/** @var string */
public $xboxUserId; //TODO
public static function createRemovalEntry(UUID $uuid) : PlayerListEntry{
$entry = new PlayerListEntry();
$entry->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;
}
}