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

Commit

Permalink
Add list plugins command
Browse files Browse the repository at this point in the history
Signed-off-by: MintoD <[email protected]>
  • Loading branch information
MintoD committed Dec 13, 2021
1 parent 89cc189 commit ecd44db
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 143 deletions.
1 change: 1 addition & 0 deletions resources/lang/eng.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list.repo.button = "List repositories"
update.button = "Update"
install.plugin.button = "Install"
remove.plugin.button = "Remove"
list.plugin.button = "List plugins"

; Command /apm add-repo
add.repo.command.description = "Add a repository"
Expand Down
1 change: 1 addition & 0 deletions resources/lang/vie.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list.repo.button = "Danh sách kho lưu trữ"
update.button = "Cập nhật kho lưu trữ"
install.plugin.button = "Cài đặt plugin"
remove.plugin.button = "Xóa plugin"
list.plugin.button = "Danh sách plugin"

; Command /apm add-repo
add.repo.command.description = "Thêm một kho lưu trữ"
Expand Down
120 changes: 48 additions & 72 deletions src/thebigcrafter/APM/APM.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,82 @@
namespace thebigcrafter\APM;

use pocketmine\lang\Language;
use thebigcrafter\APM\commands\APMCommand;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Config;
use pocketmine\utils\SingletonTrait;
use thebigcrafter\APM\commands\APMCommand;
use thebigcrafter\APM\jobs\Cache;

