From 9d3ce903490b15a9517bf96f154e702db685a83f Mon Sep 17 00:00:00 2001 From: toby7002 <144540995+toby7002@users.noreply.github.com> Date: Thu, 19 Oct 2023 21:40:32 +0700 Subject: [PATCH] Finish listing installed plugins command --- resources/lang/en_us.json | 2 + src/thebigcrafter/omp/OhMyPMMP.php | 1 + .../omp/cache/InstalledPluginCache.php | 34 ++++++++++++ .../omp/cache/InstalledPluginsPool.php | 54 ++++++++++++++++++ .../omp/commands/subcommands/ListCommand.php | 55 +++++++++++++++++-- .../omp/tasks/CacheInstalledPlugins.php | 51 +++++++++++++++++ 6 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 src/thebigcrafter/omp/cache/InstalledPluginCache.php create mode 100644 src/thebigcrafter/omp/cache/InstalledPluginsPool.php create mode 100644 src/thebigcrafter/omp/tasks/CacheInstalledPlugins.php diff --git a/resources/lang/en_us.json b/resources/lang/en_us.json index 3f3439b..87af3aa 100644 --- a/resources/lang/en_us.json +++ b/resources/lang/en_us.json @@ -4,6 +4,8 @@ "text.cache.failed": "§cA error has occurred: {%reason}", "text.cache.poggit.successfully": "§a{{count}} plugins from Poggit has been cached successfully", "text.cache.poggit.failed": "§cCannot cache plugins from Poggit repo: {{reason}}", + "text.cache.installed.successfully": "§a{{count}} installed plugins has been cached successfully", + "text.cache.installed.failed": "§cCannot cache plugins from Poggit repo: {{reason}}", "text.cache.installed.plugin": "§eGetting installed plugins", "text.cache.upgradable.plugin": "§eChecking upgradable plugins", diff --git a/src/thebigcrafter/omp/OhMyPMMP.php b/src/thebigcrafter/omp/OhMyPMMP.php index f3aa8c0..2fb51f2 100644 --- a/src/thebigcrafter/omp/OhMyPMMP.php +++ b/src/thebigcrafter/omp/OhMyPMMP.php @@ -72,6 +72,7 @@ public function cachePlugins() : void { Iodine::async(function() { CachePlugins::run()->await(); + CacheInstalledPlugins::run()->await(); })->await(); } } diff --git a/src/thebigcrafter/omp/cache/InstalledPluginCache.php b/src/thebigcrafter/omp/cache/InstalledPluginCache.php new file mode 100644 index 0000000..cd14da1 --- /dev/null +++ b/src/thebigcrafter/omp/cache/InstalledPluginCache.php @@ -0,0 +1,34 @@ + + * 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\cache; + +use function count; +use function sort; + +class InstalledPluginCache +{ + public function __construct(private string $name, private string $version, private string $description) + { + } + + public function getName() : string { + return $this->name; + } + + public function getVersion() : string { + return $this->version; + } + + public function getDescription(): string { + return $this->description; +} +} diff --git a/src/thebigcrafter/omp/cache/InstalledPluginsPool.php b/src/thebigcrafter/omp/cache/InstalledPluginsPool.php new file mode 100644 index 0000000..794fac2 --- /dev/null +++ b/src/thebigcrafter/omp/cache/InstalledPluginsPool.php @@ -0,0 +1,54 @@ + + * 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\cache; + +use Generator; +use SplObjectStorage; + +final class InstalledPluginsPool { + /** @var ?SplObjectStorage $storage */ + private static ?SplObjectStorage $storage = null; + + /** + * @return SplObjectStorage + */ + public static function getStorage() : SplObjectStorage{ + if(self::$storage === null){ + self::$storage = new SplObjectStorage(); /** @phpstan-ignore-line */ + } + + return self::$storage; /** @phpstan-ignore-line */ + } + + /** + * @param SplObjectStorage $storage + */ + public static function setStorage(SplObjectStorage $storage) : void{ + self::$storage = $storage; + } + + public static function add(InstalledPluginCache $pluginCache) : void { + self::getStorage()->attach($pluginCache); + } + + /** + * @param InstalledPluginCache[] $array + */ + public static function addMultiple(array $array) : void { + foreach ($array as $plugin) { + if ($plugin instanceof InstalledPluginCache) { + self::getStorage()->attach($plugin); + } + } + } + +} diff --git a/src/thebigcrafter/omp/commands/subcommands/ListCommand.php b/src/thebigcrafter/omp/commands/subcommands/ListCommand.php index 057bc28..a990d14 100644 --- a/src/thebigcrafter/omp/commands/subcommands/ListCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/ListCommand.php @@ -16,6 +16,8 @@ use CortexPE\Commando\BaseSubCommand; use pocketmine\command\CommandSender; use pocketmine\Server; +use thebigcrafter\omp\cache\InstalledPluginCache; +use thebigcrafter\omp\cache\InstalledPluginsPool; use thebigcrafter\omp\cache\PluginCache; use thebigcrafter\omp\cache\PluginsPool; use thebigcrafter\omp\OhMyPMMP; @@ -35,10 +37,23 @@ protected function prepare() : void { */ public function onRun(CommandSender $sender,string $aliasUsed,array $args) : void { $page = isset($args["page"]) ? $args["page"] : 1; - $type = isset($args["type"]) ? $args["type"] : 1; // 1 for available, 2 for installed, 3 for upgradable + $type = isset($args["type"]) ? $args["type"] : 1; // 1 for available, 2 for installed and 3 for upgradable plugins switch ($type) { - case "1": + case "installed": + case "--installed": + case "-i": + $type = 2; + break; + case "upgradable": + case "--upgradable": + case "-u": + $type = 3; + break; + } + + switch ($type) { + case 1: $maxPage = ceil(iterator_count(PluginsPool::getNamePlugins()) / self::PLUGINS_PER_PAGE); $currentPage = max(1, $page); $startIndex = ($currentPage - 1) * self::PLUGINS_PER_PAGE; @@ -55,17 +70,36 @@ public function onRun(CommandSender $sender,string $aliasUsed,array $args) : voi $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.list.title", ["currentPage" => $currentPage, "maxPage" => $maxPage])); foreach ($currentPlugins as $plugin) { - $this->renderBlock($sender, $plugin); + $this->renderPluginsBlock($sender, $plugin); } break; - case "2": + case 2: + var_dump(count(InstalledPluginsPool::getStorage())); + $maxPage = ceil(count(InstalledPluginsPool::getStorage()) / self::PLUGINS_PER_PAGE); + $currentPage = max(1, $page); + $startIndex = ($currentPage - 1) * self::PLUGINS_PER_PAGE; + $endIndex = $startIndex + self::PLUGINS_PER_PAGE; + + $currentPlugins = []; + $currentIndex = 0; + foreach (InstalledPluginsPool::getStorage() as $plugin) { + if ($currentIndex >= $startIndex && $currentIndex < $endIndex) { + $currentPlugins[] = $plugin; + } + $currentIndex++; + } + + $sender->sendMessage(OhMyPMMP::getLanguage()->translate("command.list.title", ["currentPage" => $currentPage, "maxPage" => $maxPage])); + foreach ($currentPlugins as $plugin) { + $this->renderInstalledPluginsBlock($sender, $plugin); + } break; - case "3": + case 3: break; } } - private function renderBlock(CommandSender $sender, PluginCache $plugin): void + private function renderPluginsBlock(CommandSender $sender, PluginCache $plugin): void { $sender->sendMessage("================" . PHP_EOL); $sender->sendMessage("# " . OhMyPMMP::getLanguage()->translate("command.list.plugin.name", ["name" => $plugin->getName()])); @@ -77,4 +111,13 @@ private function renderBlock(CommandSender $sender, PluginCache $plugin): void $sender->sendMessage("# " . OhMyPMMP::getLanguage()->translate("command.list.plugin.license", ["license" => $plugin->getLicense()])); $sender->sendMessage("================" . PHP_EOL . PHP_EOL); } + + private function renderInstalledPluginsBlock(CommandSender $sender, InstalledPluginCache $plugin): void + { + $sender->sendMessage("================" . PHP_EOL); + $sender->sendMessage("# " . OhMyPMMP::getLanguage()->translate("command.list.plugin.name", ["name" => $plugin->getName()])); + $sender->sendMessage("# " . OhMyPMMP::getLanguage()->translate("command.list.plugin.versions", ["versions" => $plugin->getVersion()])); + $sender->sendMessage("# " . OhMyPMMP::getLanguage()->translate("command.list.plugin.description", ["description" => $plugin->getDescription()])); + $sender->sendMessage("================" . PHP_EOL . PHP_EOL); + } } diff --git a/src/thebigcrafter/omp/tasks/CacheInstalledPlugins.php b/src/thebigcrafter/omp/tasks/CacheInstalledPlugins.php new file mode 100644 index 0000000..365f76b --- /dev/null +++ b/src/thebigcrafter/omp/tasks/CacheInstalledPlugins.php @@ -0,0 +1,51 @@ + + * 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 pocketmine\utils\InternetException; +use thebigcrafter\Iodine\Future; +use thebigcrafter\Iodine\Iodine; +use thebigcrafter\omp\cache\InstalledPluginCache; +use thebigcrafter\omp\cache\InstalledPluginsPool; +use thebigcrafter\omp\cache\PluginCache; +use thebigcrafter\omp\cache\PluginsPool; +use thebigcrafter\omp\cache\PluginVersion; +use thebigcrafter\omp\OhMyPMMP; +use thebigcrafter\omp\utils\Internet; +use thebigcrafter\omp\Vars; +use function array_merge; +use function count; +use function json_decode; +use function sort; + +class CacheInstalledPlugins +{ + public static function run() : Future { + return Iodine::async(function() { + $plugins = OhMyPMMP::getInstance()->getServer()->getPluginManager()->getPlugins(); + + sort($plugins); + $pluginCaches = []; + + foreach ($plugins as $plugin) { + $name = $plugin->getName(); + $version = $plugin->getDescription()->getVersion(); + $description = $plugin->getDescription()->getDescription(); + // TODO: Deps + $pluginCaches[] = new InstalledPluginCache($name, $version, $description); + } + + InstalledPluginsPool::addMultiple($pluginCaches); + OhMyPMMP::getInstance()->getLogger()->info(OhMyPMMP::getLanguage()->translate("text.cache.installed.successfully", ["count" => count($pluginCaches)])); + }); + } +}