From ea5d2847b82fac66edc4163d1842ff859e0b98ab Mon Sep 17 00:00:00 2001 From: toby7002 <144540995+toby7002@users.noreply.github.com> Date: Fri, 20 Oct 2023 10:05:54 +0700 Subject: [PATCH] Add purge command --- plugin.yml | 3 ++ resources/lang/en_us.json | 6 ++- src/thebigcrafter/omp/commands/OMPCommand.php | 4 +- .../omp/commands/subcommands/PurgeCommand.php | 48 +++++++++++++++++++ .../commands/subcommands/RemoveCommand.php | 22 +++------ src/thebigcrafter/omp/tasks/RemovePlugin.php | 46 ++++++++++++++++++ 6 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 src/thebigcrafter/omp/commands/subcommands/PurgeCommand.php create mode 100644 src/thebigcrafter/omp/tasks/RemovePlugin.php diff --git a/plugin.yml b/plugin.yml index ad28ff8..d23015c 100644 --- a/plugin.yml +++ b/plugin.yml @@ -34,3 +34,6 @@ permissions: oh-my-pmmp.upgrade: description: Allow players to upgrade plugin default: op + oh-my-pmmp.purge: + description: Allow players to remove plugin and it data + default: op diff --git a/resources/lang/en_us.json b/resources/lang/en_us.json index 45dda6b..0da640c 100644 --- a/resources/lang/en_us.json +++ b/resources/lang/en_us.json @@ -29,5 +29,9 @@ "command.remove.description": "Remove the plugin", "command.remove.failed": "§cPlugin cannot be removed: {{reason}}", - "command.remove.successfully": "§a{{name}} removed successfully" + "command.remove.successfully": "§a{{name}} removed successfully", + + "command.purge.description": "Remove the plugin and it data", + "command.purge.failed": "§cPlugin cannot be removed: {{reason}}", + "command.purge.successfully": "§a{{name}} and it data removed successfully" } diff --git a/src/thebigcrafter/omp/commands/OMPCommand.php b/src/thebigcrafter/omp/commands/OMPCommand.php index feceb5b..ef40c62 100644 --- a/src/thebigcrafter/omp/commands/OMPCommand.php +++ b/src/thebigcrafter/omp/commands/OMPCommand.php @@ -15,6 +15,7 @@ use pocketmine\command\CommandSender; use thebigcrafter\omp\commands\subcommands\HelpCommand; use thebigcrafter\omp\commands\subcommands\ListCommand; +use thebigcrafter\omp\commands\subcommands\PurgeCommand; use thebigcrafter\omp\commands\subcommands\RemoveCommand; use thebigcrafter\omp\commands\subcommands\VersionCommand; use thebigcrafter\omp\OhMyPMMP; @@ -29,7 +30,8 @@ protected function prepare() : void { new VersionCommand(OhMyPMMP::getInstance(),"version", OhMyPMMP::getLanguage()->translate("command.version.description"), ["v", "-v", "--version"]), new HelpCommand(OhMyPMMP::getInstance(),"help", OhMyPMMP::getLanguage()->translate("command.help.description"), ["h", "-h", "--help"]), new ListCommand(OhMyPMMP::getInstance(), "list", OhMyPMMP::getLanguage()->translate("command.list.description"), ["l", "-l", "--list"]), - new RemoveCommand(OhMyPMMP::getInstance(), "remove", OhMyPMMP::getLanguage()->translate("command.remove.description"), ["r", "-r", "--remove"]) + new RemoveCommand(OhMyPMMP::getInstance(), "remove", OhMyPMMP::getLanguage()->translate("command.remove.description"), ["r", "-r", "--remove"]), + new PurgeCommand(OhMyPMMP::getInstance(), "purge", OhMyPMMP::getLanguage()->translate("command.purge.description"), ["p", "-p", "--purge"]) ]; foreach ($subcommands as $subcommand) { diff --git a/src/thebigcrafter/omp/commands/subcommands/PurgeCommand.php b/src/thebigcrafter/omp/commands/subcommands/PurgeCommand.php new file mode 100644 index 0000000..9d989c3 --- /dev/null +++ b/src/thebigcrafter/omp/commands/subcommands/PurgeCommand.php @@ -0,0 +1,48 @@ + + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\commands\subcommands; + +use CortexPE\Commando\args\RawStringArgument; +use CortexPE\Commando\BaseSubCommand; +use pocketmine\command\CommandSender; +use thebigcrafter\Fluorine\Fluorine; +use thebigcrafter\Iodine\Iodine; +use thebigcrafter\omp\OhMyPMMP; +use thebigcrafter\omp\tasks\RemovePlugin; + +class PurgeCommand extends BaseSubCommand +{ + protected function prepare() : void + { + $this->setPermission("oh-my-pmmp.purge"); + + $this->registerArgument(0, new RawStringArgument("name", false)); + } + + public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void + { + $pluginName = $args["name"]; + + Iodine::async(function() use ($pluginName, $sender) { + try { + RemovePlugin::run($pluginName, true)->await(); + + $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.purge.successfully", ["name" => $pluginName])); + return; + } catch (\Exception $e) { + $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.purge.failed", ["reason" => $e->getMessage()])); + } + })->await(); + + Fluorine::run(); + } +} diff --git a/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php b/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php index 3affe91..cf425f2 100644 --- a/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php @@ -14,12 +14,10 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; use pocketmine\command\CommandSender; -use Symfony\Component\Filesystem\Exception\IOException; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Filesystem\Path; use thebigcrafter\Fluorine\Fluorine; use thebigcrafter\Iodine\Iodine; use thebigcrafter\omp\OhMyPMMP; +use thebigcrafter\omp\tasks\RemovePlugin; class RemoveCommand extends BaseSubCommand { @@ -33,20 +31,14 @@ protected function prepare() : void public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { $pluginName = $args["name"]; - $fs = new Filesystem(); - $pluginPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins/$pluginName.phar"); - - if(!$fs->exists($pluginPath)) { - $sender->sendMessage(OhMyPMMP::getLanguage()->translate("text.plugin.not.found", ["name" => $pluginName])); - return; - } - - Iodine::async(function() use ($pluginName, $sender, $pluginPath, $fs) { + Iodine::async(function() use ($pluginName, $sender) { try { - $fs->remove($pluginPath); - $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.remove.successfully", ["name" => $pluginName])); - } catch (IOException $e) { + RemovePlugin::run($pluginName)->await(); + + $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.remove.successfully", ["name" => $pluginName])); + return; + } catch (\Exception $e) { $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.remove.failed", ["reason" => $e->getMessage()])); } })->await(); diff --git a/src/thebigcrafter/omp/tasks/RemovePlugin.php b/src/thebigcrafter/omp/tasks/RemovePlugin.php new file mode 100644 index 0000000..fc47b3f --- /dev/null +++ b/src/thebigcrafter/omp/tasks/RemovePlugin.php @@ -0,0 +1,46 @@ + + * This source file is subject to the GPL-3.0 license that is bundled + * with this source code in the file LICENSE. + */ + +declare(strict_types=1); + +namespace thebigcrafter\omp\tasks; + +use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Filesystem\Path; +use thebigcrafter\Iodine\DeferredFuture; +use thebigcrafter\Iodine\Future; +use thebigcrafter\omp\OhMyPMMP; + +class RemovePlugin +{ + public static function run(string $pluginName, bool $wipeData = false) : Future { + $deferred = new DeferredFuture(); + $fs = new Filesystem(); + $pluginPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins/$pluginName.phar"); + $pluginDataPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugin_data/$pluginName"); + + if(!$fs->exists($pluginPath)) { + $deferred->error(new \Exception(OhMyPMMP::getLanguage()->translate("text.plugin.not.found", ["name" => $pluginName]))); + } else { + try { + if($wipeData) { + $fs->remove([$pluginPath, $pluginDataPath]); + } else { + $fs->remove($pluginPath); + } + $deferred->complete(true); + } catch (IOException $e) { + $deferred->error($e); + } + } + + return $deferred->getFuture(); + } +}