Skip to content

Commit

Permalink
Finish listing installed plugins command
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Oct 19, 2023
1 parent e84c0f6 commit 9d3ce90
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 6 deletions.
2 changes: 2 additions & 0 deletions resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
1 change: 1 addition & 0 deletions src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function cachePlugins() : void {

Iodine::async(function() {
CachePlugins::run()->await();
CacheInstalledPlugins::run()->await();
})->await();
}
}
34 changes: 34 additions & 0 deletions src/thebigcrafter/omp/cache/InstalledPluginCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\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;
}
}
54 changes: 54 additions & 0 deletions src/thebigcrafter/omp/cache/InstalledPluginsPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\cache;

use Generator;
use SplObjectStorage;

final class InstalledPluginsPool {
/** @var ?SplObjectStorage<InstalledPluginCache, mixed> $storage */
private static ?SplObjectStorage $storage = null;

/**
* @return SplObjectStorage<InstalledPluginCache, mixed>
*/
public static function getStorage() : SplObjectStorage{
if(self::$storage === null){
self::$storage = new SplObjectStorage(); /** @phpstan-ignore-line */
}

return self::$storage; /** @phpstan-ignore-line */
}

/**
* @param SplObjectStorage<InstalledPluginCache, mixed> $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);
}
}
}

}
55 changes: 49 additions & 6 deletions src/thebigcrafter/omp/commands/subcommands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Check failure on line 59 in src/thebigcrafter/omp/commands/subcommands/ListCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan analysis (ubuntu-latest, 8.1)

Binary operation "-" between 1|string and 1 results in an error.
Expand All @@ -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()]));
Expand All @@ -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);
}
}
51 changes: 51 additions & 0 deletions src/thebigcrafter/omp/tasks/CacheInstalledPlugins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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 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)]));
});
}
}

0 comments on commit 9d3ce90

Please sign in to comment.