Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Add remove plugin command
Browse files Browse the repository at this point in the history
  • Loading branch information
MintoD committed Nov 9, 2021
1 parent 0f3536a commit d615397
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/thebigcrafter/APM/commands/APMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
35 changes: 35 additions & 0 deletions src/thebigcrafter/APM/commands/subcommands/RemovePluginCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\APM\commands\subcommands;

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use thebigcrafter\APM\forms\RepoForm;
use thebigcrafter\APM\jobs\Remover;

class RemovePluginCommand extends BaseSubCommand
{
protected function prepare(): void
{
$this->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!");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
23 changes: 23 additions & 0 deletions src/thebigcrafter/APM/forms/RepoForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
15 changes: 15 additions & 0 deletions src/thebigcrafter/APM/jobs/Remover.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit d615397

Please sign in to comment.