PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
RequestAbilityPacket.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\LE;
21use pmmp\encoding\VarInt;
23use function is_bool;
24use function is_float;
25
32 public const NETWORK_ID = ProtocolInfo::REQUEST_ABILITY_PACKET;
33
34 private const VALUE_TYPE_BOOL = 1;
35 private const VALUE_TYPE_FLOAT = 2;
36
37 public const ABILITY_FLYING = 9;
38 public const ABILITY_NOCLIP = 17;
39
40 private int $abilityId;
41 private float|bool $abilityValue;
42
46 public static function create(int $abilityId, float|bool $abilityValue) : self{
47 $result = new self;
48 $result->abilityId = $abilityId;
49 $result->abilityValue = $abilityValue;
50 return $result;
51 }
52
53 public function getAbilityId() : int{ return $this->abilityId; }
54
55 public function getAbilityValue() : float|bool{ return $this->abilityValue; }
56
57 protected function decodePayload(ByteBufferReader $in) : void{
58 $this->abilityId = VarInt::readSignedInt($in);
59
60 $valueType = Byte::readUnsigned($in);
61
62 //what is the point of having a type ID if you just write all the types anyway ??? mojang ...
63 //only one of these values is ever used; the other(s) are discarded
64 $boolValue = CommonTypes::getBool($in);
65 $floatValue = LE::readFloat($in);
66
67 $this->abilityValue = match($valueType){
68 self::VALUE_TYPE_BOOL => $boolValue,
69 self::VALUE_TYPE_FLOAT => $floatValue,
70 default => throw new PacketDecodeException("Unknown ability value type $valueType")
71 };
72 }
73
74 protected function encodePayload(ByteBufferWriter $out) : void{
75 VarInt::writeSignedInt($out, $this->abilityId);
76
77 [$valueType, $boolValue, $floatValue] = match(true){
78 is_bool($this->abilityValue) => [self::VALUE_TYPE_BOOL, $this->abilityValue, 0.0],
79 is_float($this->abilityValue) => [self::VALUE_TYPE_FLOAT, false, $this->abilityValue],
80 default => throw new \LogicException("Unreachable")
81 };
82 Byte::writeUnsigned($out, $valueType);
83 CommonTypes::putBool($out, $boolValue);
84 LE::writeFloat($out, $floatValue);
85 }
86
87 public function handle(PacketHandlerInterface $handler) : bool{
88 return $handler->handleRequestAbility($this);
89 }
90}
static create(int $abilityId, float|bool $abilityValue)