Skip to content

Commit

Permalink
Add purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Oct 20, 2023
1 parent 9d1f295 commit ea5d284
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 17 deletions.
3 changes: 3 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 3 additions & 1 deletion src/thebigcrafter/omp/commands/OMPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
48 changes: 48 additions & 0 deletions src/thebigcrafter/omp/commands/subcommands/PurgeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* 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();
}
}
22 changes: 7 additions & 15 deletions src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
46 changes: 46 additions & 0 deletions src/thebigcrafter/omp/tasks/RemovePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of oh-my-pmmp.
* (c) thebigcrafter <[email protected]>
* 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();
}
}

0 comments on commit ea5d284

Please sign in to comment.