From d615397667095a89283267f8b67464883aedd42b Mon Sep 17 00:00:00 2001 From: MintoD Date: Tue, 9 Nov 2021 11:33:08 +0700 Subject: [PATCH] Add remove plugin command --- src/thebigcrafter/APM/commands/APMCommand.php | 2 ++ .../commands/subcommands/AddRepoCommand.php | 2 +- .../subcommands/InstallPluginCommand.php | 2 +- .../subcommands/RemovePluginCommand.php | 35 +++++++++++++++++++ .../subcommands/RemoveRepoCommand.php | 2 +- src/thebigcrafter/APM/forms/RepoForm.php | 23 ++++++++++++ src/thebigcrafter/APM/jobs/Remover.php | 15 ++++++++ 7 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/thebigcrafter/APM/commands/subcommands/RemovePluginCommand.php diff --git a/src/thebigcrafter/APM/commands/APMCommand.php b/src/thebigcrafter/APM/commands/APMCommand.php index c6b0052..983169c 100644 --- a/src/thebigcrafter/APM/commands/APMCommand.php +++ b/src/thebigcrafter/APM/commands/APMCommand.php @@ -13,6 +13,7 @@ use thebigcrafter\APM\forms\MenuForm; use pocketmine\command\CommandSender; use pocketmine\Player; +use thebigcrafter\APM\commands\subcommands\RemovePluginCommand; class APMCommand extends BaseCommand { @@ -26,6 +27,7 @@ protected function prepare(): void $this->registerSubCommand(new ListRepoCommand($this->getPlugin(), "list-repo", "List repositories")); $this->registerSubCommand(new UpdateCommand($this->getPlugin(), "update", "Update repositories")); $this->registerSubCommand(new InstallPluginCommand($this->getPlugin(), "install", "Install plugin")); + $this->registerSubCommand(new RemovePluginCommand($this->getPlugin(), "remove", "Remove plugin")); } public function onRun(CommandSender $sender, string $aliasUsed, array $args): void diff --git a/src/thebigcrafter/APM/commands/subcommands/AddRepoCommand.php b/src/thebigcrafter/APM/commands/subcommands/AddRepoCommand.php index 8f4efb3..364f435 100644 --- a/src/thebigcrafter/APM/commands/subcommands/AddRepoCommand.php +++ b/src/thebigcrafter/APM/commands/subcommands/AddRepoCommand.php @@ -34,7 +34,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo if (!isset($args["url"])) { return; } - if (Adder::addRepo($args["url"])) { + if (Adder::addRepo((string) $args["url"])) { $sender->sendMessage(APM::$PREFIX . TextFormat::GREEN . "Added!"); } else { $sender->sendMessage(APM::$PREFIX . TextFormat::DARK_RED . $args["url"] . " is not a valid URL!"); diff --git a/src/thebigcrafter/APM/commands/subcommands/InstallPluginCommand.php b/src/thebigcrafter/APM/commands/subcommands/InstallPluginCommand.php index e2f36f3..3447d58 100644 --- a/src/thebigcrafter/APM/commands/subcommands/InstallPluginCommand.php +++ b/src/thebigcrafter/APM/commands/subcommands/InstallPluginCommand.php @@ -26,7 +26,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo if ($sender instanceof Player) { $sender->sendForm(RepoForm::getInstallForm()); } else { - if (Installer::install($args["plugin name"])) { + if (Installer::install((string) $args["plugin name"])) { $sender->sendMessage(APM::$PREFIX . "§aPlugin installed successfully"); } else { $sender->sendMessage(APM::$PREFIX . "§4Plugin not found!"); diff --git a/src/thebigcrafter/APM/commands/subcommands/RemovePluginCommand.php b/src/thebigcrafter/APM/commands/subcommands/RemovePluginCommand.php new file mode 100644 index 0000000..7fb595e --- /dev/null +++ b/src/thebigcrafter/APM/commands/subcommands/RemovePluginCommand.php @@ -0,0 +1,35 @@ +setDescription("Removes a plugin"); + + $this->registerArgument(0, new RawStringArgument("plugin name"), "The plugin to remove", false); + } + + public function onRun(CommandSender $sender, string $aliasUsed, array $args): void + { + if ($sender instanceof Player) { + $sender->sendForm(RepoForm::getRemoveForm()); + } else { + if (Remover::removePlugin((string) $args["plugin name"])) { + $sender->sendMessage("§aPlugin removed successfully!"); + } else { + $sender->sendMessage("§cPlugin not found!"); + } + } + } +} diff --git a/src/thebigcrafter/APM/commands/subcommands/RemoveRepoCommand.php b/src/thebigcrafter/APM/commands/subcommands/RemoveRepoCommand.php index a3f24b0..aefe71b 100644 --- a/src/thebigcrafter/APM/commands/subcommands/RemoveRepoCommand.php +++ b/src/thebigcrafter/APM/commands/subcommands/RemoveRepoCommand.php @@ -23,7 +23,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo if (!isset($args["url"])) { return; } - if (Remover::removeRepo($args["url"])) { + if (Remover::removeRepo((string) $args["url"])) { $sender->sendMessage(APM::$PREFIX . TextFormat::GREEN . "Removed!"); } else { $sender->sendMessage(APM::$PREFIX . TextFormat::DARK_RED . $args["url"] . " not found!"); diff --git a/src/thebigcrafter/APM/forms/RepoForm.php b/src/thebigcrafter/APM/forms/RepoForm.php index 45a8954..b10accd 100644 --- a/src/thebigcrafter/APM/forms/RepoForm.php +++ b/src/thebigcrafter/APM/forms/RepoForm.php @@ -73,6 +73,10 @@ public static function getListingForm(): Form return $form; } + /** + * Get install plugin form + * @return Form + */ public static function getInstallForm(): Form { $form = new CustomForm(function (Player $player, array $data = null) { @@ -86,4 +90,23 @@ public static function getInstallForm(): Form $form->addInput("Please enter plugin name"); return $form; } + + /** + * Get removing plugin form + * @return Form + */ + + public static function getRemoveForm(): Form + { + $form = new CustomForm(function (Player $player, array $data = null) { + if (Remover::removePlugin($data[0])) { + $player->sendMessage(APM::$PREFIX . TextFormat::GREEN . "Removed!"); + } else { + $player->sendMessage(APM::$PREFIX . TextFormat::DARK_RED . $data[0] . " not found!"); + } + }); + $form->setTitle("Remove plugin"); + $form->addInput("Please enter plugin name"); + return $form; + } } diff --git a/src/thebigcrafter/APM/jobs/Remover.php b/src/thebigcrafter/APM/jobs/Remover.php index ad77460..71313db 100644 --- a/src/thebigcrafter/APM/jobs/Remover.php +++ b/src/thebigcrafter/APM/jobs/Remover.php @@ -24,4 +24,19 @@ public static function removeRepo(string $url): bool return false; } } + + /** + * Remove plugin. If removed, return true else if cannot find plugin return false + * + * @return bool + */ + public static function removePlugin(string $name): bool + { + if (file_exists(APM::getInstance()->getServer()->getDataPath() . "plugins/" . $name . ".phar")) { + unlink(APM::getInstance()->getServer()->getDataPath() . "plugins/" . $name . ".phar"); + return true; + } else { + return false; + } + } }