PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
SetScorePacket.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;
24use function count;
25
27 public const NETWORK_ID = ProtocolInfo::SET_SCORE_PACKET;
28
29 public const TYPE_CHANGE = 0;
30 public const TYPE_REMOVE = 1;
31
32 public int $type;
34 public array $entries = [];
35
40 public static function create(int $type, array $entries) : self{
41 $result = new self;
42 $result->type = $type;
43 $result->entries = $entries;
44 return $result;
45 }
46
47 protected function decodePayload(ByteBufferReader $in) : void{
48 $this->type = Byte::readUnsigned($in);
49 for($i = 0, $i2 = VarInt::readUnsignedInt($in); $i < $i2; ++$i){
50 $entry = new ScorePacketEntry();
51 $entry->scoreboardId = VarInt::readSignedLong($in);
52 $entry->objectiveName = CommonTypes::getString($in);
53 $entry->score = LE::readSignedInt($in);
54 if($this->type !== self::TYPE_REMOVE){
55 $entry->type = Byte::readUnsigned($in);
56 switch($entry->type){
57 case ScorePacketEntry::TYPE_PLAYER:
58 case ScorePacketEntry::TYPE_ENTITY:
59 $entry->actorUniqueId = CommonTypes::getActorUniqueId($in);
60 break;
61 case ScorePacketEntry::TYPE_FAKE_PLAYER:
62 $entry->customName = CommonTypes::getString($in);
63 break;
64 default:
65 throw new PacketDecodeException("Unknown entry type $entry->type");
66 }
67 }
68 $this->entries[] = $entry;
69 }
70 }
71
72 protected function encodePayload(ByteBufferWriter $out) : void{
73 Byte::writeUnsigned($out, $this->type);
74 VarInt::writeUnsignedInt($out, count($this->entries));
75 foreach($this->entries as $entry){
76 VarInt::writeSignedLong($out, $entry->scoreboardId);
77 CommonTypes::putString($out, $entry->objectiveName);
78 LE::writeSignedInt($out, $entry->score);
79 if($this->type !== self::TYPE_REMOVE){
80 Byte::writeUnsigned($out, $entry->type);
81 switch($entry->type){
82 case ScorePacketEntry::TYPE_PLAYER:
83 case ScorePacketEntry::TYPE_ENTITY:
84 CommonTypes::putActorUniqueId($out, $entry->actorUniqueId);
85 break;
86 case ScorePacketEntry::TYPE_FAKE_PLAYER:
87 CommonTypes::putString($out, $entry->customName);
88 break;
89 default:
90 throw new \InvalidArgumentException("Unknown entry type $entry->type");
91 }
92 }
93 }
94 }
95
96 public function handle(PacketHandlerInterface $handler) : bool{
97 return $handler->handleSetScore($this);
98 }
99}
static create(int $type, array $entries)
handle(PacketHandlerInterface $handler)