Skip to content

Commit

Permalink
Code cleanup #1
Browse files Browse the repository at this point in the history
  • Loading branch information
toby7002 committed Jan 2, 2024
1 parent c3d7093 commit 0e04a6e
Show file tree
Hide file tree
Showing 20 changed files with 461 additions and 371 deletions.
4 changes: 3 additions & 1 deletion src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
52 changes: 0 additions & 52 deletions src/thebigcrafter/omp/Utils.php

This file was deleted.

14 changes: 9 additions & 5 deletions src/thebigcrafter/omp/commands/subcommands/DisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@

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\Exception\IOException;
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");

Expand All @@ -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;
}
Expand Down
10 changes: 7 additions & 3 deletions src/thebigcrafter/omp/commands/subcommands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@

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\Exception\IOException;
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");

Expand All @@ -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)) {
Expand Down
13 changes: 8 additions & 5 deletions src/thebigcrafter/omp/commands/subcommands/ExtractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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]));
Expand All @@ -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()]));
Expand Down
15 changes: 10 additions & 5 deletions src/thebigcrafter/omp/commands/subcommands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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");

Expand Down
56 changes: 30 additions & 26 deletions src/thebigcrafter/omp/commands/subcommands/ListCommand.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

/*
* This file is part of oh-my-pmmp.
*
* (c) thebigcrafter <hello@thebigcrafter.team>
*
* 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 <hello@thebigcrafter.team>
*
* 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;
Expand All @@ -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<string> $args
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
Expand All @@ -48,13 +52,13 @@ 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()]));
$sender->sendMessage(Language::translate("commands.list.form.versions", ["versions" => implode(", ", $plugin->getVersionsOnly())]));
$sender->sendMessage("====================");
}
}
}
}
6 changes: 5 additions & 1 deletion src/thebigcrafter/omp/commands/subcommands/RemoveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,7 +25,10 @@

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

Expand Down
Loading

0 comments on commit 0e04a6e

Please sign in to comment.