From 3c677bd3ec3584572f84c4728d65179302705ea2 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 17 Jun 2019 16:10:13 +0100 Subject: [PATCH] added PermissibleDelegateTrait to cut down boilerplate in Player and ConsoleCommandSender --- src/pocketmine/Player.php | 59 ++------------ .../command/ConsoleCommandSender.php | 57 +------------ .../permission/PermissibleDelegateTrait.php | 81 +++++++++++++++++++ 3 files changed, 88 insertions(+), 109 deletions(-) create mode 100644 src/pocketmine/permission/PermissibleDelegateTrait.php diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 38ef47229..081e95198 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -101,8 +101,7 @@ use pocketmine\network\mcpe\protocol\types\EntityMetadataFlags; use pocketmine\network\mcpe\protocol\types\EntityMetadataProperties; use pocketmine\network\mcpe\protocol\types\PlayerMetadataFlags; use pocketmine\permission\PermissibleBase; -use pocketmine\permission\PermissionAttachment; -use pocketmine\permission\PermissionAttachmentInfo; +use pocketmine\permission\PermissibleDelegateTrait; use pocketmine\permission\PermissionManager; use pocketmine\plugin\Plugin; use pocketmine\timings\Timings; @@ -144,6 +143,9 @@ use const PHP_INT_MAX; * Main class that handles networking, recovery, and packet sending to the server part */ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, IPlayer{ + use PermissibleDelegateTrait { + recalculatePermissions as private delegateRecalculatePermissions; + } /** * Checks a supplied username and checks it is valid. @@ -253,9 +255,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, /** @var bool */ protected $flying = false; - /** @var PermissibleBase */ - private $perm = null; - /** @var int|null */ protected $lineHeight = null; /** @var string */ @@ -690,47 +689,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, $this->networkSession->syncAdventureSettings($this); } - /** - * @param permission\Permission|string $name - * - * @return bool - */ - public function isPermissionSet($name) : bool{ - return $this->perm->isPermissionSet($name); - } - - /** - * @param permission\Permission|string $name - * - * @return bool - * - * @throws \InvalidStateException if the player is closed - */ - public function hasPermission($name) : bool{ - if($this->closed){ - throw new \InvalidStateException("Trying to get permissions of closed player"); - } - return $this->perm->hasPermission($name); - } - - /** - * @param Plugin $plugin - * @param string $name - * @param bool $value - * - * @return PermissionAttachment - */ - public function addAttachment(Plugin $plugin, ?string $name = null, ?bool $value = null) : PermissionAttachment{ - return $this->perm->addAttachment($plugin, $name, $value); - } - - /** - * @param PermissionAttachment $attachment - */ - public function removeAttachment(PermissionAttachment $attachment) : void{ - $this->perm->removeAttachment($attachment); - } - public function recalculatePermissions() : void{ $permManager = PermissionManager::getInstance(); $permManager->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this); @@ -740,7 +698,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, return; } - $this->perm->recalculatePermissions(); + $this->delegateRecalculatePermissions(); if($this->spawned){ if($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)){ @@ -754,13 +712,6 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, } } - /** - * @return PermissionAttachmentInfo[] - */ - public function getEffectivePermissions() : array{ - return $this->perm->getEffectivePermissions(); - } - /** * @return bool */ diff --git a/src/pocketmine/command/ConsoleCommandSender.php b/src/pocketmine/command/ConsoleCommandSender.php index 7834c4399..a80a3c235 100644 --- a/src/pocketmine/command/ConsoleCommandSender.php +++ b/src/pocketmine/command/ConsoleCommandSender.php @@ -25,18 +25,14 @@ namespace pocketmine\command; use pocketmine\lang\TextContainer; use pocketmine\permission\PermissibleBase; -use pocketmine\permission\Permission; -use pocketmine\permission\PermissionAttachment; -use pocketmine\permission\PermissionAttachmentInfo; -use pocketmine\plugin\Plugin; +use pocketmine\permission\PermissibleDelegateTrait; use pocketmine\Server; use function explode; use function trim; use const PHP_INT_MAX; class ConsoleCommandSender implements CommandSender{ - - private $perm; + use PermissibleDelegateTrait; /** @var int|null */ protected $lineHeight = null; @@ -45,55 +41,6 @@ class ConsoleCommandSender implements CommandSender{ $this->perm = new PermissibleBase($this); } - /** - * @param Permission|string $name - * - * @return bool - */ - public function isPermissionSet($name) : bool{ - return $this->perm->isPermissionSet($name); - } - - /** - * @param Permission|string $name - * - * @return bool - */ - public function hasPermission($name) : bool{ - return $this->perm->hasPermission($name); - } - - /** - * @param Plugin $plugin - * @param string $name - * @param bool $value - * - * @return PermissionAttachment - */ - public function addAttachment(Plugin $plugin, ?string $name = null, ?bool $value = null) : PermissionAttachment{ - return $this->perm->addAttachment($plugin, $name, $value); - } - - /** - * @param PermissionAttachment $attachment - * - * @return void - */ - public function removeAttachment(PermissionAttachment $attachment) : void{ - $this->perm->removeAttachment($attachment); - } - - public function recalculatePermissions() : void{ - $this->perm->recalculatePermissions(); - } - - /** - * @return PermissionAttachmentInfo[] - */ - public function getEffectivePermissions() : array{ - return $this->perm->getEffectivePermissions(); - } - /** * @return Server */ diff --git a/src/pocketmine/permission/PermissibleDelegateTrait.php b/src/pocketmine/permission/PermissibleDelegateTrait.php new file mode 100644 index 000000000..a6bab9e4c --- /dev/null +++ b/src/pocketmine/permission/PermissibleDelegateTrait.php @@ -0,0 +1,81 @@ +perm->isPermissionSet($name); + } + + /** + * @param Permission|string $name + * + * @return bool + */ + public function hasPermission($name) : bool{ + return $this->perm->hasPermission($name); + } + + /** + * @param Plugin $plugin + * @param string $name + * @param bool $value + * + * @return PermissionAttachment + */ + public function addAttachment(Plugin $plugin, ?string $name = null, ?bool $value = null) : PermissionAttachment{ + return $this->perm->addAttachment($plugin, $name, $value); + } + + /** + * @param PermissionAttachment $attachment + */ + public function removeAttachment(PermissionAttachment $attachment) : void{ + $this->perm->removeAttachment($attachment); + } + + public function recalculatePermissions() : void{ + $this->perm->recalculatePermissions(); + } + + /** + * @return PermissionAttachmentInfo[] + */ + public function getEffectivePermissions() : array{ + return $this->perm->getEffectivePermissions(); + } + +}