PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
DisconnectPacket.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\VarInt;
21
23 public const NETWORK_ID = ProtocolInfo::DISCONNECT_PACKET;
24
25 public int $reason; //TODO: add constants / enum
26 public ?string $message;
27 public ?string $filteredMessage;
28
32 public static function create(int $reason, ?string $message, ?string $filteredMessage) : self{
33 $result = new self;
34 $result->reason = $reason;
35 $result->message = $message;
36 $result->filteredMessage = $filteredMessage;
37 return $result;
38 }
39
40 public function canBeSentBeforeLogin() : bool{
41 return true;
42 }
43
44 protected function decodePayload(ByteBufferReader $in) : void{
45 $this->reason = VarInt::readSignedInt($in);
46 $skipMessage = CommonTypes::getBool($in);
47 $this->message = $skipMessage ? null : CommonTypes::getString($in);
48 $this->filteredMessage = $skipMessage ? null : CommonTypes::getString($in);
49 }
50
51 protected function encodePayload(ByteBufferWriter $out) : void{
52 VarInt::writeSignedInt($out, $this->reason);
53 CommonTypes::putBool($out, $skipMessage = $this->message === null && $this->filteredMessage === null);
54 if(!$skipMessage){
55 CommonTypes::putString($out, $this->message ?? "");
56 CommonTypes::putString($out, $this->filteredMessage ?? "");
57 }
58 }
59
60 public function handle(PacketHandlerInterface $handler) : bool{
61 return $handler->handleDisconnect($this);
62 }
63}
static create(int $reason, ?string $message, ?string $filteredMessage)