PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
PlayerListPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
19use function count;
20
22 public const NETWORK_ID = ProtocolInfo::PLAYER_LIST_PACKET;
23
24 public const TYPE_ADD = 0;
25 public const TYPE_REMOVE = 1;
26
27 public int $type;
29 public array $entries = [];
30
35 private static function create(int $type, array $entries) : self{
36 $result = new self;
37 $result->type = $type;
38 $result->entries = $entries;
39 return $result;
40 }
41
45 public static function add(array $entries) : self{
46 return self::create(self::TYPE_ADD, $entries);
47 }
48
52 public static function remove(array $entries) : self{
53 return self::create(self::TYPE_REMOVE, $entries);
54 }
55
56 protected function decodePayload(PacketSerializer $in) : void{
57 $this->type = $in->getByte();
58 $count = $in->getUnsignedVarInt();
59 for($i = 0; $i < $count; ++$i){
60 $entry = new PlayerListEntry();
61
62 if($this->type === self::TYPE_ADD){
63 $entry->uuid = $in->getUUID();
64 $entry->actorUniqueId = $in->getActorUniqueId();
65 $entry->username = $in->getString();
66 $entry->xboxUserId = $in->getString();
67 $entry->platformChatId = $in->getString();
68 $entry->buildPlatform = $in->getLInt();
69 $entry->skinData = $in->getSkin();
70 $entry->isTeacher = $in->getBool();
71 $entry->isHost = $in->getBool();
72 $entry->isSubClient = $in->getBool();
73 }else{
74 $entry->uuid = $in->getUUID();
75 }
76
77 $this->entries[$i] = $entry;
78 }
79 if($this->type === self::TYPE_ADD){
80 for($i = 0; $i < $count; ++$i){
81 $this->entries[$i]->skinData->setVerified($in->getBool());
82 }
83 }
84 }
85
86 protected function encodePayload(PacketSerializer $out) : void{
87 $out->putByte($this->type);
88 $out->putUnsignedVarInt(count($this->entries));
89 foreach($this->entries as $entry){
90 if($this->type === self::TYPE_ADD){
91 $out->putUUID($entry->uuid);
92 $out->putActorUniqueId($entry->actorUniqueId);
93 $out->putString($entry->username);
94 $out->putString($entry->xboxUserId);
95 $out->putString($entry->platformChatId);
96 $out->putLInt($entry->buildPlatform);
97 $out->putSkin($entry->skinData);
98 $out->putBool($entry->isTeacher);
99 $out->putBool($entry->isHost);
100 $out->putBool($entry->isSubClient);
101 }else{
102 $out->putUUID($entry->uuid);
103 }
104 }
105 if($this->type === self::TYPE_ADD){
106 foreach($this->entries as $entry){
107 $out->putBool($entry->skinData->isVerified());
108 }
109 }
110 }
111
112 public function handle(PacketHandlerInterface $handler) : bool{
113 return $handler->handlePlayerList($this);
114 }
115}