PocketMine-MP 5.18.1 git-9381fc4172e5dce4cada1cb356050c8a2ab57b94
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
18use function is_bool;
19use function is_float;
20
27 public const NETWORK_ID = ProtocolInfo::REQUEST_ABILITY_PACKET;
28
29 private const VALUE_TYPE_BOOL = 1;
30 private const VALUE_TYPE_FLOAT = 2;
31
32 public const ABILITY_FLYING = 9;
33 public const ABILITY_NOCLIP = 17;
34
35 private int $abilityId;
36 private float|bool $abilityValue;
37
41 public static function create(int $abilityId, float|bool $abilityValue) : self{
42 $result = new self;
43 $result->abilityId = $abilityId;
44 $result->abilityValue = $abilityValue;
45 return $result;
46 }
47
48 public function getAbilityId() : int{ return $this->abilityId; }
49
50 public function getAbilityValue() : float|bool{ return $this->abilityValue; }
51
52 protected function decodePayload(PacketSerializer $in) : void{
53 $this->abilityId = $in->getVarInt();
54
55 $valueType = $in->getByte();
56
57 //what is the point of having a type ID if you just write all the types anyway ??? mojang ...
58 //only one of these values is ever used; the other(s) are discarded
59 $boolValue = $in->getBool();
60 $floatValue = $in->getLFloat();
61
62 $this->abilityValue = match($valueType){
63 self::VALUE_TYPE_BOOL => $boolValue,
64 self::VALUE_TYPE_FLOAT => $floatValue,
65 default => throw new PacketDecodeException("Unknown ability value type $valueType")
66 };
67 }
68
69 protected function encodePayload(PacketSerializer $out) : void{
70 $out->putVarInt($this->abilityId);
71
72 [$valueType, $boolValue, $floatValue] = match(true){
73 is_bool($this->abilityValue) => [self::VALUE_TYPE_BOOL, $this->abilityValue, 0.0],
74 is_float($this->abilityValue) => [self::VALUE_TYPE_FLOAT, false, $this->abilityValue],
75 default => throw new \LogicException("Unreachable")
76 };
77 $out->putByte($valueType);
78 $out->putBool($boolValue);
79 $out->putLFloat($floatValue);
80 }
81
82 public function handle(PacketHandlerInterface $handler) : bool{
83 return $handler->handleRequestAbility($this);
84 }
85}
static create(int $abilityId, float|bool $abilityValue)