PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\LE;
21use pmmp\encoding\VarInt;
25use function count;
26
28 public const NETWORK_ID = ProtocolInfo::PLAYER_LIST_PACKET;
29
30 public const TYPE_ADD = 0;
31 public const TYPE_REMOVE = 1;
32
33 public int $type;
35 public array $entries = [];
36
41 private static function create(int $type, array $entries) : self{
42 $result = new self;
43 $result->type = $type;
44 $result->entries = $entries;
45 return $result;
46 }
47
51 public static function add(array $entries) : self{
52 return self::create(self::TYPE_ADD, $entries);
53 }
54
58 public static function remove(array $entries) : self{
59 return self::create(self::TYPE_REMOVE, $entries);
60 }
61
62 protected function decodePayload(ByteBufferReader $in) : void{
63 $this->type = Byte::readUnsigned($in);
64 $count = VarInt::readUnsignedInt($in);
65 for($i = 0; $i < $count; ++$i){
66 $entry = new PlayerListEntry();
67
68 if($this->type === self::TYPE_ADD){
69 $entry->uuid = CommonTypes::getUUID($in);
70 $entry->actorUniqueId = CommonTypes::getActorUniqueId($in);
71 $entry->username = CommonTypes::getString($in);
72 $entry->xboxUserId = CommonTypes::getString($in);
73 $entry->platformChatId = CommonTypes::getString($in);
74 $entry->buildPlatform = LE::readSignedInt($in);
75 $entry->skinData = CommonTypes::getSkin($in);
76 $entry->isTeacher = CommonTypes::getBool($in);
77 $entry->isHost = CommonTypes::getBool($in);
78 $entry->isSubClient = CommonTypes::getBool($in);
79 $entry->color = Color::fromARGB(LE::readUnsignedInt($in));
80 }else{
81 $entry->uuid = CommonTypes::getUUID($in);
82 }
83
84 $this->entries[$i] = $entry;
85 }
86 if($this->type === self::TYPE_ADD){
87 for($i = 0; $i < $count; ++$i){
88 $this->entries[$i]->skinData->setVerified(CommonTypes::getBool($in));
89 }
90 }
91 }
92
93 protected function encodePayload(ByteBufferWriter $out) : void{
94 Byte::writeUnsigned($out, $this->type);
95 VarInt::writeUnsignedInt($out, count($this->entries));
96 foreach($this->entries as $entry){
97 if($this->type === self::TYPE_ADD){
98 CommonTypes::putUUID($out, $entry->uuid);
99 CommonTypes::putActorUniqueId($out, $entry->actorUniqueId);
100 CommonTypes::putString($out, $entry->username);
101 CommonTypes::putString($out, $entry->xboxUserId);
102 CommonTypes::putString($out, $entry->platformChatId);
103 LE::writeSignedInt($out, $entry->buildPlatform);
104 CommonTypes::putSkin($out, $entry->skinData);
105 CommonTypes::putBool($out, $entry->isTeacher);
106 CommonTypes::putBool($out, $entry->isHost);
107 CommonTypes::putBool($out, $entry->isSubClient);
108 LE::writeUnsignedInt($out, ($entry->color ?? new Color(255, 255, 255))->toARGB());
109 }else{
110 CommonTypes::putUUID($out, $entry->uuid);
111 }
112 }
113 if($this->type === self::TYPE_ADD){
114 foreach($this->entries as $entry){
115 CommonTypes::putBool($out, $entry->skinData->isVerified());
116 }
117 }
118 }
119
120 public function handle(PacketHandlerInterface $handler) : bool{
121 return $handler->handlePlayerList($this);
122 }
123}