Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use Permission objects instead of string #6638

Open
wants to merge 14 commits into
base: major-next
Choose a base branch
from
6 changes: 2 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ public function addOp(string $name) : void{
$this->operators->set(strtolower($name), true);

if(($player = $this->getPlayerExact($name)) !== null){
$player->setBasePermission(DefaultPermissions::ROOT_OPERATOR, true);
$player->setBasePermission(DefaultPermissions::GROUP_OPERATOR(), true);
}
$this->operators->save();
}
Expand All @@ -712,7 +712,7 @@ public function removeOp(string $name) : void{
}

if(($player = $this->getPlayerExact($name)) !== null){
$player->unsetBasePermission(DefaultPermissions::ROOT_OPERATOR);
$player->unsetBasePermission(DefaultPermissions::GROUP_OPERATOR());
}
$this->operators->save();
}
Expand Down Expand Up @@ -1007,8 +1007,6 @@ public function __construct(
)));
$this->logger->info($this->language->translate(KnownTranslationFactory::pocketmine_server_license($this->getName())));

DefaultPermissions::registerCorePermissions();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only just caught this, but this could be problematic if plugins register permissions that conflict with the built-in ones. RegistryTrait will only register the permissions when the first permission is accessed. Sure, technically SimpleCommandMap should trigger it but I don't feel happy about relying on that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protecting the namespace pocketmine. for registering permission could be a solution. I don't really like it but I don't see another way to do it atm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how that would work without pre-registering the permissions somehow. Maybe PermissionManager::make() should do it similar to how we setup states for RuntimeBlockStateRegistry.

Copy link
Contributor Author

@Dhaiven Dhaiven Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think plugins don't must remove/override a default permission

$this->commandMap = new SimpleCommandMap($this);

$this->craftingManager = CraftingManagerFromDataHelper::make(BedrockDataFiles::RECIPES);
Expand Down
3 changes: 2 additions & 1 deletion src/command/ClosureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace pocketmine\command;

use pocketmine\lang\Translatable;
use pocketmine\permission\Permission;
use pocketmine\utils\Utils;

