PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
SetScoreboardIdentityPacket.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::SET_SCOREBOARD_IDENTITY_PACKET;
23
24 public const TYPE_REGISTER_IDENTITY = 0;
25 public const TYPE_CLEAR_IDENTITY = 1;
26
27 public int $type;
29 public array $entries = [];
30
35 public 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
42 protected function decodePayload(PacketSerializer $in) : void{
43 $this->type = $in->getByte();
44 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
45 $entry = new ScoreboardIdentityPacketEntry();
46 $entry->scoreboardId = $in->getVarLong();
47 if($this->type === self::TYPE_REGISTER_IDENTITY){
48 $entry->actorUniqueId = $in->getActorUniqueId();
49 }
50
51 $this->entries[] = $entry;
52 }
53 }
54
55 protected function encodePayload(PacketSerializer $out) : void{
56 $out->putByte($this->type);
57 $out->putUnsignedVarInt(count($this->entries));
58 foreach($this->entries as $entry){
59 $out->putVarLong($entry->scoreboardId);
60 if($this->type === self::TYPE_REGISTER_IDENTITY){
61 $out->putActorUniqueId($entry->actorUniqueId);
62 }
63 }
64 }
65
66 public function handle(PacketHandlerInterface $handler) : bool{
67 return $handler->handleSetScoreboardIdentity($this);
68 }
69}