PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
CommandOutputPacket.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
21use function count;
22
24 public const NETWORK_ID = ProtocolInfo::COMMAND_OUTPUT_PACKET;
25
26 public const TYPE_LAST = 1;
27 public const TYPE_SILENT = 2;
28 public const TYPE_ALL = 3;
29 public const TYPE_DATA_SET = 4;
30
31 public CommandOriginData $originData;
32 public int $outputType;
33 public int $successCount;
35 public array $messages = [];
36 public string $unknownString;
37
38 protected function decodePayload(PacketSerializer $in) : void{
39 $this->originData = $in->getCommandOriginData();
40 $this->outputType = $in->getByte();
41 $this->successCount = $in->getUnsignedVarInt();
42
43 for($i = 0, $size = $in->getUnsignedVarInt(); $i < $size; ++$i){
44 $this->messages[] = $this->getCommandMessage($in);
45 }
46
47 if($this->outputType === self::TYPE_DATA_SET){
48 $this->unknownString = $in->getString();
49 }
50 }
51
56 $message = new CommandOutputMessage();
57
58 $message->isInternal = $in->getBool();
59 $message->messageId = $in->getString();
60
61 for($i = 0, $size = $in->getUnsignedVarInt(); $i < $size; ++$i){
62 $message->parameters[] = $in->getString();
63 }
64
65 return $message;
66 }
67
68 protected function encodePayload(PacketSerializer $out) : void{
69 $out->putCommandOriginData($this->originData);
70 $out->putByte($this->outputType);
71 $out->putUnsignedVarInt($this->successCount);
72
73 $out->putUnsignedVarInt(count($this->messages));
74 foreach($this->messages as $message){
75 $this->putCommandMessage($message, $out);
76 }
77
78 if($this->outputType === self::TYPE_DATA_SET){
79 $out->putString($this->unknownString);
80 }
81 }
82
83 protected function putCommandMessage(CommandOutputMessage $message, PacketSerializer $out) : void{
84 $out->putBool($message->isInternal);
85 $out->putString($message->messageId);
86
87 $out->putUnsignedVarInt(count($message->parameters));
88 foreach($message->parameters as $parameter){
89 $out->putString($parameter);
90 }
91 }
92
93 public function handle(PacketHandlerInterface $handler) : bool{
94 return $handler->handleCommandOutput($this);
95 }
96}