PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
18
20 public const NETWORK_ID = ProtocolInfo::MODAL_FORM_RESPONSE_PACKET;
21
22 public const CANCEL_REASON_CLOSED = 0;
24 public const CANCEL_REASON_USER_BUSY = 1;
25
26 public int $formId;
27 public ?string $formData; //json
28 public ?int $cancelReason;
29
33 private static function create(int $formId, ?string $formData, ?int $cancelReason) : self{
34 $result = new self;
35 $result->formId = $formId;
36 $result->formData = $formData;
37 $result->cancelReason = $cancelReason;
38 return $result;
39 }
40
41 public static function response(int $formId, string $formData) : self{
42 return self::create($formId, $formData, null);
43 }
44
45 public static function cancel(int $formId, int $cancelReason) : self{
46 return self::create($formId, null, $cancelReason);
47 }
48
49 protected function decodePayload(PacketSerializer $in) : void{
50 $this->formId = $in->getUnsignedVarInt();
51 $this->formData = $in->readOptional($in->getString(...));
52 $this->cancelReason = $in->readOptional($in->getByte(...));
53 }
54
55 protected function encodePayload(PacketSerializer $out) : void{
56 $out->putUnsignedVarInt($this->formId);
57
58 $out->writeOptional($this->formData, $out->putString(...));
59 $out->writeOptional($this->cancelReason, $out->putByte(...));
60 }
61
62 public function handle(PacketHandlerInterface $handler) : bool{
63 return $handler->handleModalFormResponse($this);
64 }
65}