PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
19use function count;
20
22 public const NETWORK_ID = ProtocolInfo::SET_SCORE_PACKET;
23
24 public const TYPE_CHANGE = 0;
25 public const TYPE_REMOVE = 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, $i2 = $in->getUnsignedVarInt(); $i < $i2; ++$i){
45 $entry = new ScorePacketEntry();
46 $entry->scoreboardId = $in->getVarLong();
47 $entry->objectiveName = $in->getString();
48 $entry->score = $in->getLInt();
49 if($this->type !== self::TYPE_REMOVE){
50 $entry->type = $in->getByte();
51 switch($entry->type){
52 case ScorePacketEntry::TYPE_PLAYER:
53 case ScorePacketEntry::TYPE_ENTITY:
54 $entry->actorUniqueId = $in->getActorUniqueId();
55 break;
56 case ScorePacketEntry::TYPE_FAKE_PLAYER:
57 $entry->customName = $in->getString();
58 break;
59 default:
60 throw new PacketDecodeException("Unknown entry type $entry->type");
61 }
62 }
63 $this->entries[] = $entry;
64 }
65 }
66
67 protected function encodePayload(PacketSerializer $out) : void{
68 $out->putByte($this->type);
69 $out->putUnsignedVarInt(count($this->entries));
70 foreach($this->entries as $entry){
71 $out->putVarLong($entry->scoreboardId);
72 $out->putString($entry->objectiveName);
73 $out->putLInt($entry->score);
74 if($this->type !== self::TYPE_REMOVE){
75 $out->putByte($entry->type);
76 switch($entry->type){
77 case ScorePacketEntry::TYPE_PLAYER:
78 case ScorePacketEntry::TYPE_ENTITY:
79 $out->putActorUniqueId($entry->actorUniqueId);
80 break;
81 case ScorePacketEntry::TYPE_FAKE_PLAYER:
82 $out->putString($entry->customName);
83 break;
84 default:
85 throw new \InvalidArgumentException("Unknown entry type $entry->type");
86 }
87 }
88 }
89 }
90
91 public function handle(PacketHandlerInterface $handler) : bool{
92 return $handler->handleSetScore($this);
93 }
94}
static create(int $type, array $entries)
handle(PacketHandlerInterface $handler)