diff --git a/src/thebigcrafter/omp/OhMyPMMP.php b/src/thebigcrafter/omp/OhMyPMMP.php index dd6f320..a7a0efa 100644 --- a/src/thebigcrafter/omp/OhMyPMMP.php +++ b/src/thebigcrafter/omp/OhMyPMMP.php @@ -27,7 +27,9 @@ class OhMyPMMP extends PluginBase public function onLoad() : void { self::setInstance($this); - Language::loadLanguages($this->getConfig()->get("language")); + /** @var string $selectedLanguage */ + $selectedLanguage = $this->getConfig()->get("language"); + Language::loadLanguages($selectedLanguage); $this->createFolders(); } diff --git a/src/thebigcrafter/omp/Utils.php b/src/thebigcrafter/omp/Utils.php deleted file mode 100644 index 031ce14..0000000 --- a/src/thebigcrafter/omp/Utils.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * 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; - -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Filesystem\Path; -use function explode; - -class Utils -{ - public static function isMajorVersionInRange(string $checkVersion, string $minVersion, string $maxVersion) : bool - { - $checkMajor = (int) explode('.', $checkVersion)[0]; - $minMajor = (int) explode('.', $minVersion)[0]; - $maxMajor = (int) explode('.', $maxVersion)[0]; - - return $checkMajor >= $minMajor && $checkMajor <= $maxMajor; - } - - public static function getPluginsFolder() : string - { - return Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins"); - } - - /** - * Check plugin by looking for it PHAR file - */ - public static function doesPluginExist(string $name) : bool - { - $fs = new Filesystem(); - return $fs->exists(self::getPluginFilePath($name)); - } - - /** - * Get it PHAR file path - */ - public static function getPluginFilePath(string $name) : string - { - return Path::join(self::getPluginsFolder(), "$name.phar"); - } -} diff --git a/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php b/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php index 9b2018d..0c17713 100644 --- a/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/DisableCommand.php @@ -15,6 +15,7 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; @@ -22,12 +23,15 @@ use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; -use thebigcrafter\omp\Utils; use thebigcrafter\omp\utils\Filesystem; +use thebigcrafter\omp\utils\Utils; class DisableCommand extends BaseSubCommand { - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { $this->setPermission("oh-my-pmmp.disable"); @@ -40,10 +44,10 @@ protected function prepare() : void public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { $name = $args["name"]; - $oldPluginFilePath = Utils::getPluginFilePath($name); - $newPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar"); + $oldPluginFilePath = Utils::generatePluginFilePathWithName($name); + $newPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "disabled_plugins", "$name.phar"); - if (!Utils::doesPluginExist($name)) { + if (!Filesystem::exists($oldPluginFilePath)) { $sender->sendMessage(Language::translate("commands.disable.failed", ["name" => $name])); return; } diff --git a/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php b/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php index f7be9a7..82f815a 100644 --- a/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/EnableCommand.php @@ -15,6 +15,7 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; @@ -22,12 +23,15 @@ use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; -use thebigcrafter\omp\Utils; use thebigcrafter\omp\utils\Filesystem; +use thebigcrafter\omp\utils\Utils; class EnableCommand extends BaseSubCommand { - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { $this->setPermission("oh-my-pmmp.enable"); @@ -40,7 +44,7 @@ protected function prepare() : void public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { $name = $args["name"]; - $newPluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); + $newPluginFilePath = Utils::generatePluginFilePathWithName($name); $oldPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar"); if (!Filesystem::exists($oldPluginFilePath)) { diff --git a/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php b/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php index bb94c47..cc6a786 100644 --- a/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php @@ -15,19 +15,22 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\helpers\PharHelper; use thebigcrafter\omp\Language; -use thebigcrafter\omp\Utils; +use thebigcrafter\omp\utils\Utils; use Throwable; class ExtractCommand extends BaseSubCommand { - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { $this->setPermission("oh-my-pmmp.extract"); @@ -41,7 +44,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v { $fs = new Filesystem(); $name = $args["name"]; - $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); + $pluginFilePath = Utils::generatePluginFilePathWithName($name); if (!$fs->exists($pluginFilePath)) { $sender->sendMessage(Language::translate("commands.extract.failed", ["name" => $name])); @@ -50,7 +53,7 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v Await::f2c(function () use ($pluginFilePath, $name, $sender) : Generator { try { - yield from PharHelper::extract($pluginFilePath, Path::join(Utils::getPluginsFolder(), $name)); + yield from PharHelper::extract($pluginFilePath, Utils::generatePluginFolderPathWithName($name)); $sender->sendMessage(Language::translate("commands.extract.successfully", ["name" => $name])); } catch (Throwable $e) { $sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $e->getMessage()])); diff --git a/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php b/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php index 731e6c1..4471e1b 100644 --- a/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/InstallCommand.php @@ -15,12 +15,14 @@ use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; use Exception; use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\helpers\PharHelper; +use thebigcrafter\omp\helpers\PoggitHelper; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; use thebigcrafter\omp\pool\PoggitPluginsPool; @@ -39,10 +41,10 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $plugin = PoggitPluginsPool::getItem($name); - if (!isset($plugin)) { - $sender->sendMessage(Language::translate("commands.install.failed_1", ["name" => $name])); - return; - } + if(!PoggitHelper::pluginExist($name)) { + $sender->sendMessage(Language::translate("commands.install.failed_1", ["name" => $name])); + return; + } $pluginVersion = $plugin->getVersion($version); @@ -69,7 +71,10 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v }); } - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { $this->setPermission("oh-my-pmmp.install"); diff --git a/src/thebigcrafter/omp/commands/subcommands/ListCommand.php b/src/thebigcrafter/omp/commands/subcommands/ListCommand.php index 8e446fd..6440c58 100644 --- a/src/thebigcrafter/omp/commands/subcommands/ListCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/ListCommand.php @@ -1,20 +1,21 @@ - * - * 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; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * 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 CortexPE\Commando\exception\ArgumentOrderException; use pocketmine\command\CommandSender; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; @@ -25,19 +26,22 @@ use function count; use function implode; use function max; -use function min; - +use function min; + class ListCommand extends BaseSubCommand { - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { - $this->setPermission("oh-my-pmmp.list"); - + $this->setPermission("oh-my-pmmp.list"); + $this->registerArgument(0, new IntegerArgument("page", true)); - } - - /** - * @param array $args + } + + /** + * @param array $args */ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { @@ -48,8 +52,8 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $page = max(0, min($page, $totalPages - 1)); $offset = $page * $pluginsPerPage; /** @var array{name: string, plugin: Plugin} $pluginsOnPage */ - $pluginsOnPage = array_slice(PoggitPluginsPool::getPool(), $offset, $pluginsPerPage); - + $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()])); @@ -57,4 +61,4 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $sender->sendMessage("===================="); } } -} +} diff --git a/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php b/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php index a23fc33..7e8a1e2 100644 --- a/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php @@ -16,6 +16,7 @@ use CortexPE\Commando\args\BooleanArgument; use CortexPE\Commando\args\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; use Generator; use pocketmine\command\CommandSender; use SOFe\AwaitGenerator\Await; @@ -24,7 +25,10 @@ class RemoveCommand extends BaseSubCommand { - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { $this->setPermission("oh-my-pmmp.remove"); diff --git a/src/thebigcrafter/omp/commands/subcommands/ShowCommand.php b/src/thebigcrafter/omp/commands/subcommands/ShowCommand.php index 65af0b1..fb61b7c 100644 --- a/src/thebigcrafter/omp/commands/subcommands/ShowCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/ShowCommand.php @@ -1,63 +1,67 @@ - * - * 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; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * 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\RawStringArgument; use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; use pocketmine\command\CommandSender; use thebigcrafter\omp\Language; use thebigcrafter\omp\pool\PoggitPluginsPool; use function implode; -use function is_null; - +use function is_null; + class ShowCommand extends BaseSubCommand { - protected function prepare() : void + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void { - $this->setPermission("oh-my-pmmp.show"); - + $this->setPermission("oh-my-pmmp.show"); + $this->registerArgument(0, new RawStringArgument("name", false)); $this->registerArgument(1, new RawStringArgument("version", true)); - } - - /** - * @param array $args + } + + /** + * @param array $args */ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void { $name = $args["name"]; - $version = isset($args["version"]) ? $args["version"] : null; - - $plugin = PoggitPluginsPool::getItem($name); - + $version = $args["version"] ?? null; + + $plugin = PoggitPluginsPool::getItem($name); + if (!isset($plugin)) { $sender->sendMessage(Language::translate("commands.show.failed_1", ["name" => $name])); return; - } - - $pluginVersion = $plugin->getVersion($version); - + } + + $pluginVersion = $plugin->getVersion($version); + if (is_null($pluginVersion["plugin"])) { $sender->sendMessage(Language::translate("commands.show.failed_2", ["version" => $version])); return; - } - - $info = $pluginVersion["plugin"]; - + } + + $info = $pluginVersion["plugin"]; + // aka $version if user provides a specified version - $latestVersion = $pluginVersion["version"]; - + $latestVersion = $pluginVersion["version"]; + $sender->sendMessage(Language::translate("commands.show.form.name", ["name" => $name])); $sender->sendMessage(Language::translate("commands.show.form.version", ["version" => $latestVersion])); $sender->sendMessage(Language::translate("commands.show.form.versions", ["versions" => implode(", ", $plugin->getVersionsOnly())])); @@ -69,16 +73,16 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v $sender->sendMessage(Language::translate("commands.show.form.description_url", ["description_url" => $info->getDescriptionUrl()])); $sender->sendMessage(Language::translate("commands.show.form.changelog_url", ["changelog_url" => $info->getChangelogUrl()])); $sender->sendMessage(Language::translate("commands.show.form.api", ["from" => $info->getSupportedAPI()->getMinimumSupportedVersion(), "to" => $info->getSupportedAPI()->getMaximumSupportedVersion()])); - $sender->sendMessage(Language::translate("commands.show.form.deps", [])); - + $sender->sendMessage(Language::translate("commands.show.form.deps", [])); + foreach($info->getDependencies() as $dep) { if($dep->isHard()) { $sender->sendMessage(Language::translate("commands.show.form.dep_2", ["name" => $dep->getName(), "version" => $dep->getVersion()])); continue; } $sender->sendMessage(Language::translate("commands.show.form.dep_1", ["name" => $dep->getName(), "version" => $dep->getVersion()])); - } - + } + $sender->sendMessage("===================="); } -} +} diff --git a/src/thebigcrafter/omp/commands/subcommands/UpgradeCommand.php b/src/thebigcrafter/omp/commands/subcommands/UpgradeCommand.php index d3495d1..4e8696c 100644 --- a/src/thebigcrafter/omp/commands/subcommands/UpgradeCommand.php +++ b/src/thebigcrafter/omp/commands/subcommands/UpgradeCommand.php @@ -13,40 +13,44 @@ namespace thebigcrafter\omp\commands\subcommands; -use CortexPE\Commando\args\RawStringArgument; -use CortexPE\Commando\BaseSubCommand; -use Exception; -use Generator; -use pocketmine\command\CommandSender; -use SOFe\AwaitGenerator\Await; -use thebigcrafter\omp\helpers\PluginHelper; -use thebigcrafter\omp\Language; +use CortexPE\Commando\args\RawStringArgument; +use CortexPE\Commando\BaseSubCommand; +use CortexPE\Commando\exception\ArgumentOrderException; +use Exception; +use Generator; +use pocketmine\command\CommandSender; +use SOFe\AwaitGenerator\Await; +use thebigcrafter\omp\helpers\PluginHelper; +use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; -class UpgradeCommand extends BaseSubCommand -{ - protected function prepare() : void - { +class UpgradeCommand extends BaseSubCommand +{ + /** + * @throws ArgumentOrderException + */ + protected function prepare() : void + { $this->setPermission("oh-my-pmmp.upgrade"); - $this->registerArgument(0, new RawStringArgument("name", false)); + $this->registerArgument(0, new RawStringArgument("name", false)); } /** * @param array $args - */ - public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void - { + */ + public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void + { $name = $args["name"]; - Await::f2c(function () use ($name, $sender) : Generator { - try { - yield from PluginHelper::remove($name, false); - } catch (Exception $e) { - $sender->sendMessage(Language::translate("commands.upgrade.failed", ["name" => $name])); - } - }); - OhMyPMMP::getInstance()->getServer()->dispatchCommand($sender, "omp i $name"); - return; - } + Await::f2c(function () use ($name, $sender) : Generator { + try { + yield from PluginHelper::remove($name, false); + } catch (Exception $e) { + $sender->sendMessage(Language::translate("commands.upgrade.failed", ["name" => $name])); + } + }); + OhMyPMMP::getInstance()->getServer()->dispatchCommand($sender, "omp i $name"); + return; + } } diff --git a/src/thebigcrafter/omp/helpers/PharHelper.php b/src/thebigcrafter/omp/helpers/PharHelper.php index 4787ac4..b10da79 100644 --- a/src/thebigcrafter/omp/helpers/PharHelper.php +++ b/src/thebigcrafter/omp/helpers/PharHelper.php @@ -18,6 +18,8 @@ use Generator; use Phar; use SOFe\AwaitGenerator\Await; +use Symfony\Component\Filesystem\Path; +use thebigcrafter\omp\OhMyPMMP; use Throwable; use function file_put_contents; @@ -41,13 +43,16 @@ public static function create(string $path, string $content) : Generator }); } - /** - * Extract a Phar file to specified location - * Throw an exception on failure - */ + /** + *

Extract the PHAR file

+ * + * @param string $filePath + * @param string $to + * @return Generator + */ public static function extract(string $filePath, string $to) : Generator { - return Await::promise(function (Closure $resolve, Closure $reject) use ($filePath, $to) { + return Await::promise(function (Closure $resolve, Closure $reject) use ($to, $filePath) { $phar = new Phar($filePath); try { $phar->extractTo($to); diff --git a/src/thebigcrafter/omp/helpers/PluginHelper.php b/src/thebigcrafter/omp/helpers/PluginHelper.php index 923e2d4..2e50323 100644 --- a/src/thebigcrafter/omp/helpers/PluginHelper.php +++ b/src/thebigcrafter/omp/helpers/PluginHelper.php @@ -1,18 +1,18 @@ - * - * 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\helpers; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * 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\helpers; + use Closure; use Exception; use Generator; @@ -20,48 +20,48 @@ use Symfony\Component\Filesystem\Path; use thebigcrafter\omp\OhMyPMMP; use thebigcrafter\omp\types\PluginType; -use thebigcrafter\omp\Utils; -use thebigcrafter\omp\utils\Filesystem; - +use thebigcrafter\omp\utils\Filesystem; +use thebigcrafter\omp\utils\Utils; + class PluginHelper { - /** - * Remove a plugin by it name and remove it data if it is required. Return true if succeed - * This function will check if the plugin Phar file exist or not, if it does, remove it. - * Else if the plugin folder exists, remove it - * If plugin not found, throw an Exception - */ - public static function remove(string $name, bool $wipeData) : Generator - { - return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($name, $wipeData) { - $pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar"); - $pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name); - - if (Filesystem::exists($pluginFilePath)) { - Await::g2c(Filesystem::remove($pluginFilePath)); - } elseif (Filesystem::exists($pluginFolderPath)) { - Await::g2c(Filesystem::remove($pluginFolderPath)); - } else { - $reject(new Exception("Plugin not found")); - } - if ($wipeData) { - $pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); - Await::g2c(Filesystem::remove($pluginDataFolder)); - } - $resolve(true); - }); - } - - /** - * Return true if plugin exists, false on failure - * This function can check plugin Phar file and plugin folder - */ - public static function exists(string $name, PluginType $type = PluginType::FILE_TYPE) : bool - { - if ($type === PluginType::FILE_TYPE) { - return Filesystem::exists(Path::join(Utils::getPluginsFolder(), "$name.phar")); - } - - return Filesystem::exists(Path::join(Utils::getPluginsFolder(), $name)); - } -} + /** + * Remove a plugin by it name and remove it data if it is required. Return true if succeed + * This function will check if the plugin Phar file exist or not, if it does, remove it. + * Else if the plugin folder exists, remove it + * If plugin not found, throw an Exception + */ + public static function remove(string $name, bool $wipeData): Generator + { + return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($name, $wipeData) { + $pluginFilePath = Utils::generatePluginFilePathWithName($name); + $pluginFolderPath = Utils::generatePluginFolderPathWithName($name); + + if (Filesystem::exists($pluginFilePath)) { + Await::g2c(Filesystem::remove($pluginFilePath)); + } elseif (Filesystem::exists($pluginFolderPath)) { + Await::g2c(Filesystem::remove($pluginFolderPath)); + } else { + $reject(new Exception("Plugin not found")); + } + if ($wipeData) { + $pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name); + Await::g2c(Filesystem::remove($pluginDataFolder)); + } + $resolve(true); + }); + } + + /** + * Return true if plugin exists, false on failure + * This function can check plugin Phar file and plugin folder + */ + public static function exists(string $name, PluginType $type = PluginType::FILE_TYPE): bool + { + if ($type === PluginType::FILE_TYPE) { + return Filesystem::exists(Utils::generatePluginFilePathWithName($name)); + } + + return Filesystem::exists(Utils::generatePluginFolderPathWithName($name)); + } +} diff --git a/src/thebigcrafter/omp/helpers/PoggitHelper.php b/src/thebigcrafter/omp/helpers/PoggitHelper.php new file mode 100644 index 0000000..e60797b --- /dev/null +++ b/src/thebigcrafter/omp/helpers/PoggitHelper.php @@ -0,0 +1,41 @@ +getVersion($version)["plugin"] === null) return false; + + return true; + } +} \ No newline at end of file diff --git a/src/thebigcrafter/omp/lang/Locale.php b/src/thebigcrafter/omp/lang/Locale.php index 37125b2..3a442d7 100644 --- a/src/thebigcrafter/omp/lang/Locale.php +++ b/src/thebigcrafter/omp/lang/Locale.php @@ -23,70 +23,76 @@ /** * This class was made base on utopia-php/locale * GitHub: https://github.com/utopia-php/locale - */ -final class Locale -{ - /** @var array> */ - protected static $languages = []; + */ +final class Locale +{ + /** @var array> */ + protected static array $languages = []; /** * Throw Exceptions? * * @var bool - */ - public static $exceptions = true; + */ + public static bool $exceptions = true; /** * Default Locale * * @var string - */ - public $default; - - public function __construct(string $default) - { - if (!array_key_exists($default, self::$languages)) { - throw new Exception("Locale not found"); + */ + public string $default; + + /** + * @throws Exception + */ + public function __construct(string $default) + { + if (!array_key_exists($default, self::$languages)) { + throw new Exception("Locale not found"); } - $this->default = $default; + $this->default = $default; } - public static function setLanguageFromJSON(string $name, string $path) : void - { - if (!file_exists($path)) { - throw new Exception("Translation file not found."); + /** + * @throws Exception + */ + public static function setLanguageFromJSON(string $name, string $path) : void + { + if (!file_exists($path)) { + throw new Exception("Translation file not found."); } - /** @var array $translations */ - $translations = json_decode(file_get_contents($path) ?: "", true); - self::$languages[$name] = $translations; + /** @var array $translations */ + $translations = json_decode(file_get_contents($path) ?: "", true); + self::$languages[$name] = $translations; } /** * @param array $placeholders - * @return mixed - * + * @return array|string|string[] + * * @throws Exception - */ - public function getText(string $key, array $placeholders = []) - { + */ + public function getText(string $key, array $placeholders = []): array|string + { $default = "{{$key}}"; - if (!array_key_exists($key, self::$languages[$this->default])) { - if (self::$exceptions) { - throw new Exception("Key named $key not found"); + if (!array_key_exists($key, self::$languages[$this->default])) { + if (self::$exceptions) { + throw new Exception("Key named $key not found"); } - return $default; + return $default; } $translation = self::$languages[$this->default][$key]; - foreach ($placeholders as $placeholderKey => $placeholderValue) { - $translation = str_replace("{{" . $placeholderKey . "}}", (string) $placeholderValue, $translation); + foreach ($placeholders as $placeholderKey => $placeholderValue) { + $translation = str_replace("{{" . $placeholderKey . "}}", (string) $placeholderValue, $translation); } - return $translation; - } + return $translation; + } } diff --git a/src/thebigcrafter/omp/tasks/CheckForUpdates.php b/src/thebigcrafter/omp/tasks/CheckForUpdates.php index 316104b..6375906 100644 --- a/src/thebigcrafter/omp/tasks/CheckForUpdates.php +++ b/src/thebigcrafter/omp/tasks/CheckForUpdates.php @@ -1,60 +1,60 @@ - * - * 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; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * 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\scheduler\AsyncTask; use pocketmine\utils\Internet; use thebigcrafter\omp\Language; use thebigcrafter\omp\OhMyPMMP; use function json_decode; -use function version_compare; - +use function version_compare; + final class CheckForUpdates extends AsyncTask { private string $highestVersion; private string $artifactUrl; - public function __construct(private readonly string $name, private string $currentVersion) + public function __construct(private readonly string $name, private readonly string $currentVersion) { $this->highestVersion = $currentVersion; $this->artifactUrl = ""; - } - + } + public function onRun() : void { - $res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name); - - $releases = (array) json_decode($res->getBody(), true); - + $res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name); + + $releases = (array) json_decode($res->getBody(), true); + if ($releases !== null) { - /** - * @var array{'version': string, 'artifact_url': string} $release + /** + * @var array{'version': string, 'artifact_url': string} $release */ foreach ($releases as $release) { if (version_compare($this->highestVersion, $release["version"], ">")) { continue; - } - + } + $this->highestVersion = $release["version"]; $this->artifactUrl = $release["artifact_url"]; } - } - + } + if ($this->highestVersion !== $this->currentVersion) { $this->setResult(false); } - } - + } + public function onCompletion() : void { if (!$this->getResult()) { @@ -67,4 +67,4 @@ public function onCompletion() : void ); } } -} +} diff --git a/src/thebigcrafter/omp/tasks/FetchDataTask.php b/src/thebigcrafter/omp/tasks/FetchDataTask.php index 243a5f3..843631c 100644 --- a/src/thebigcrafter/omp/tasks/FetchDataTask.php +++ b/src/thebigcrafter/omp/tasks/FetchDataTask.php @@ -1,18 +1,18 @@ - * - * 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; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * 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\scheduler\AsyncTask; use pocketmine\utils\Internet; use pocketmine\utils\InternetRequestResult; @@ -24,40 +24,39 @@ use thebigcrafter\omp\types\Dependency; use thebigcrafter\omp\types\Plugin; use thebigcrafter\omp\types\PluginVersion; -use thebigcrafter\omp\Utils; +use thebigcrafter\omp\utils\Utils; use function array_map; use function count; use function json_decode; -use function strval; - +use function strval; + class FetchDataTask extends AsyncTask { public function onRun() : void { - $res = Internet::getURL(Poggit::REPO_URL); - + $res = Internet::getURL(Poggit::REPO_URL); + if (!$res instanceof InternetRequestResult) { return; - } - + } + $this->setResult(json_decode($res->getBody(), true)); - } - + } + public function onCompletion() : void { /** @var array> $data */ - $data = $this->getResult(); - + $data = $this->getResult(); + foreach ($data as $pl) { if (!isset($pl["api"][0])) { continue; - } - - // @phpstan-ignore-next-line + } + if (OhMyPMMP::getInstance()->getConfig()->get("skipIncompatiblePlugins") && !Utils::isMajorVersionInRange(OhMyPMMP::getInstance()->getServer()->getApiVersion(), $pl["api"][0]["from"], $pl["api"][0]["to"])) { continue; - } - + } + if (PoggitPluginsPool::getItem($pl["name"]) === null) { PoggitPluginsPool::addItem($pl["name"], new Plugin($pl["license"] ? $pl["license"] : "")); PoggitPluginsPool::getItem($pl["name"])->addVersion( @@ -92,8 +91,8 @@ public function onCompletion() : void ) ); } - } - + } + OhMyPMMP::getInstance()->getLogger()->info(Language::translate("messages.pool.fetched", ["amount" => count(PoggitPluginsPool::getPool())])); } -} +} diff --git a/src/thebigcrafter/omp/trait/SingletonTrait.php b/src/thebigcrafter/omp/trait/SingletonTrait.php index 61cd92e..257b175 100644 --- a/src/thebigcrafter/omp/trait/SingletonTrait.php +++ b/src/thebigcrafter/omp/trait/SingletonTrait.php @@ -17,7 +17,7 @@ trait SingletonTrait{ /** @var OhMyPMMP|null */ - private static $instance = null; + private static ?OhMyPMMP $instance = null; private static function make() : OhMyPMMP{ // It can create a new OhMyPMMP without any params. Magical! diff --git a/src/thebigcrafter/omp/utils/Filesystem.php b/src/thebigcrafter/omp/utils/Filesystem.php index d39da40..739e486 100644 --- a/src/thebigcrafter/omp/utils/Filesystem.php +++ b/src/thebigcrafter/omp/utils/Filesystem.php @@ -1,63 +1,67 @@ - * - * 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\utils; - + +/* + * This file is part of oh-my-pmmp. + * + * (c) thebigcrafter + * + * 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\utils; + use Closure; use Generator; use SOFe\AwaitGenerator\Await; -use Symfony\Component\Filesystem\Exception\IOException; - +use Symfony\Component\Filesystem\Exception\IOException; + class Filesystem { - /** - * Files and folders remover + /** + * Remove files and folder */ public static function remove(string $path) : Generator { return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path) { $fs = new \Symfony\Component\Filesystem\Filesystem(); try { - // @phpstan-ignore-next-line - $resolve($fs->remove($path)); + $fs->remove($path); + $resolve(); } catch (IOException $e) { $reject($e); } }); - } - - /** - * Rename files and directories, can move them also + } + + /** + * Rename files and directories, can move them also */ public static function rename(string $origin, string $target) : Generator { return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($origin, $target) { $fs = new \Symfony\Component\Filesystem\Filesystem(); try { - // @phpstan-ignore-next-line - $resolve($fs->rename($origin, $target)); + $fs->rename($origin, $target); + $resolve(); } catch (IOException $e) { $reject($e); } }); - } - - /** - * Check if files or folders exist - */ + } + + /** + * Checks the existence of files or directories. + * + * @param iterable|string $files + * + * @return bool + */ public static function exists(iterable|string $files) : bool { $fs = new \Symfony\Component\Filesystem\Filesystem(); return $fs->exists($files); } -} +} diff --git a/src/thebigcrafter/omp/utils/Internet.php b/src/thebigcrafter/omp/utils/Internet.php index 2dcecbc..f94e55e 100644 --- a/src/thebigcrafter/omp/utils/Internet.php +++ b/src/thebigcrafter/omp/utils/Internet.php @@ -13,22 +13,23 @@ namespace thebigcrafter\omp\utils; -use Exception; +use Exception; use SOFe\AwaitGenerator\Await; -final class Internet { - public static function fetch(string $url) : \Generator { - return yield from Await::promise(function (\Closure $resolve, \Closure $reject) use ($url) { +final class Internet { + public static function fetch(string $url) : \Generator { + return yield from Await::promise(function (\Closure $resolve, \Closure $reject) use ($url) { $res = \pocketmine\utils\Internet::getURL($url, 10, [], $err); /** * @var string $err - */ - if($err) { - $reject(new Exception($err)); + */ + if($err || $res === null) { + $reject(new Exception($err)); + return; } - $resolve($res->getBody()); - }); - } + $resolve($res->getBody()); + }); + } } diff --git a/src/thebigcrafter/omp/utils/Utils.php b/src/thebigcrafter/omp/utils/Utils.php new file mode 100644 index 0000000..86d1bf2 --- /dev/null +++ b/src/thebigcrafter/omp/utils/Utils.php @@ -0,0 +1,52 @@ +Generate an absolute plugin folder path with it name.

+ *

For example, with $name = "oh-my-pmmp", it will be /root/Server/plugins/oh-my-pmmp

+ * + * @param string $name + * @return string + */ + public static function generatePluginFolderPathWithName(string $name): string + { + return Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins/$name"); + } + + /** + *

Generate an absolute plugin PHAR file path with it name.

+ *

For example, with $name = "oh-my-pmmp", it will be /root/Server/plugins/oh-my-pmmp.phar

+ * + * @param string $name + * @return string + */ + public static function generatePluginFilePathWithName(string $name): string + { + return Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar"); + } + + /** + *

Check if the major version in range

+ *

For example, if $minVersion = 5, $maxVersion = 5, return true if version is 5.X.X, otherwise false

+ * + * @param string $checkVersion + * @param string $minVersion + * @param string $maxVersion + * @return bool + */ + public static function isMajorVersionInRange(string $checkVersion, string $minVersion, string $maxVersion) : bool + { + $checkMajor = (int) explode('.', $checkVersion)[0]; + $minMajor = (int) explode('.', $minVersion)[0]; + $maxMajor = (int) explode('.', $maxVersion)[0]; + + return $checkMajor >= $minMajor && $checkMajor <= $maxMajor; + } + +} \ No newline at end of file