class APM extends PluginBase
{
use SingletonTrait;

/**
* Language FILE name
*
* @var array<string>
*/
private array $languages = ["eng", "vie"];

/**
* Default repository
* @var string
*/
private string $defaultRepo = "https://thebigcrafter.github.io/";

/**
* Prefix
*
* @var string
*/
public static string $PREFIX = "§a[§bAPM§a]§r ";

/**
* Repositories list
*/
public Config $repos;

/**
* Plugin config
*
* @var Config
*/
public Config $config;

/**
* Plugins cache
*
* @var string[]
* @var array
*/
public static array $reposPluginsCache = [];

/**
* Repositories information cache
*
* @var string[]
*
* @var array
*/
public static array $reposInfoCache = [];

/**
* Loaded plugins list
*
* @var array
*/
public static array $loadedPlugins = [];

/**
* Language
*
* @var Language
*/
private static Language $language;

/**
* Get instance
* Repositories list
*
* @var Config
*/
public Config $repos;
/**
* Plugin configurations
*
* @var Config
*/
public Config $config;
/**
* Language FILE name
*
* @var array<string>
*/
private array $languages = ["eng", "vie"];
/**
* Default repository
*
* @var string
*/
private string $defaultRepo = "https://thebigcrafter.github.io/";

/**
* @return self
*/
public static function getInstance(): self
{
return self::$instance;
}

/**
* @return Language
*/
public static function getLanguage(): Language
{
return self::$language;
Expand All @@ -94,49 +90,16 @@ public function onEnable(): void
{
self::setInstance($this);

$this->initConfig();
$this->initLanguageFiles($this->config->get("language"), $this->languages);
$this->cacheRepo();
Cache::cacheLoadedPlugins();

$this->getServer()->getCommandMap()->register("apm", new APMCommand($this, "apm", "Advanced Plugin Manager"));

}

/**
* Initialize config
*
* @return void
*/
public function initConfig(): void
{
$this->saveDefaultConfig();
$this->config = $this->getConfig();
$this->repos = new Config($this->getDataFolder() . "repos.yml", Config::YAML, array("repositories" => [$this->defaultRepo]));
}

/**
* Cache repositories information
*
* @return void
*/
public function cacheRepo(): void
{
$this->reloadConfig();

Cache::cacheReposInfo($this->repos->get("repositories"));
$this->initLanguageFiles($this->config->get("language"), $this->languages);
$this->cacheRepo();
Cache::cacheLoadedPlugins();

$this->cachePlugin();
}
$this->getServer()->getCommandMap()->register("apm", new APMCommand($this, "apm", "Advanced Plugin Manager"));

/**
* Cache plugins from repositories
*
* @return void
*/
public function cachePlugin(): void
{
Cache::cacheReposPlugins($this->repos->get("repositories"));
}

/**
Expand All @@ -161,4 +124,17 @@ public function initLanguageFiles(string $lang, array $languageFiles): void

self::$language = new Language($lang, $this->getDataFolder() . "lang/");
}

/**
* Cache repositories data
*
* @return void
*/
public function cacheRepo(): void
{
$this->reloadConfig();

Cache::cacheReposInfo($this->repos->get("repositories"));
Cache::cacheReposPlugins($this->repos->get("repositories"));
}
}
29 changes: 15 additions & 14 deletions src/thebigcrafter/APM/commands/subcommands/AddRepoCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,21 @@

namespace thebigcrafter\APM\commands\subcommands;

use CortexPE\Commando\BaseSubCommand;
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use CortexPE\Commando\exception\ArgumentOrderException;
use thebigcrafter\APM\APM;
use thebigcrafter\APM\forms\RepoForm;
use thebigcrafter\APM\jobs\Adder;
use JsonException;
use pocketmine\command\CommandSender;
use pocketmine\player\Player;
use thebigcrafter\APM\APM;
use thebigcrafter\APM\error\ErrorHandler;
use thebigcrafter\APM\forms\RepoForm;
use thebigcrafter\APM\jobs\Adder;

class AddRepoCommand extends BaseSubCommand
{
/**
* @throws ArgumentOrderException
*/
protected function prepare(): void
{
$this->registerArgument(0, new RawStringArgument("url", false));
}

/**
* @throws \JsonException
* @throws JsonException
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args): void
{
Expand All @@ -35,11 +28,19 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args): vo
if (!isset($args["url"])) {
ErrorHandler::sendErrorToConsole(ErrorHandler::$INVALID_URL);
}
if (Adder::addRepo((string) $args["url"])) {
if (Adder::addRepo((string)$args["url"])) {
$sender->sendMessage(APM::$PREFIX . APM::getLanguage()->translateString("add.repo.success"));
} else {
ErrorHandler::sendErrorToConsole(ErrorHandler::$INVALID_URL);
}
}
}

/**
* @throws ArgumentOrderException
*/
protected function prepare(): void
{
$this->registerArgument(0, new RawStringArgument("url", false));
}
}
10 changes: 7 additions & 3 deletions src/thebigcrafter/APM/forms/MenuForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

use dktapps\pmforms\MenuForm as PmformsMenuForm;
use dktapps\pmforms\MenuOption;
use thebigcrafter\APM\APM;
use pocketmine\form\Form;
use pocketmine\player\Player;
use thebigcrafter\APM\APM;

class MenuForm
{
Expand All @@ -20,7 +19,7 @@ public static function getMenuForm(): PmformsMenuForm
{
$buttons = [];

foreach ([APM::getLanguage()->translateString("add.repo.button"), APM::getLanguage()->translateString("remove.repo.button"), APM::getLanguage()->translateString("list.repo.button"), APM::getLanguage()->translateString("update.button"), APM::getLanguage()->translateString("install.plugin.button"), APM::getLanguage()->translateString("remove.plugin.button")] as $name) {
foreach ([APM::getLanguage()->translateString("add.repo.button"), APM::getLanguage()->translateString("remove.repo.button"), APM::getLanguage()->translateString("list.repo.button"), APM::getLanguage()->translateString("update.button"), APM::getLanguage()->translateString("install.plugin.button"), APM::getLanguage()->translateString("remove.plugin.button"), APM::getLanguage()->translateString("list.plugin.button")] as $name) {
$buttons[] = new MenuOption($name);
}

Expand All @@ -47,6 +46,11 @@ public static function getMenuForm(): PmformsMenuForm
case 5:
$player->sendForm(RepoForm::getRemovePluginForm());
break;
case 6:
foreach (APM::$reposPluginsCache as $plugin) {
$player->sendMessage($plugin["name"]);
}
break;
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/thebigcrafter/APM/forms/RepoForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public static function getRemoveRepoForm(): CustomForm
public static function getRepoListForm(Player $player): void
{
$repositoriesList = "";
foreach (APM::$reposPluginsCache as $cache) {
foreach (APM::$reposInfoCache as $cache) {
foreach (APM::getInstance()->repos->get("repositories") as $repo) {
if ($cache["repo"] == $repo) {
$repositoriesList = $repositoriesList . "Name: " . $cache["label"] . "\n" . "Codename: " . $cache["codename"] . "\n" . "Repository URL: " . $repo . "\n\n";
$repositoriesList = $repositoriesList . "Name: " . $cache["label"] . "\n" . "Description: " . $cache["description"] . "\n" . "Repository URL: " . $repo . "\n\n";
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/thebigcrafter/APM/jobs/Adder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace thebigcrafter\APM\jobs;

use JsonException;
use thebigcrafter\APM\APM;
use thebigcrafter\APM\utils\Utils;

Expand All @@ -14,7 +15,7 @@ class Adder
*
* @return bool
*
* @throws \JsonException
* @throws JsonException
*/
public static function addRepo(string $url): bool
{
Expand Down
1 change: 1 addition & 0 deletions src/thebigcrafter/APM/jobs/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function cacheReposInfo(array $urls): void
$info = json_decode(Internet::getURL($url . "Release.json")->getBody(), true);

APM::$reposInfoCache[] = [
"repo" => $url,
"label" => $info["label"],
"suite" => $info["suite"],
"description" => $info["description"],
Expand Down
Loading

0 comments on commit ecd44db

Please sign in to comment.