/**
Expand All @@ -34,7 +35,7 @@ final class ClosureCommand extends Command{
private \Closure $execute;

/**
* @param string[] $permissions
* @param Permission[] $permissions
* @phpstan-param Execute $execute
*/
public function __construct(
Expand Down
31 changes: 15 additions & 16 deletions src/command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
use pocketmine\command\utils\CommandException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\lang\Translatable;
use pocketmine\permission\PermissionManager;
use pocketmine\permission\Permission;
use pocketmine\Server;
use pocketmine\utils\BroadcastLoggerForwarder;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Utils;
use function array_map;
use function array_values;
use function explode;
use function implode;
use function str_replace;

Expand Down Expand Up @@ -63,7 +64,7 @@ abstract class Command{

protected Translatable|string $usageMessage;

/** @var string[] */
/** @var Permission[] */
private array $permission = [];
private Translatable|string|null $permissionMessage = null;

Expand Down Expand Up @@ -93,30 +94,25 @@ public function getName() : string{
}

/**
* @return string[]
* @return Permission[]
*/
public function getPermissions() : array{
return $this->permission;
}

/**
* @param string[] $permissions
* @param Permission[] $permissions
*/
public function setPermissions(array $permissions) : void{
$permissionManager = PermissionManager::getInstance();
foreach($permissions as $perm){
if($permissionManager->getPermission($perm) === null){
throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\"");
}
}
Utils::validateArrayValueType($permissions, function (Permission $permission) : void{});
$this->permission = $permissions;
}

public function setPermission(?string $permission) : void{
$this->setPermissions($permission === null ? [] : explode(";", $permission));
public function setPermission(?Permission $permission) : void{
$this->setPermissions($permission === null ? [] : [$permission]);
}

public function testPermission(CommandSender $target, ?string $permission = null) : bool{
public function testPermission(CommandSender $target, ?Permission $permission = null) : bool{
if($this->testPermissionSilent($target, $permission)){
return true;
}
Expand All @@ -125,13 +121,16 @@ public function testPermission(CommandSender $target, ?string $permission = null
if($message instanceof Translatable){
$target->sendMessage($message->prefix(TextFormat::RED));
}elseif($message !== ""){
$target->sendMessage(str_replace("<permission>", $permission ?? implode(";", $this->permission), $message));
$permissionsName = array_map(function (Permission $permission) : string{
return $permission->getName();
}, $this->permission);
$target->sendMessage(str_replace("<permission>", $permission?->getName() ?? implode(";", $permissionsName), $message));
}

return false;
}

public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{
public function testPermissionSilent(CommandSender $target, ?Permission $permission = null) : bool{
$list = $permission !== null ? [$permission] : $this->permission;
foreach($list as $p){
if($target->hasPermission($p)){
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/BanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\Player;
use function array_shift;
use function count;
Expand All @@ -41,7 +41,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_ban_player_description(),
KnownTranslationFactory::commands_ban_usage()
);
$this->setPermission(DefaultPermissionNames::COMMAND_BAN_PLAYER);
$this->setPermission(DefaultPermissions::COMMAND_BAN_PLAYER());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/BanIpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\Player;
use function array_shift;
use function count;
Expand All @@ -42,7 +42,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_ban_ip_description(),
KnownTranslationFactory::commands_banip_usage()
);
$this->setPermission(DefaultPermissionNames::COMMAND_BAN_IP);
$this->setPermission(DefaultPermissions::COMMAND_BAN_IP());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/BanListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\BanEntry;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use function array_map;
use function count;
use function implode;
Expand All @@ -43,7 +43,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_banlist_description(),
KnownTranslationFactory::commands_banlist_usage()
);
$this->setPermission(DefaultPermissionNames::COMMAND_BAN_LIST);
$this->setPermission(DefaultPermissions::COMMAND_BAN_LIST());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
9 changes: 6 additions & 3 deletions src/command/defaults/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
use pocketmine\item\LegacyStringToItemParserException;
use pocketmine\item\StringToItemParser;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\utils\TextFormat;
use function count;
use function min;
Expand All @@ -45,15 +45,18 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_clear_description(),
KnownTranslationFactory::pocketmine_command_clear_usage()
);
$this->setPermissions([DefaultPermissionNames::COMMAND_CLEAR_SELF, DefaultPermissionNames::COMMAND_CLEAR_OTHER]);
$this->setPermissions([
DefaultPermissions::COMMAND_CLEAR_SELF(),
DefaultPermissions::COMMAND_CLEAR_OTHER()
]);
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
if(count($args) > 3){
throw new InvalidCommandSyntaxException();
}

$target = $this->fetchPermittedPlayerTarget($sender, $args[0] ?? null, DefaultPermissionNames::COMMAND_CLEAR_SELF, DefaultPermissionNames::COMMAND_CLEAR_OTHER);
$target = $this->fetchPermittedPlayerTarget($sender, $args[0] ?? null, DefaultPermissions::COMMAND_CLEAR_SELF(), DefaultPermissions::COMMAND_CLEAR_OTHER());
if($target === null){
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/DefaultGamemodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\GameMode;
use pocketmine\ServerProperties;
use function count;
Expand All @@ -39,7 +39,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_defaultgamemode_description(),
KnownTranslationFactory::commands_defaultgamemode_usage()
);
$this->setPermission(DefaultPermissionNames::COMMAND_DEFAULTGAMEMODE);
$this->setPermission(DefaultPermissions::COMMAND_DEFAULTGAMEMODE());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/DeopCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\Player;
use pocketmine\utils\TextFormat;
use function array_shift;
Expand All @@ -41,7 +41,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_deop_description(),
KnownTranslationFactory::commands_deop_usage()
);
$this->setPermission(DefaultPermissionNames::COMMAND_OP_TAKE);
$this->setPermission(DefaultPermissions::COMMAND_OP_TAKE());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/DifficultyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\ServerProperties;
use pocketmine\world\World;
use function count;
Expand All @@ -40,7 +40,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_difficulty_description(),
KnownTranslationFactory::commands_difficulty_usage()
);
$this->setPermission(DefaultPermissionNames::COMMAND_DIFFICULTY);
$this->setPermission(DefaultPermissions::COMMAND_DIFFICULTY());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/DumpMemoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use pocketmine\command\CommandSender;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use Symfony\Component\Filesystem\Path;
use function date;

Expand All @@ -37,7 +37,7 @@ public function __construct(){
KnownTranslationFactory::pocketmine_command_dumpmemory_description(),
"/dumpmemory [path]"
);
$this->setPermission(DefaultPermissionNames::COMMAND_DUMPMEMORY);
$this->setPermission(DefaultPermissions::COMMAND_DUMPMEMORY());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
8 changes: 4 additions & 4 deletions src/command/defaults/EffectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use pocketmine\entity\effect\EffectInstance;
use pocketmine\entity\effect\StringToEffectParser;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\utils\Limits;
use pocketmine\utils\TextFormat;
use function count;
Expand All @@ -43,8 +43,8 @@ public function __construct(){
KnownTranslationFactory::commands_effect_usage()
);
$this->setPermissions([
DefaultPermissionNames::COMMAND_EFFECT_SELF,
DefaultPermissionNames::COMMAND_EFFECT_OTHER
DefaultPermissions::COMMAND_EFFECT_SELF(),
DefaultPermissions::COMMAND_EFFECT_OTHER()
]);
}

Expand All @@ -53,7 +53,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
throw new InvalidCommandSyntaxException();
}

