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