PocketMine-MP 5.25.1 git-694aa17cc916a954b10fe12721c81b1dc73eecd5
Loading...
Searching...
No Matches
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 public const LAYER_LOADING_SCREEN = 5;
28
29 public const ABILITY_BUILD = 0;
30 public const ABILITY_MINE = 1;
31 public const ABILITY_DOORS_AND_SWITCHES = 2; //disabling this also disables dropping items (???)
32 public const ABILITY_OPEN_CONTAINERS = 3;
33 public const ABILITY_ATTACK_PLAYERS = 4;
34 public const ABILITY_ATTACK_MOBS = 5;
35 public const ABILITY_OPERATOR = 6;
36 public const ABILITY_TELEPORT = 7;
37 public const ABILITY_INVULNERABLE = 8;
38 public const ABILITY_FLYING = 9;
39 public const ABILITY_ALLOW_FLIGHT = 10;
40 public const ABILITY_INFINITE_RESOURCES = 11; //in vanilla they call this "instabuild", which is a bad name
41 public const ABILITY_LIGHTNING = 12; //???
42 private const ABILITY_FLY_SPEED = 13;
43 private const ABILITY_WALK_SPEED = 14;
44 public const ABILITY_MUTED = 15;
45 public const ABILITY_WORLD_BUILDER = 16;
46 public const ABILITY_NO_CLIP = 17;
47 public const ABILITY_PRIVILEGED_BUILDER = 18;
48 public const ABILITY_VERTICAL_FLY_SPEED = 19;
49
50 public const NUMBER_OF_ABILITIES = 20;
51
56 public function __construct(
57 private int $layerId,
58 private array $boolAbilities,
59 private ?float $flySpeed,
60 private ?float $verticalFlySpeed,
61 private ?float $walkSpeed
62 ){}
63
64 public function getLayerId() : int{ return $this->layerId; }
65
71 public function getBoolAbilities() : array{ return $this->boolAbilities; }
72
73 public function getFlySpeed() : ?float{ return $this->flySpeed; }
74
75 public function getVerticalFlySpeed() : ?float{ return $this->verticalFlySpeed; }
76
77 public function getWalkSpeed() : ?float{ return $this->walkSpeed; }
78
79 public static function decode(PacketSerializer $in) : self{
80 $layerId = $in->getLShort();
81 $setAbilities = $in->getLInt();
82 $setAbilityValues = $in->getLInt();
83 $flySpeed = $in->getLFloat();
84 $verticalFlySpeed = $in->getLFloat();
85 $walkSpeed = $in->getLFloat();
86
87 $boolAbilities = [];
88 for($i = 0; $i < self::NUMBER_OF_ABILITIES; $i++){
89 if($i === self::ABILITY_FLY_SPEED || $i === self::ABILITY_WALK_SPEED){
90 continue;
91 }
92 if(($setAbilities & (1 << $i)) !== 0){
93 $boolAbilities[$i] = ($setAbilityValues & (1 << $i)) !== 0;
94 }
95 }
96 if(($setAbilities & (1 << self::ABILITY_FLY_SPEED)) === 0){
97 if($flySpeed !== 0.0){
98 throw new PacketDecodeException("Fly speed should be zero if the layer does not set it");
99 }
100 $flySpeed = null;
101 }
102 if(($setAbilities & (1 << self::ABILITY_VERTICAL_FLY_SPEED)) === 0){
103 if($verticalFlySpeed !== 0.0){
104 throw new PacketDecodeException("Vertical fly speed should be zero if the layer does not set it");
105 }
106 $verticalFlySpeed = null;
107 }
108 if(($setAbilities & (1 << self::ABILITY_WALK_SPEED)) === 0){
109 if($walkSpeed !== 0.0){
110 throw new PacketDecodeException("Walk speed should be zero if the layer does not set it");
111 }
112 $walkSpeed = null;
113 }
114
115 return new self($layerId, $boolAbilities, $flySpeed, $verticalFlySpeed, $walkSpeed);
116 }
117
118 public function encode(PacketSerializer $out) : void{
119 $out->putLShort($this->layerId);
120
121 $setAbilities = 0;
122 $setAbilityValues = 0;
123 foreach($this->boolAbilities as $ability => $value){
124 $setAbilities |= (1 << $ability);
125 $setAbilityValues |= ($value ? 1 << $ability : 0);
126 }
127 if($this->flySpeed !== null){
128 $setAbilities |= (1 << self::ABILITY_FLY_SPEED);
129 }
130 if($this->verticalFlySpeed !== null){
131 $setAbilities |= (1 << self::ABILITY_VERTICAL_FLY_SPEED);
132 }
133 if($this->walkSpeed !== null){
134 $setAbilities |= (1 << self::ABILITY_WALK_SPEED);
135 }
136
137 $out->putLInt($setAbilities);
138 $out->putLInt($setAbilityValues);
139 $out->putLFloat($this->flySpeed ?? 0);
140 $out->putLFloat($this->verticalFlySpeed ?? 0);
141 $out->putLFloat($this->walkSpeed ?? 0);
142 }
143}
__construct(private int $layerId, private array $boolAbilities, private ?float $flySpeed, private ?float $verticalFlySpeed, private ?float $walkSpeed)