$player = $this->fetchPermittedPlayerTarget($sender, $args[0], DefaultPermissionNames::COMMAND_EFFECT_SELF, DefaultPermissionNames::COMMAND_EFFECT_OTHER);
$player = $this->fetchPermittedPlayerTarget($sender, $args[0], DefaultPermissions::COMMAND_EFFECT_SELF(), DefaultPermissions::COMMAND_EFFECT_OTHER());
if($player === null){
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/command/defaults/EnchantCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\StringToEnchantmentParser;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use function count;

class EnchantCommand extends VanillaCommand{
Expand All @@ -41,8 +41,8 @@ public function __construct(){
KnownTranslationFactory::commands_enchant_usage()
);
$this->setPermissions([
DefaultPermissionNames::COMMAND_ENCHANT_SELF,
DefaultPermissionNames::COMMAND_ENCHANT_OTHER
DefaultPermissions::COMMAND_ENCHANT_SELF(),
DefaultPermissions::COMMAND_ENCHANT_OTHER()
]);
}

Expand All @@ -51,7 +51,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
throw new InvalidCommandSyntaxException();
}

$player = $this->fetchPermittedPlayerTarget($sender, $args[0], DefaultPermissionNames::COMMAND_ENCHANT_SELF, DefaultPermissionNames::COMMAND_ENCHANT_OTHER);
$player = $this->fetchPermittedPlayerTarget($sender, $args[0], DefaultPermissions::COMMAND_ENCHANT_SELF(), DefaultPermissions::COMMAND_ENCHANT_OTHER());
if($player === null){
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/command/defaults/GamemodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\player\GameMode;
use function count;

Expand All @@ -40,8 +40,8 @@ public function __construct(){
KnownTranslationFactory::commands_gamemode_usage()
);
$this->setPermissions([
DefaultPermissionNames::COMMAND_GAMEMODE_SELF,
DefaultPermissionNames::COMMAND_GAMEMODE_OTHER
DefaultPermissions::COMMAND_GAMEMODE_SELF(),
DefaultPermissions::COMMAND_GAMEMODE_OTHER()
]);
}

Expand All @@ -56,7 +56,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
return true;
}

$target = $this->fetchPermittedPlayerTarget($sender, $args[1] ?? null, DefaultPermissionNames::COMMAND_GAMEMODE_SELF, DefaultPermissionNames::COMMAND_GAMEMODE_OTHER);
$target = $this->fetchPermittedPlayerTarget($sender, $args[1] ?? null, DefaultPermissions::COMMAND_GAMEMODE_SELF(), DefaultPermissions::COMMAND_GAMEMODE_OTHER());
if($target === null){
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/defaults/GarbageCollectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

use pocketmine\command\CommandSender;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\permission\DefaultPermissions;
use pocketmine\utils\TextFormat;
use function count;
use function memory_get_usage;
Expand All @@ -39,7 +39,7 @@ public function __construct(){
"gc",
KnownTranslationFactory::pocketmine_command_gc_description()
);
$this->setPermission(DefaultPermissionNames::COMMAND_GC);
$this->setPermission(DefaultPermissions::COMMAND_GC());
}

public function execute(CommandSender $sender, string $commandLabel, array $args){
Expand Down
Loading
Loading