PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
ModalFormResponsePacket.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\VarInt;
22
24 public const NETWORK_ID = ProtocolInfo::MODAL_FORM_RESPONSE_PACKET;
25
26 public const CANCEL_REASON_CLOSED = 0;
28 public const CANCEL_REASON_USER_BUSY = 1;
29
30 public int $formId;
31 public ?string $formData; //json
32 public ?int $cancelReason;
33
37 private static function create(int $formId, ?string $formData, ?int $cancelReason) : self{
38 $result = new self;
39 $result->formId = $formId;
40 $result->formData = $formData;
41 $result->cancelReason = $cancelReason;
42 return $result;
43 }
44
45 public static function response(int $formId, string $formData) : self{
46 return self::create($formId, $formData, null);
47 }
48
49 public static function cancel(int $formId, int $cancelReason) : self{
50 return self::create($formId, null, $cancelReason);
51 }
52
53 protected function decodePayload(ByteBufferReader $in) : void{
54 $this->formId = VarInt::readUnsignedInt($in);
55 $this->formData = CommonTypes::readOptional($in, CommonTypes::getString(...));
56 $this->cancelReason = CommonTypes::readOptional($in, Byte::readUnsigned(...));
57 }
58
59 protected function encodePayload(ByteBufferWriter $out) : void{
60 VarInt::writeUnsignedInt($out, $this->formId);
61
62 CommonTypes::writeOptional($out, $this->formData, CommonTypes::putString(...));
63 CommonTypes::writeOptional($out, $this->cancelReason, Byte::writeUnsigned(...));
64 }
65
66 public function handle(PacketHandlerInterface $handler) : bool{
67 return $handler->handleModalFormResponse($this);
68 }
69}