Skip to content

Commit

Permalink
Add list command
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Nov 21, 2023
1 parent f550898 commit 95f2405
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 85 deletions.
9 changes: 8 additions & 1 deletion resources/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"messages.pool.fetching": "Fetching data from Poggit...",
"messages.pool.fetched": "Fetched {{amount}} plugins from Poggit successfully",

"commands.version.message_1": "OMP v{{version}}",
"commands.version.message_2": "PHP v{{phpVersion}} {{arch}}",

"commands.remove.successfully": "{{name}} was removed successfully, please restart the server to apply changes",
"commands.remove.failed": "{{name}} not found"
"commands.remove.failed": "{{name}} not found",

"commands.list.form.name": "Name: {{name}}",
"commands.list.form.license": "License: {{license}}",
"commands.list.form.versions": "Versions: {{versions}}"
}
6 changes: 5 additions & 1 deletion src/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use thebigcrafter\omp\types\Plugin;
use thebigcrafter\omp\types\PluginVersion;
use function array_map;
use function count;
use function json_decode;
use function strval;

Expand All @@ -35,19 +36,20 @@ class OhMyPMMP extends PluginBase
public function onLoad() : void
{
self::setInstance($this);
Language::loadLanguages();
}

public function onEnable() : void
{
$this->fetchData();
$this->saveDefaultConfig();
Language::loadLanguages();

$this->getServer()->getCommandMap()->register("OhMyPMMP", new OMPCommand($this, "ohmypmmp", "Oh My PMMP", ["omp", "oh-my-pmmp"]));
}

private function fetchData() : void
{
$this->getLogger()->info(Language::translate("messages.pool.fetching", []));
$client = HttpClientBuilder::buildDefault();

$res = $client->request(new Request(Vars::POGGIT_REPO_URL));
Expand Down Expand Up @@ -98,5 +100,7 @@ private function fetchData() : void
);
}
}

$this->getLogger()->info(Language::translate("messages.pool.fetched", ["amount" => count(PoggitPluginsPool::getPool())]));
}
}
44 changes: 22 additions & 22 deletions src/commands/OMPCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@

class OMPCommand extends BaseCommand
{
protected function prepare(): void
{
$this->setPermission("oh-my-pmmp.cmds");

$subcommands = [
new VersionCommand($this->getOwningPlugin(), "version", "Print oh-my-pmmp and PHP version", ["v", "-v", "--version"]),
new RemoveCommand($this->getOwningPlugin(), "remove", "Remove a plugin", ["r", "-r", "--remove"]),
new ListCommand($this->getOwningPlugin(), "list", "List available plugins", ["l", "-l", "--list"])
];

foreach ($subcommands as $subcommand) {
$this->registerSubcommand($subcommand);
}
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args): void
{
$this->sendUsage();
}
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.cmds");

$subcommands = [
new VersionCommand($this->getOwningPlugin(), "version", "Print oh-my-pmmp and PHP version", ["v", "-v", "--version"]),
new RemoveCommand($this->getOwningPlugin(), "remove", "Remove a plugin", ["r", "-r", "--remove"]),
new ListCommand($this->getOwningPlugin(), "list", "List available plugins", ["l", "-l", "--list"])
];

foreach ($subcommands as $subcommand) {
$this->registerSubcommand($subcommand);
}
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$this->sendUsage();
}
}
52 changes: 37 additions & 15 deletions src/commands/subcommands/ListCommand.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,58 @@
<?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\IntegerArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\pool\PoggitPluginsPool;
use function array_slice;
use function ceil;
use function count;
use function implode;
use function max;
use function min;

