Skip to content

Commit 08bdae3

Browse files
committed
Switch back to constants instead of enums
1 parent 8e04173 commit 08bdae3

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

src/types/AbilitiesData.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use pmmp\encoding\ByteBufferReader;
1919
use pmmp\encoding\ByteBufferWriter;
2020
use pmmp\encoding\LE;
21-
use pocketmine\network\mcpe\protocol\types\command\CommandPermissions;
2221
use function count;
2322

2423
final class AbilitiesData{
@@ -27,13 +26,13 @@ final class AbilitiesData{
2726
* @phpstan-param array<int, AbilitiesLayer> $abilityLayers
2827
*/
2928
public function __construct(
30-
private CommandPermissions $commandPermission,
29+
private int $commandPermission,
3130
private int $playerPermission,
3231
private int $targetActorUniqueId, //This is a little-endian long, NOT a var-long. (WTF Mojang)
3332
private array $abilityLayers
3433
){}
3534

36-
public function getCommandPermission() : CommandPermissions{ return $this->commandPermission; }
35+
public function getCommandPermission() : int{ return $this->commandPermission; }
3736

3837
public function getPlayerPermission() : int{ return $this->playerPermission; }
3938

@@ -48,7 +47,7 @@ public function getAbilityLayers() : array{ return $this->abilityLayers; }
4847
public static function decode(ByteBufferReader $in) : self{
4948
$targetActorUniqueId = LE::readSignedLong($in); //WHY IS THIS NON-STANDARD?
5049
$playerPermission = Byte::readUnsigned($in);
51-
$commandPermission = CommandPermissions::fromPacket(Byte::readUnsigned($in));
50+
$commandPermission = Byte::readUnsigned($in);
5251

5352
$abilityLayers = [];
5453
for($i = 0, $len = Byte::readUnsigned($in); $i < $len; $i++){
@@ -61,7 +60,7 @@ public static function decode(ByteBufferReader $in) : self{
6160
public function encode(ByteBufferWriter $out) : void{
6261
LE::writeSignedLong($out, $this->targetActorUniqueId);
6362
Byte::writeUnsigned($out, $this->playerPermission);
64-
Byte::writeUnsigned($out, $this->commandPermission->value);
63+
Byte::writeUnsigned($out, $this->commandPermission);
6564

6665
Byte::writeUnsigned($out, count($this->abilityLayers));
6766
foreach($this->abilityLayers as $abilityLayer){

src/types/command/CommandData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
public string $name,
2424
public string $description,
2525
public int $flags,
26-
public CommandPermissions $permission,
26+
public string $permission,
2727
public ?CommandHardEnum $aliases,
2828
public array $overloads,
2929
public array $chainedSubCommandData
@@ -44,7 +44,7 @@ public function getFlags() : int{
4444
return $this->flags;
4545
}
4646

47-
public function getPermission() : CommandPermissions{
47+
public function getPermission() : string{
4848
return $this->permission;
4949
}
5050

src/types/command/CommandPermissions.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
use pocketmine\network\mcpe\protocol\types\PacketIntEnumTrait;
1919
use function array_flip;
2020

21-
enum CommandPermissions : int{
22-
use PacketIntEnumTrait;
21+
final class CommandPermissions{
22+
private function __construct(){
23+
//NOOP
24+
}
2325

24-
case NORMAL = 0;
25-
case OPERATOR = 1;
26-
case AUTOMATION = 2; //command blocks
27-
case HOST = 3; //hosting player on LAN multiplayer
28-
case OWNER = 4; //server terminal on BDS
29-
case INTERNAL = 5;
26+
public const NORMAL = 0;
27+
public const OPERATOR = 1;
28+
public const AUTOMATION = 2; //command blocks
29+
public const HOST = 3; //hosting player on LAN multiplayer
30+
public const OWNER = 4; //server terminal on BDS
31+
public const INTERNAL = 5;
3032

3133
private const PERMISSION_NAMES = [ // enum case references requires PHP 8.2
3234
0 => "any",
@@ -37,18 +39,18 @@ enum CommandPermissions : int{
3739
5 => "internal",
3840
];
3941

40-
public function getPermissionName() : string{
41-
return self::PERMISSION_NAMES[$this->value];
42+
public static function getPermissionName(int $value) : string{
43+
return self::PERMISSION_NAMES[$value] ?? throw new PacketDecodeException("Invalid raw value $value for " . static::class);
4244
}
4345

44-
public static function fromPermissionName(string $name) : self{
46+
public static function fromPermissionName(string $name) : int{
4547
static $cache = null;
4648
if($cache === null){
4749
$cache = array_flip(self::PERMISSION_NAMES);
4850
}
4951

5052
$value = $cache[$name] ?? throw new PacketDecodeException("Invalid raw value $name for " . static::class);
5153

52-
return self::fromPacket($value);
54+
return $value;
5355
}
5456
}

src/types/command/raw/CommandRawData.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use pmmp\encoding\LE;
2020
use pmmp\encoding\VarInt;
2121
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
22-
use pocketmine\network\mcpe\protocol\types\command\CommandPermissions;
2322
use function count;
2423

2524
final class CommandRawData{
@@ -34,7 +33,7 @@ public function __construct(
3433
private string $name,
3534
private string $description,
3635
private int $flags,
37-
private CommandPermissions $permission,
36+
private string $permission,
3837
private int $aliasEnumIndex,
3938
private array $chainedSubCommandDataIndexes,
4039
private array $overloads,
@@ -46,7 +45,7 @@ public function getDescription() : string{ return $this->description; }
4645

4746
public function getFlags() : int{ return $this->flags; }
4847

49-
public function getPermission() : CommandPermissions{ return $this->permission; }
48+
public function getPermission() : string{ return $this->permission; }
5049

5150
public function getAliasEnumIndex() : int{ return $this->aliasEnumIndex; }
5251

@@ -66,7 +65,7 @@ public static function read(ByteBufferReader $in) : self{
6665
$name = CommonTypes::getString($in);
6766
$description = CommonTypes::getString($in);
6867
$flags = LE::readUnsignedShort($in);
69-
$permission = CommandPermissions::fromPermissionName(CommonTypes::getString($in));
68+
$permission = CommonTypes::getString($in);
7069
$aliasEnumIndex = LE::readSignedInt($in); //may be -1 for not set
7170

7271
$chainedSubCommandDataIndexes = [];
@@ -94,7 +93,7 @@ public function write(ByteBufferWriter $out) : void{
9493
CommonTypes::putString($out, $this->name);
9594
CommonTypes::putString($out, $this->description);
9695
LE::writeUnsignedShort($out, $this->flags);
97-
CommonTypes::putString($out, $this->permission->getPermissionName());
96+
CommonTypes::putString($out, $this->permission);
9897
LE::writeSignedInt($out, $this->aliasEnumIndex);
9998

10099
VarInt::writeUnsignedInt($out, count($this->chainedSubCommandDataIndexes));

0 commit comments

Comments
 (0)