From 3238b4ff33ec3caa06b4547d2838930dea20937b Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 11 May 2020 19:25:52 +0100 Subject: [PATCH] Remove PluginIdentifiableCommand in favour of a more generic PluginOwned interface --- src/Server.php | 8 ++-- src/command/PluginCommand.php | 12 ++---- src/plugin/PluginBase.php | 8 ++-- .../PluginOwned.php} | 11 ++--- src/plugin/PluginOwnedTrait.php | 40 +++++++++++++++++++ tests/plugins/PocketMine-DevTools | 2 +- 6 files changed, 60 insertions(+), 21 deletions(-) rename src/{command/PluginIdentifiableCommand.php => plugin/PluginOwned.php} (76%) create mode 100644 src/plugin/PluginOwnedTrait.php diff --git a/src/Server.php b/src/Server.php index 20347fb04..22cb9a9ab 100644 --- a/src/Server.php +++ b/src/Server.php @@ -27,10 +27,10 @@ declare(strict_types=1); */ namespace pocketmine; +use pocketmine\command\Command; use pocketmine\command\CommandReader; use pocketmine\command\CommandSender; use pocketmine\command\ConsoleCommandSender; -use pocketmine\command\PluginIdentifiableCommand; use pocketmine\command\SimpleCommandMap; use pocketmine\crafting\CraftingManager; use pocketmine\crafting\CraftingManagerFromDataHelper; @@ -71,6 +71,7 @@ use pocketmine\plugin\Plugin; use pocketmine\plugin\PluginGraylist; use pocketmine\plugin\PluginLoadOrder; use pocketmine\plugin\PluginManager; +use pocketmine\plugin\PluginOwned; use pocketmine\plugin\ScriptPluginLoader; use pocketmine\resourcepacks\ResourcePackManager; use pocketmine\scheduler\AsyncPool; @@ -694,10 +695,11 @@ class Server{ } /** - * @return PluginIdentifiableCommand|null + * @return Command|PluginOwned|null + * @phpstan-return (Command&PluginOwned)|null */ public function getPluginCommand(string $name){ - if(($command = $this->commandMap->getCommand($name)) instanceof PluginIdentifiableCommand){ + if(($command = $this->commandMap->getCommand($name)) instanceof PluginOwned){ return $command; }else{ return null; diff --git a/src/command/PluginCommand.php b/src/command/PluginCommand.php index 827239c26..d4c0abf64 100644 --- a/src/command/PluginCommand.php +++ b/src/command/PluginCommand.php @@ -25,11 +25,11 @@ namespace pocketmine\command; use pocketmine\command\utils\InvalidCommandSyntaxException; use pocketmine\plugin\Plugin; +use pocketmine\plugin\PluginOwned; +use pocketmine\plugin\PluginOwnedTrait; -class PluginCommand extends Command implements PluginIdentifiableCommand{ - - /** @var Plugin */ - private $owningPlugin; +class PluginCommand extends Command implements PluginOwned{ + use PluginOwnedTrait; /** @var CommandExecutor */ private $executor; @@ -67,8 +67,4 @@ class PluginCommand extends Command implements PluginIdentifiableCommand{ public function setExecutor(CommandExecutor $executor) : void{ $this->executor = $executor; } - - public function getPlugin() : Plugin{ - return $this->owningPlugin; - } } diff --git a/src/plugin/PluginBase.php b/src/plugin/PluginBase.php index 63ddd10a7..8f5c0d3b8 100644 --- a/src/plugin/PluginBase.php +++ b/src/plugin/PluginBase.php @@ -27,7 +27,6 @@ use pocketmine\command\Command; use pocketmine\command\CommandExecutor; use pocketmine\command\CommandSender; use pocketmine\command\PluginCommand; -use pocketmine\command\PluginIdentifiableCommand; use pocketmine\scheduler\TaskScheduler; use pocketmine\Server; use pocketmine\utils\AssumptionFailedError; @@ -223,15 +222,16 @@ abstract class PluginBase implements Plugin, CommandExecutor{ } /** - * @return Command|PluginIdentifiableCommand|null + * @return Command|PluginOwned|null + * @phpstan-return (Command&PluginOwned)|null */ public function getCommand(string $name){ $command = $this->getServer()->getPluginCommand($name); - if($command === null or $command->getPlugin() !== $this){ + if($command === null or $command->getOwningPlugin() !== $this){ $command = $this->getServer()->getPluginCommand(strtolower($this->description->getName()) . ":" . $name); } - if($command instanceof PluginIdentifiableCommand and $command->getPlugin() === $this){ + if($command instanceof PluginOwned and $command->getOwningPlugin() === $this){ return $command; }else{ return null; diff --git a/src/command/PluginIdentifiableCommand.php b/src/plugin/PluginOwned.php similarity index 76% rename from src/command/PluginIdentifiableCommand.php rename to src/plugin/PluginOwned.php index 02e85f7e9..8497c7866 100644 --- a/src/command/PluginIdentifiableCommand.php +++ b/src/plugin/PluginOwned.php @@ -21,11 +21,12 @@ declare(strict_types=1); -namespace pocketmine\command; +namespace pocketmine\plugin; -use pocketmine\plugin\Plugin; +/** + * This interface may be implemented by objects which are owned by plugins, to allow them to be identified as such. + */ +interface PluginOwned{ -interface PluginIdentifiableCommand{ - - public function getPlugin() : Plugin; + public function getOwningPlugin() : Plugin; } diff --git a/src/plugin/PluginOwnedTrait.php b/src/plugin/PluginOwnedTrait.php new file mode 100644 index 000000000..278108c60 --- /dev/null +++ b/src/plugin/PluginOwnedTrait.php @@ -0,0 +1,40 @@ +owningPlugin = $owningPlugin; + } + + public function getOwningPlugin() : Plugin{ + return $this->owningPlugin; + } +} diff --git a/tests/plugins/PocketMine-DevTools b/tests/plugins/PocketMine-DevTools index beb079c25..aef2b1018 160000 --- a/tests/plugins/PocketMine-DevTools +++ b/tests/plugins/PocketMine-DevTools @@ -1 +1 @@ -Subproject commit beb079c256eea7cae0f68f02e2b61096ddc00690 +Subproject commit aef2b101855762bf02638dfdb64bec585f5cba68