PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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
17use pmmp\encoding\Byte;
18use pmmp\encoding\ByteBufferReader;
19use pmmp\encoding\ByteBufferWriter;
20use pmmp\encoding\VarInt;
23use function count;
24
26 public const NETWORK_ID = ProtocolInfo::SET_SCOREBOARD_IDENTITY_PACKET;
27
28 public const TYPE_REGISTER_IDENTITY = 0;
29 public const TYPE_CLEAR_IDENTITY = 1;
30
31 public int $type;
33 public array $entries = [];
34
39 public static function create(int $type, array $entries) : self{
40 $result = new self;
41 $result->type = $type;
42 $result->entries = $entries;
43 return $result;
44 }
45
46 protected function decodePayload(ByteBufferReader $in) : void{
47 $this->type = Byte::readUnsigned($in);
48 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
49 $entry = new ScoreboardIdentityPacketEntry();
50 $entry->scoreboardId = VarInt::readSignedLong($in);
51 if($this->type === self::TYPE_REGISTER_IDENTITY){
52 $entry->actorUniqueId = CommonTypes::getActorUniqueId($in);
53 }
54
55 $this->entries[] = $entry;
56 }
57 }
58
59 protected function encodePayload(ByteBufferWriter $out) : void{
60 Byte::writeUnsigned($out, $this->type);
61 VarInt::writeUnsignedInt($out, count($this->entries));
62 foreach($this->entries as $entry){
63 VarInt::writeSignedLong($out, $entry->scoreboardId);
64 if($this->type === self::TYPE_REGISTER_IDENTITY){
65 CommonTypes::putActorUniqueId($out, $entry->actorUniqueId);
66 }
67 }
68 }
69
70 public function handle(PacketHandlerInterface $handler) : bool{
71 return $handler->handleSetScoreboardIdentity($this);
72 }
73}