PocketMine-MP 5.39.3 git-21ae710729750cd637333d673bbbbbc598fc659e
Loading...
Searching...
No Matches
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
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\DataDecodeException;
20use pmmp\encoding\LE;
21use pmmp\encoding\VarInt;
25use function count;
26
28 public const NETWORK_ID = ProtocolInfo::COMMAND_OUTPUT_PACKET;
29
30 public const TYPE_LAST = "lastoutput";
31 public const TYPE_SILENT = "silent";
32 public const TYPE_ALL = "alloutput";
33 public const TYPE_DATA_SET = "dataset";
34
35 public CommandOriginData $originData;
36 public string $outputType;
37 public int $successCount;
39 public array $messages = [];
40 public ?string $data;
41
42 protected function decodePayload(ByteBufferReader $in) : void{
43 $this->originData = CommonTypes::getCommandOriginData($in);
44 $this->outputType = CommonTypes::getString($in);
45 $this->successCount = LE::readUnsignedInt($in);
46
47 for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; ++$i){
48 $this->messages[] = $this->getCommandMessage($in);
49 }
50
51 $this->data = CommonTypes::readOptional($in, CommonTypes::getString(...));
52 }
53
57 protected function getCommandMessage(ByteBufferReader $in) : CommandOutputMessage{
58 $message = new CommandOutputMessage();
59
60 $message->messageId = CommonTypes::getString($in);
61 $message->isInternal = CommonTypes::getBool($in);
62
63 for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; ++$i){
64 $message->parameters[] = CommonTypes::getString($in);
65 }
66
67 return $message;
68 }
69
70 protected function encodePayload(ByteBufferWriter $out) : void{
71 CommonTypes::putCommandOriginData($out, $this->originData);
72 CommonTypes::putString($out, $this->outputType);
73 LE::writeUnsignedInt($out, $this->successCount);
74
75 VarInt::writeUnsignedInt($out, count($this->messages));
76 foreach($this->messages as $message){
77 $this->putCommandMessage($message, $out);
78 }
79
80 CommonTypes::writeOptional($out, $this->data, CommonTypes::putString(...));
81 }
82
83 protected function putCommandMessage(CommandOutputMessage $message, ByteBufferWriter $out) : void{
84 CommonTypes::putString($out, $message->messageId);
85 CommonTypes::putBool($out, $message->isInternal);
86
87 VarInt::writeUnsignedInt($out, count($message->parameters));
88 foreach($message->parameters as $parameter){
89 CommonTypes::putString($out, $parameter);
90 }
91 }
92
93 public function handle(PacketHandlerInterface $handler) : bool{
94 return $handler->handleCommandOutput($this);
95 }
96}