mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-10 13:35:29 +00:00
Use an array for command permissions
it doesn't make sense to have to parse the string every time we want to verify permissions, nor to expect that people will somehow know to use ; to separate them without it being documented anywhere...
This commit is contained in:
parent
cb10360c20
commit
caebe14dab
@ -33,7 +33,7 @@ use pocketmine\permission\PermissionManager;
|
|||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
use pocketmine\utils\BroadcastLoggerForwarder;
|
use pocketmine\utils\BroadcastLoggerForwarder;
|
||||||
use pocketmine\utils\TextFormat;
|
use pocketmine\utils\TextFormat;
|
||||||
use function explode;
|
use function implode;
|
||||||
use function str_replace;
|
use function str_replace;
|
||||||
|
|
||||||
abstract class Command{
|
abstract class Command{
|
||||||
@ -55,7 +55,8 @@ abstract class Command{
|
|||||||
|
|
||||||
protected Translatable|string $usageMessage;
|
protected Translatable|string $usageMessage;
|
||||||
|
|
||||||
private ?string $permission = null;
|
/** @var string[] */
|
||||||
|
private array $permission = [];
|
||||||
private ?string $permissionMessage = null;
|
private ?string $permissionMessage = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,19 +82,28 @@ abstract class Command{
|
|||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPermission() : ?string{
|
/**
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function getPermission() : array{
|
||||||
return $this->permission;
|
return $this->permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setPermission(?string $permission) : void{
|
/**
|
||||||
if($permission !== null){
|
* @param string[]|string|null $permissions
|
||||||
foreach(explode(";", $permission) as $perm){
|
*/
|
||||||
if(PermissionManager::getInstance()->getPermission($perm) === null){
|
public function setPermissions(array $permissions) : void{
|
||||||
throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\"");
|
$permissionManager = PermissionManager::getInstance();
|
||||||
}
|
foreach($permissions as $perm){
|
||||||
|
if($permissionManager->getPermission($perm) === null){
|
||||||
|
throw new \InvalidArgumentException("Cannot use non-existing permission \"$perm\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$this->permission = $permission;
|
$this->permission = $permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPermission(?string $permission) : void{
|
||||||
|
$this->setPermissions($permission === null ? [] : explode(";", $permission));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPermission(CommandSender $target, ?string $permission = null) : bool{
|
public function testPermission(CommandSender $target, ?string $permission = null) : bool{
|
||||||
@ -104,19 +114,15 @@ abstract class Command{
|
|||||||
if($this->permissionMessage === null){
|
if($this->permissionMessage === null){
|
||||||
$target->sendMessage(KnownTranslationFactory::pocketmine_command_error_permission($this->name)->prefix(TextFormat::RED));
|
$target->sendMessage(KnownTranslationFactory::pocketmine_command_error_permission($this->name)->prefix(TextFormat::RED));
|
||||||
}elseif($this->permissionMessage !== ""){
|
}elseif($this->permissionMessage !== ""){
|
||||||
$target->sendMessage(str_replace("<permission>", $permission ?? $this->permission, $this->permissionMessage));
|
$target->sendMessage(str_replace("<permission>", $permission ?? implode(";", $this->permission), $this->permissionMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{
|
public function testPermissionSilent(CommandSender $target, ?string $permission = null) : bool{
|
||||||
$permission ??= $this->permission;
|
$list = $permission !== null ? [$permission] : $this->permission;
|
||||||
if($permission === null || $permission === ""){
|
foreach($list as $p){
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach(explode(";", $permission) as $p){
|
|
||||||
if($target->hasPermission($p)){
|
if($target->hasPermission($p)){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ class SimpleCommandMap implements CommandMap{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function register(string $fallbackPrefix, Command $command, ?string $label = null) : bool{
|
public function register(string $fallbackPrefix, Command $command, ?string $label = null) : bool{
|
||||||
if($command->getPermission() === null){
|
if(count($command->getPermission()) === 0){
|
||||||
throw new \InvalidArgumentException("Commands must have a permission set");
|
throw new \InvalidArgumentException("Commands must have a permission set");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ use pocketmine\lang\KnownTranslationFactory;
|
|||||||
use pocketmine\permission\DefaultPermissionNames;
|
use pocketmine\permission\DefaultPermissionNames;
|
||||||
use pocketmine\utils\TextFormat;
|
use pocketmine\utils\TextFormat;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
use function min;
|
use function min;
|
||||||
|
|
||||||
class ClearCommand extends VanillaCommand{
|
class ClearCommand extends VanillaCommand{
|
||||||
@ -46,7 +45,7 @@ class ClearCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_clear_description(),
|
KnownTranslationFactory::pocketmine_command_clear_description(),
|
||||||
KnownTranslationFactory::pocketmine_command_clear_usage()
|
KnownTranslationFactory::pocketmine_command_clear_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [DefaultPermissionNames::COMMAND_CLEAR_SELF, DefaultPermissionNames::COMMAND_CLEAR_OTHER]));
|
$this->setPermissions([DefaultPermissionNames::COMMAND_CLEAR_SELF, DefaultPermissionNames::COMMAND_CLEAR_OTHER]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -32,7 +32,6 @@ use pocketmine\permission\DefaultPermissionNames;
|
|||||||
use pocketmine\utils\Limits;
|
use pocketmine\utils\Limits;
|
||||||
use pocketmine\utils\TextFormat;
|
use pocketmine\utils\TextFormat;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
use function strtolower;
|
use function strtolower;
|
||||||
|
|
||||||
class EffectCommand extends VanillaCommand{
|
class EffectCommand extends VanillaCommand{
|
||||||
@ -43,10 +42,10 @@ class EffectCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_effect_description(),
|
KnownTranslationFactory::pocketmine_command_effect_description(),
|
||||||
KnownTranslationFactory::commands_effect_usage()
|
KnownTranslationFactory::commands_effect_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_EFFECT_SELF,
|
DefaultPermissionNames::COMMAND_EFFECT_SELF,
|
||||||
DefaultPermissionNames::COMMAND_EFFECT_OTHER
|
DefaultPermissionNames::COMMAND_EFFECT_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -30,7 +30,6 @@ use pocketmine\item\enchantment\StringToEnchantmentParser;
|
|||||||
use pocketmine\lang\KnownTranslationFactory;
|
use pocketmine\lang\KnownTranslationFactory;
|
||||||
use pocketmine\permission\DefaultPermissionNames;
|
use pocketmine\permission\DefaultPermissionNames;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
|
|
||||||
class EnchantCommand extends VanillaCommand{
|
class EnchantCommand extends VanillaCommand{
|
||||||
|
|
||||||
@ -40,10 +39,10 @@ class EnchantCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_enchant_description(),
|
KnownTranslationFactory::pocketmine_command_enchant_description(),
|
||||||
KnownTranslationFactory::commands_enchant_usage()
|
KnownTranslationFactory::commands_enchant_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_ENCHANT_SELF,
|
DefaultPermissionNames::COMMAND_ENCHANT_SELF,
|
||||||
DefaultPermissionNames::COMMAND_ENCHANT_OTHER
|
DefaultPermissionNames::COMMAND_ENCHANT_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -30,7 +30,6 @@ use pocketmine\lang\KnownTranslationFactory;
|
|||||||
use pocketmine\permission\DefaultPermissionNames;
|
use pocketmine\permission\DefaultPermissionNames;
|
||||||
use pocketmine\player\GameMode;
|
use pocketmine\player\GameMode;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
|
|
||||||
class GamemodeCommand extends VanillaCommand{
|
class GamemodeCommand extends VanillaCommand{
|
||||||
|
|
||||||
@ -40,10 +39,10 @@ class GamemodeCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_gamemode_description(),
|
KnownTranslationFactory::pocketmine_command_gamemode_description(),
|
||||||
KnownTranslationFactory::commands_gamemode_usage()
|
KnownTranslationFactory::commands_gamemode_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_GAMEMODE_SELF,
|
DefaultPermissionNames::COMMAND_GAMEMODE_SELF,
|
||||||
DefaultPermissionNames::COMMAND_GAMEMODE_OTHER
|
DefaultPermissionNames::COMMAND_GAMEMODE_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -47,10 +47,10 @@ class GiveCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_give_description(),
|
KnownTranslationFactory::pocketmine_command_give_description(),
|
||||||
KnownTranslationFactory::pocketmine_command_give_usage()
|
KnownTranslationFactory::pocketmine_command_give_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_GIVE_SELF,
|
DefaultPermissionNames::COMMAND_GIVE_SELF,
|
||||||
DefaultPermissionNames::COMMAND_GIVE_OTHER
|
DefaultPermissionNames::COMMAND_GIVE_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -30,7 +30,6 @@ use pocketmine\event\entity\EntityDamageEvent;
|
|||||||
use pocketmine\lang\KnownTranslationFactory;
|
use pocketmine\lang\KnownTranslationFactory;
|
||||||
use pocketmine\permission\DefaultPermissionNames;
|
use pocketmine\permission\DefaultPermissionNames;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
|
|
||||||
class KillCommand extends VanillaCommand{
|
class KillCommand extends VanillaCommand{
|
||||||
|
|
||||||
@ -41,7 +40,7 @@ class KillCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_kill_usage(),
|
KnownTranslationFactory::pocketmine_command_kill_usage(),
|
||||||
["suicide"]
|
["suicide"]
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [DefaultPermissionNames::COMMAND_KILL_SELF, DefaultPermissionNames::COMMAND_KILL_OTHER]));
|
$this->setPermissions([DefaultPermissionNames::COMMAND_KILL_SELF, DefaultPermissionNames::COMMAND_KILL_OTHER]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -32,7 +32,6 @@ use pocketmine\player\Player;
|
|||||||
use pocketmine\world\Position;
|
use pocketmine\world\Position;
|
||||||
use pocketmine\world\World;
|
use pocketmine\world\World;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
use function round;
|
use function round;
|
||||||
|
|
||||||
class SpawnpointCommand extends VanillaCommand{
|
class SpawnpointCommand extends VanillaCommand{
|
||||||
@ -43,10 +42,10 @@ class SpawnpointCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_spawnpoint_description(),
|
KnownTranslationFactory::pocketmine_command_spawnpoint_description(),
|
||||||
KnownTranslationFactory::commands_spawnpoint_usage()
|
KnownTranslationFactory::commands_spawnpoint_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_SPAWNPOINT_SELF,
|
DefaultPermissionNames::COMMAND_SPAWNPOINT_SELF,
|
||||||
DefaultPermissionNames::COMMAND_SPAWNPOINT_OTHER
|
DefaultPermissionNames::COMMAND_SPAWNPOINT_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -35,7 +35,6 @@ use pocketmine\utils\TextFormat;
|
|||||||
use pocketmine\world\World;
|
use pocketmine\world\World;
|
||||||
use function array_shift;
|
use function array_shift;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
use function round;
|
use function round;
|
||||||
|
|
||||||
class TeleportCommand extends VanillaCommand{
|
class TeleportCommand extends VanillaCommand{
|
||||||
@ -47,10 +46,10 @@ class TeleportCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::commands_tp_usage(),
|
KnownTranslationFactory::commands_tp_usage(),
|
||||||
["teleport"]
|
["teleport"]
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_TELEPORT_SELF,
|
DefaultPermissionNames::COMMAND_TELEPORT_SELF,
|
||||||
DefaultPermissionNames::COMMAND_TELEPORT_OTHER
|
DefaultPermissionNames::COMMAND_TELEPORT_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function findPlayer(CommandSender $sender, string $playerName) : ?Player{
|
private function findPlayer(CommandSender $sender, string $playerName) : ?Player{
|
||||||
|
@ -31,7 +31,6 @@ use pocketmine\permission\DefaultPermissionNames;
|
|||||||
use pocketmine\player\Player;
|
use pocketmine\player\Player;
|
||||||
use pocketmine\world\World;
|
use pocketmine\world\World;
|
||||||
use function count;
|
use function count;
|
||||||
use function implode;
|
|
||||||
|
|
||||||
class TimeCommand extends VanillaCommand{
|
class TimeCommand extends VanillaCommand{
|
||||||
|
|
||||||
@ -41,13 +40,13 @@ class TimeCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_time_description(),
|
KnownTranslationFactory::pocketmine_command_time_description(),
|
||||||
KnownTranslationFactory::pocketmine_command_time_usage()
|
KnownTranslationFactory::pocketmine_command_time_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_TIME_ADD,
|
DefaultPermissionNames::COMMAND_TIME_ADD,
|
||||||
DefaultPermissionNames::COMMAND_TIME_SET,
|
DefaultPermissionNames::COMMAND_TIME_SET,
|
||||||
DefaultPermissionNames::COMMAND_TIME_START,
|
DefaultPermissionNames::COMMAND_TIME_START,
|
||||||
DefaultPermissionNames::COMMAND_TIME_STOP,
|
DefaultPermissionNames::COMMAND_TIME_STOP,
|
||||||
DefaultPermissionNames::COMMAND_TIME_QUERY
|
DefaultPermissionNames::COMMAND_TIME_QUERY
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -39,10 +39,10 @@ class TitleCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_title_description(),
|
KnownTranslationFactory::pocketmine_command_title_description(),
|
||||||
KnownTranslationFactory::commands_title_usage()
|
KnownTranslationFactory::commands_title_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_TITLE_SELF,
|
DefaultPermissionNames::COMMAND_TITLE_SELF,
|
||||||
DefaultPermissionNames::COMMAND_TITLE_OTHER
|
DefaultPermissionNames::COMMAND_TITLE_OTHER
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
@ -44,14 +44,14 @@ class WhitelistCommand extends VanillaCommand{
|
|||||||
KnownTranslationFactory::pocketmine_command_whitelist_description(),
|
KnownTranslationFactory::pocketmine_command_whitelist_description(),
|
||||||
KnownTranslationFactory::commands_whitelist_usage()
|
KnownTranslationFactory::commands_whitelist_usage()
|
||||||
);
|
);
|
||||||
$this->setPermission(implode(";", [
|
$this->setPermissions([
|
||||||
DefaultPermissionNames::COMMAND_WHITELIST_RELOAD,
|
DefaultPermissionNames::COMMAND_WHITELIST_RELOAD,
|
||||||
DefaultPermissionNames::COMMAND_WHITELIST_ENABLE,
|
DefaultPermissionNames::COMMAND_WHITELIST_ENABLE,
|
||||||
DefaultPermissionNames::COMMAND_WHITELIST_DISABLE,
|
DefaultPermissionNames::COMMAND_WHITELIST_DISABLE,
|
||||||
DefaultPermissionNames::COMMAND_WHITELIST_LIST,
|
DefaultPermissionNames::COMMAND_WHITELIST_LIST,
|
||||||
DefaultPermissionNames::COMMAND_WHITELIST_ADD,
|
DefaultPermissionNames::COMMAND_WHITELIST_ADD,
|
||||||
DefaultPermissionNames::COMMAND_WHITELIST_REMOVE
|
DefaultPermissionNames::COMMAND_WHITELIST_REMOVE
|
||||||
]));
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
public function execute(CommandSender $sender, string $commandLabel, array $args){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user