PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
AbilitiesLayer.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\types;
16
19
20final class AbilitiesLayer{
21
22 public const LAYER_CACHE = 0;
23 public const LAYER_BASE = 1;
24 public const LAYER_SPECTATOR = 2;
25 public const LAYER_COMMANDS = 3;
26 public const LAYER_EDITOR = 4;
27
28 public const ABILITY_BUILD = 0;
29 public const ABILITY_MINE = 1;
30 public const ABILITY_DOORS_AND_SWITCHES = 2; //disabling this also disables dropping items (???)
31 public const ABILITY_OPEN_CONTAINERS = 3;
32 public const ABILITY_ATTACK_PLAYERS = 4;
33 public const ABILITY_ATTACK_MOBS = 5;
34 public const ABILITY_OPERATOR = 6;
35 public const ABILITY_TELEPORT = 7;
36 public const ABILITY_INVULNERABLE = 8;
37 public const ABILITY_FLYING = 9;
38 public const ABILITY_ALLOW_FLIGHT = 10;
39 public const ABILITY_INFINITE_RESOURCES = 11; //in vanilla they call this "instabuild", which is a bad name
40 public const ABILITY_LIGHTNING = 12; //???
41 private const ABILITY_FLY_SPEED = 13;
42 private const ABILITY_WALK_SPEED = 14;
43 public const ABILITY_MUTED = 15;
44 public const ABILITY_WORLD_BUILDER = 16;
45 public const ABILITY_NO_CLIP = 17;
46 public const ABILITY_PRIVILEGED_BUILDER = 18;
47
48 public const NUMBER_OF_ABILITIES = 19;
49
54 public function __construct(
55 private int $layerId,
56 private array $boolAbilities,
57 private ?float $flySpeed,
58 private ?float $walkSpeed
59 ){}
60
61 public function getLayerId() : int{ return $this->layerId; }
62
68 public function getBoolAbilities() : array{ return $this->boolAbilities; }
69
70 public function getFlySpeed() : ?float{ return $this->flySpeed; }
71
72 public function getWalkSpeed() : ?float{ return $this->walkSpeed; }
73
74 public static function decode(PacketSerializer $in) : self{
75 $layerId = $in->getLShort();
76 $setAbilities = $in->getLInt();
77 $setAbilityValues = $in->getLInt();
78 $flySpeed = $in->getLFloat();
79 $walkSpeed = $in->getLFloat();
80
81 $boolAbilities = [];
82 for($i = 0; $i < self::NUMBER_OF_ABILITIES; $i++){
83 if($i === self::ABILITY_FLY_SPEED || $i === self::ABILITY_WALK_SPEED){
84 continue;
85 }
86 if(($setAbilities & (1 << $i)) !== 0){
87 $boolAbilities[$i] = ($setAbilityValues & (1 << $i)) !== 0;
88 }
89 }
90 if(($setAbilities & (1 << self::ABILITY_FLY_SPEED)) === 0){
91 if($flySpeed !== 0.0){
92 throw new PacketDecodeException("Fly speed should be zero if the layer does not set it");
93 }
94 $flySpeed = null;
95 }
96 if(($setAbilities & (1 << self::ABILITY_WALK_SPEED)) === 0){
97 if($walkSpeed !== 0.0){
98 throw new PacketDecodeException("Walk speed should be zero if the layer does not set it");
99 }
100 $walkSpeed = null;
101 }
102
103 return new self($layerId, $boolAbilities, $flySpeed, $walkSpeed);
104 }
105
106 public function encode(PacketSerializer $out) : void{
107 $out->putLShort($this->layerId);
108
109 $setAbilities = 0;
110 $setAbilityValues = 0;
111 foreach($this->boolAbilities as $ability => $value){
112 $setAbilities |= (1 << $ability);
113 $setAbilityValues |= ($value ? 1 << $ability : 0);
114 }
115 if($this->flySpeed !== null){
116 $setAbilities |= (1 << self::ABILITY_FLY_SPEED);
117 }
118 if($this->walkSpeed !== null){
119 $setAbilities |= (1 << self::ABILITY_WALK_SPEED);
120 }
121
122 $out->putLInt($setAbilities);
123 $out->putLInt($setAbilityValues);
124 $out->putLFloat($this->flySpeed ?? 0);
125 $out->putLFloat($this->walkSpeed ?? 0);
126 }
127}
__construct(private int $layerId, private array $boolAbilities, private ?float $flySpeed, private ?float $walkSpeed)