PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
49 public const NUMBER_OF_ABILITIES = 19;
50
55 public function __construct(
56 private int $layerId,
57 private array $boolAbilities,
58 private ?float $flySpeed,
59 private ?float $walkSpeed
60 ){}
61
62 public function getLayerId() : int{ return $this->layerId; }
63
69 public function getBoolAbilities() : array{ return $this->boolAbilities; }
70
71 public function getFlySpeed() : ?float{ return $this->flySpeed; }
72
73 public function getWalkSpeed() : ?float{ return $this->walkSpeed; }
74
75 public static function decode(PacketSerializer $in) : self{
76 $layerId = $in->getLShort();
77 $setAbilities = $in->getLInt();
78 $setAbilityValues = $in->getLInt();
79 $flySpeed = $in->getLFloat();
80 $walkSpeed = $in->getLFloat();
81
82 $boolAbilities = [];
83 for($i = 0; $i < self::NUMBER_OF_ABILITIES; $i++){
84 if($i === self::ABILITY_FLY_SPEED || $i === self::ABILITY_WALK_SPEED){
85 continue;
86 }
87 if(($setAbilities & (1 << $i)) !== 0){
88 $boolAbilities[$i] = ($setAbilityValues & (1 << $i)) !== 0;
89 }
90 }
91 if(($setAbilities & (1 << self::ABILITY_FLY_SPEED)) === 0){
92 if($flySpeed !== 0.0){
93 throw new PacketDecodeException("Fly speed should be zero if the layer does not set it");
94 }
95 $flySpeed = null;
96 }
97 if(($setAbilities & (1 << self::ABILITY_WALK_SPEED)) === 0){
98 if($walkSpeed !== 0.0){
99 throw new PacketDecodeException("Walk speed should be zero if the layer does not set it");
100 }
101 $walkSpeed = null;
102 }
103
104 return new self($layerId, $boolAbilities, $flySpeed, $walkSpeed);
105 }
106
107 public function encode(PacketSerializer $out) : void{
108 $out->putLShort($this->layerId);
109
110 $setAbilities = 0;
111 $setAbilityValues = 0;
112 foreach($this->boolAbilities as $ability => $value){
113 $setAbilities |= (1 << $ability);
114 $setAbilityValues |= ($value ? 1 << $ability : 0);
115 }
116 if($this->flySpeed !== null){
117 $setAbilities |= (1 << self::ABILITY_FLY_SPEED);
118 }
119 if($this->walkSpeed !== null){
120 $setAbilities |= (1 << self::ABILITY_WALK_SPEED);
121 }
122
123 $out->putLInt($setAbilities);
124 $out->putLInt($setAbilityValues);
125 $out->putLFloat($this->flySpeed ?? 0);
126 $out->putLFloat($this->walkSpeed ?? 0);
127 }
128}
__construct(private int $layerId, private array $boolAbilities, private ?float $flySpeed, private ?float $walkSpeed)