class ListCommand extends BaseSubCommand {
protected function prepare() : void
class ListCommand extends BaseSubCommand
{
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.list");

$this->registerArgument(0, new IntegerArgument("page", true));
$this->registerArgument(0, new IntegerArgument("page", true));
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$page = isset($args["page"]) ? $args["page"] : 0;
$pluginsPerPage = OhMyPMMP::getInstance()->getConfig()->get("pluginsPerPage");
$totalPages = ceil(count(PoggitPluginsPool::getPool()) / $pluginsPerPage);
$page = max(0, min($page, $totalPages - 1));
$offset = $page * $pluginsPerPage;
/** @var array{name: string, plugin: Plugin} $pluginsOnPage */
$pluginsOnPage = array_slice(PoggitPluginsPool::getPool(), $offset, $pluginsPerPage);

foreach ($pluginsOnPage as $name => $info) {
// TODO: make a beautiful table :D
}
$page = isset($args["page"]) ? $args["page"] : 0;
$pluginsPerPage = OhMyPMMP::getInstance()->getConfig()->get("pluginsPerPage");
$totalPages = ceil(count(PoggitPluginsPool::getPool()) / $pluginsPerPage);
$page = max(0, min($page, $totalPages - 1));
$offset = $page * $pluginsPerPage;
/** @var array{name: string, plugin: Plugin} $pluginsOnPage */
$pluginsOnPage = array_slice(PoggitPluginsPool::getPool(), $offset, $pluginsPerPage);

foreach ($pluginsOnPage as $name => $plugin) {
$sender->sendMessage(Language::translate("commands.list.form.name", ["name" => $name]));
$sender->sendMessage(Language::translate("commands.list.form.license", ["license" => $plugin->getLicense()]));
$sender->sendMessage(Language::translate("commands.list.form.versions", ["versions" => implode(", ", $plugin->getVersionsOnly())]));
$sender->sendMessage("====================");
}
}
}
}
103 changes: 57 additions & 46 deletions src/commands/subcommands/RemoveCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?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\BooleanArgument;
Expand All @@ -10,61 +21,61 @@
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use function Amp\File\deleteDirectory;
use function Amp\File\exists;
use function Amp\File\deleteFile;
use function Amp\File\exists;

class RemoveCommand extends BaseSubCommand
{
protected function prepare(): void
{
$this->setPermission("oh-my-pmmp.remove");
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.remove");

$this->registerArgument(0, new RawStringArgument("name", false));
$this->registerArgument(1, new BooleanArgument("wipeData", true));
}
$this->registerArgument(0, new RawStringArgument("name", false));
$this->registerArgument(1, new BooleanArgument("wipeData", true));
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args): void
{
$name = $args["name"];
$wipeData = isset($args["wipeData"]) ? $args["wipeData"] : false;
/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$name = $args["name"];
$wipeData = isset($args["wipeData"]) ? $args["wipeData"] : false;

$exec = $this->removePlugin($name, $wipeData);
$exec = $this->removePlugin($name, $wipeData);

if (!$exec) {
$sender->sendMessage(Language::translate("commands.remove.failed", ["name" => $name]));
return;
}
$sender->sendMessage(Language::translate("commands.remove.successfully", ["name" => $name]));
return;
}
if (!$exec) {
$sender->sendMessage(Language::translate("commands.remove.failed", ["name" => $name]));
return;
}
$sender->sendMessage(Language::translate("commands.remove.successfully", ["name" => $name]));
return;
}

/**
* Return false if plugin not found
*/
private function removePlugin(string $name, bool $wipeData): bool
{
$pluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar");
$pluginFolderPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", $name);
/**
* Return false if plugin not found
*/
private function removePlugin(string $name, bool $wipeData) : bool
{
$pluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar");
$pluginFolderPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", $name);

if (exists($pluginFilePath)) {
deleteFile($pluginFilePath);
} elseif (exists($pluginFolderPath)) {
deleteDirectory($pluginFolderPath);
} else {
return false;
}
if ($wipeData) {
$this->wipeData($name);
}
return true;
}
if (exists($pluginFilePath)) {
deleteFile($pluginFilePath);
} elseif (exists($pluginFolderPath)) {
deleteDirectory($pluginFolderPath);
} else {
return false;
}
if ($wipeData) {
$this->wipeData($name);
}
return true;
}

private function wipeData(string $name): void
{
$pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name);
deleteDirectory($pluginDataFolder);
}
private function wipeData(string $name) : void
{
$pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name);
deleteDirectory($pluginDataFolder);
}
}
5 changes: 5 additions & 0 deletions src/types/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace thebigcrafter\omp\types;

use function array_keys;
use function is_null;
use function version_compare;

Expand All @@ -38,6 +39,10 @@ public function getVersions() : array
return $this->versions;
}

public function getVersionsOnly() : array {
return array_keys($this->getVersions());
}

/**
* Return the latest version if no specific version is provided.
*/
Expand Down

0 comments on commit 95f2405

Please sign in to comment.