move PlayerListPacket sending responsibility to NetworkSession

This commit is contained in:
Dylan K. Taylor 2019-06-04 18:02:36 +01:00
parent 89d4f596bd
commit 6f29fe063f
4 changed files with 35 additions and 52 deletions

View File

@ -802,9 +802,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
*/ */
public function setDisplayName(string $name){ public function setDisplayName(string $name){
$this->displayName = $name; $this->displayName = $name;
if($this->spawned){
$this->server->updatePlayerListData($this->getUniqueId(), $this->getId(), $this->getDisplayName(), $this->getSkin(), $this->getXuid());
}
} }
/** /**

View File

@ -34,7 +34,6 @@ use pocketmine\command\ConsoleCommandSender;
use pocketmine\command\PluginIdentifiableCommand; use pocketmine\command\PluginIdentifiableCommand;
use pocketmine\command\SimpleCommandMap; use pocketmine\command\SimpleCommandMap;
use pocketmine\entity\EntityFactory; use pocketmine\entity\EntityFactory;
use pocketmine\entity\Skin;
use pocketmine\event\HandlerList; use pocketmine\event\HandlerList;
use pocketmine\event\player\PlayerDataSaveEvent; use pocketmine\event\player\PlayerDataSaveEvent;
use pocketmine\event\server\CommandEvent; use pocketmine\event\server\CommandEvent;
@ -61,9 +60,7 @@ use pocketmine\network\mcpe\NetworkCompression;
use pocketmine\network\mcpe\NetworkSession; use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\PacketBatch; use pocketmine\network\mcpe\PacketBatch;
use pocketmine\network\mcpe\protocol\ClientboundPacket; use pocketmine\network\mcpe\protocol\ClientboundPacket;
use pocketmine\network\mcpe\protocol\PlayerListPacket;
use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\ProtocolInfo;
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;
@ -1813,8 +1810,9 @@ class Server{
} }
public function addOnlinePlayer(Player $player) : void{ public function addOnlinePlayer(Player $player) : void{
$this->updatePlayerListData($player->getUniqueId(), $player->getId(), $player->getDisplayName(), $player->getSkin(), $player->getXuid()); foreach($this->playerList as $p){
$p->getNetworkSession()->onPlayerAdded($player);
}
$this->playerList[$player->getRawUniqueId()] = $player; $this->playerList[$player->getRawUniqueId()] = $player;
if($this->sendUsageTicker > 0){ if($this->sendUsageTicker > 0){
@ -1825,50 +1823,10 @@ class Server{
public function removeOnlinePlayer(Player $player) : void{ public function removeOnlinePlayer(Player $player) : void{
if(isset($this->playerList[$player->getRawUniqueId()])){ if(isset($this->playerList[$player->getRawUniqueId()])){
unset($this->playerList[$player->getRawUniqueId()]); unset($this->playerList[$player->getRawUniqueId()]);
foreach($this->playerList as $p){
$this->removePlayerListData($player->getUniqueId()); $p->getNetworkSession()->onPlayerRemoved($player);
} }
} }
/**
* @param UUID $uuid
* @param int $entityId
* @param string $name
* @param Skin $skin
* @param string $xboxUserId
* @param Player[]|null $players
*/
public function updatePlayerListData(UUID $uuid, int $entityId, string $name, Skin $skin, string $xboxUserId = "", ?array $players = null) : void{
$pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_ADD;
$pk->entries[] = PlayerListEntry::createAdditionEntry($uuid, $entityId, $name, $skin, $xboxUserId);
$this->broadcastPacket($players ?? $this->playerList, $pk);
}
/**
* @param UUID $uuid
* @param Player[]|null $players
*/
public function removePlayerListData(UUID $uuid, ?array $players = null) : void{
$pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_REMOVE;
$pk->entries[] = PlayerListEntry::createRemovalEntry($uuid);
$this->broadcastPacket($players ?? $this->playerList, $pk);
}
/**
* @param Player $p
*/
public function sendFullPlayerListData(Player $p) : void{
$pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_ADD;
foreach($this->playerList as $player){
$pk->entries[] = PlayerListEntry::createAdditionEntry($player->getUniqueId(), $player->getId(), $player->getDisplayName(), $player->getSkin(), $player->getXuid());
}
$p->sendDataPacket($pk);
} }
public function sendUsage(int $type = SendUsageTask::TYPE_STATUS) : void{ public function sendUsage(int $type = SendUsageTask::TYPE_STATUS) : void{

View File

@ -55,6 +55,7 @@ use pocketmine\network\mcpe\protocol\ModalFormRequestPacket;
use pocketmine\network\mcpe\protocol\MovePlayerPacket; use pocketmine\network\mcpe\protocol\MovePlayerPacket;
use pocketmine\network\mcpe\protocol\NetworkChunkPublisherUpdatePacket; use pocketmine\network\mcpe\protocol\NetworkChunkPublisherUpdatePacket;
use pocketmine\network\mcpe\protocol\Packet; use pocketmine\network\mcpe\protocol\Packet;
use pocketmine\network\mcpe\protocol\PlayerListPacket;
use pocketmine\network\mcpe\protocol\PlayStatusPacket; use pocketmine\network\mcpe\protocol\PlayStatusPacket;
use pocketmine\network\mcpe\protocol\ServerboundPacket; use pocketmine\network\mcpe\protocol\ServerboundPacket;
use pocketmine\network\mcpe\protocol\ServerToClientHandshakePacket; use pocketmine\network\mcpe\protocol\ServerToClientHandshakePacket;
@ -66,6 +67,7 @@ use pocketmine\network\mcpe\protocol\types\CommandData;
use pocketmine\network\mcpe\protocol\types\CommandEnum; use pocketmine\network\mcpe\protocol\types\CommandEnum;
use pocketmine\network\mcpe\protocol\types\CommandParameter; use pocketmine\network\mcpe\protocol\types\CommandParameter;
use pocketmine\network\mcpe\protocol\types\ContainerIds; use pocketmine\network\mcpe\protocol\types\ContainerIds;
use pocketmine\network\mcpe\protocol\types\PlayerListEntry;
use pocketmine\network\mcpe\protocol\types\PlayerPermissions; use pocketmine\network\mcpe\protocol\types\PlayerPermissions;
use pocketmine\network\mcpe\protocol\UpdateAttributesPacket; use pocketmine\network\mcpe\protocol\UpdateAttributesPacket;
use pocketmine\network\NetworkInterface; use pocketmine\network\NetworkInterface;
@ -843,6 +845,32 @@ class NetworkSession{
$this->sendDataPacket($pk); $this->sendDataPacket($pk);
} }
public function syncPlayerList() : void{
$pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_ADD;
foreach($this->server->getOnlinePlayers() as $player){
$pk->entries[] = PlayerListEntry::createAdditionEntry($player->getUniqueId(), $player->getId(), $player->getDisplayName(), $player->getSkin(), $player->getXuid());
}
$this->sendDataPacket($pk);
}
public function onPlayerAdded(Player $p) : void{
$pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_ADD;
$pk->entries[] = PlayerListEntry::createAdditionEntry($p->getUniqueId(), $p->getId(), $p->getName(), $p->getSkin(), $p->getXuid());
$this->sendDataPacket($pk);
}
public function onPlayerRemoved(Player $p) : void{
if($p !== $this->player){
$pk = new PlayerListPacket();
$pk->type = PlayerListPacket::TYPE_REMOVE;
$pk->entries[] = PlayerListEntry::createRemovalEntry($p->getUniqueId());
$this->sendDataPacket($pk);
}
}
public function tick() : bool{ public function tick() : bool{
if($this->handler instanceof LoginSessionHandler){ if($this->handler instanceof LoginSessionHandler){
if(time() >= $this->connectTime + 10){ if(time() >= $this->connectTime + 10){

View File

@ -96,7 +96,7 @@ class PreSpawnSessionHandler extends SessionHandler{
$this->player->getInventory()->sendHeldItem($this->player); $this->player->getInventory()->sendHeldItem($this->player);
$this->session->queueCompressed($this->server->getCraftingManager()->getCraftingDataPacket()); $this->session->queueCompressed($this->server->getCraftingManager()->getCraftingDataPacket());
$this->server->sendFullPlayerListData($this->player); $this->session->syncPlayerList();
} }
public function handleRequestChunkRadius(RequestChunkRadiusPacket $packet) : bool{ public function handleRequestChunkRadius(RequestChunkRadiusPacket $packet) : bool{