PocketMine-MP 5.28.1 git-88cdc2eb67c40075559c3ef51418b418cd5488e9
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
20use function count;
21
23 public const NETWORK_ID = ProtocolInfo::PLAYER_LIST_PACKET;
24
25 public const TYPE_ADD = 0;
26 public const TYPE_REMOVE = 1;
27
28 public int $type;
30 public array $entries = [];
31
36 private static function create(int $type, array $entries) : self{
37 $result = new self;
38 $result->type = $type;
39 $result->entries = $entries;
40 return $result;
41 }
42
46 public static function add(array $entries) : self{
47 return self::create(self::TYPE_ADD, $entries);
48 }
49
53 public static function remove(array $entries) : self{
54 return self::create(self::TYPE_REMOVE, $entries);
55 }
56
57 protected function decodePayload(PacketSerializer $in) : void{
58 $this->type = $in->getByte();
59 $count = $in->getUnsignedVarInt();
60 for($i = 0; $i < $count; ++$i){
61 $entry = new PlayerListEntry();
62
63 if($this->type === self::TYPE_ADD){
64 $entry->uuid = $in->getUUID();
65 $entry->actorUniqueId = $in->getActorUniqueId();
66 $entry->username = $in->getString();
67 $entry->xboxUserId = $in->getString();
68 $entry->platformChatId = $in->getString();
69 $entry->buildPlatform = $in->getLInt();
70 $entry->skinData = $in->getSkin();
71 $entry->isTeacher = $in->getBool();
72 $entry->isHost = $in->getBool();
73 $entry->isSubClient = $in->getBool();
74 $entry->color = Color::fromARGB($in->getLInt());
75 }else{
76 $entry->uuid = $in->getUUID();
77 }
78
79 $this->entries[$i] = $entry;
80 }
81 if($this->type === self::TYPE_ADD){
82 for($i = 0; $i < $count; ++$i){
83 $this->entries[$i]->skinData->setVerified($in->getBool());
84 }
85 }
86 }
87
88 protected function encodePayload(PacketSerializer $out) : void{
89 $out->putByte($this->type);
90 $out->putUnsignedVarInt(count($this->entries));
91 foreach($this->entries as $entry){
92 if($this->type === self::TYPE_ADD){
93 $out->putUUID($entry->uuid);
94 $out->putActorUniqueId($entry->actorUniqueId);
95 $out->putString($entry->username);
96 $out->putString($entry->xboxUserId);
97 $out->putString($entry->platformChatId);
98 $out->putLInt($entry->buildPlatform);
99 $out->putSkin($entry->skinData);
100 $out->putBool($entry->isTeacher);
101 $out->putBool($entry->isHost);
102 $out->putBool($entry->isSubClient);
103 $out->putLInt(($entry->color ?? new Color(255, 255, 255))->toARGB());
104 }else{
105 $out->putUUID($entry->uuid);
106 }
107 }
108 if($this->type === self::TYPE_ADD){
109 foreach($this->entries as $entry){
110 $out->putBool($entry->skinData->isVerified());
111 }
112 }
113 }
114
115 public function handle(PacketHandlerInterface $handler) : bool{
116 return $handler->handlePlayerList($this);
117 }
118}