Skip to content

Commit

Permalink
No more amphp's libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Nov 30, 2023
1 parent f568542 commit 110ff18
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 252 deletions.
1 change: 0 additions & 1 deletion box.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"LICENSE"
],
"directories": [
"vendor",
"resources",
"src"
]
Expand Down
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"type": "project",
"require": {
"php": "^8.1",
"amphp/file": "^3.0",
"amphp/http-client": "^5.0",
"thebigcrafter/commando": "dev-master"
},
"require-dev": {
Expand Down
23 changes: 5 additions & 18 deletions src/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,12 @@

namespace thebigcrafter\omp;

require __DIR__ . "/../vendor/autoload.php";

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use pocketmine\plugin\PluginBase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\commands\OMPCommand;
use thebigcrafter\omp\pool\PoggitPluginsPool;
use thebigcrafter\omp\tasks\FetchDataTask;
use thebigcrafter\omp\trait\SingletonTrait;
use thebigcrafter\omp\types\API;
use thebigcrafter\omp\types\Dependency;
use thebigcrafter\omp\types\Plugin;
use thebigcrafter\omp\types\PluginVersion;
use function Amp\File\createDirectory;
use function Amp\File\isDirectory;
use function array_map;
use function count;
use function json_decode;
use function strval;

class OhMyPMMP extends PluginBase
{
Expand All @@ -56,15 +42,16 @@ public function onEnable() : void
public function fetchData() : void
{
$this->getLogger()->info(Language::translate("messages.pool.fetching", []));
$this->getServer()->getAsyncPool()->submitTask(new FetchDataTask($this));
$this->getServer()->getAsyncPool()->submitTask(new FetchDataTask());
}

private function createFolders() : void
{
$fs = new Filesystem();
$disabledPluginsFolderPath = Path::join($this->getServer()->getPluginPath(), "..", "disabled_plugins");

if (!isDirectory($disabledPluginsFolderPath)) {
createDirectory($disabledPluginsFolderPath);
if (!$fs->exists($disabledPluginsFolderPath)) {
$fs->mkdir($disabledPluginsFolderPath);
}
}
}
5 changes: 3 additions & 2 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace thebigcrafter\omp;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use function Amp\File\exists;
use function explode;

class Utils
Expand All @@ -38,7 +38,8 @@ public static function getPluginsFolder() : string
*/
public static function doesPluginExist(string $name) : bool
{
return exists(self::getPluginFilePath($name));
$fs = new Filesystem();
return $fs->exists(self::getPluginFilePath($name));
}

/**
Expand Down
51 changes: 26 additions & 25 deletions src/commands/subcommands/DisableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,41 @@

namespace thebigcrafter\omp\commands\subcommands;

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\Utils;
use function Amp\File\move;

class DisableCommand extends BaseSubCommand
{
protected function prepare() : void
{
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\Utils;

class DisableCommand extends BaseSubCommand
{
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.disable");

$this->registerArgument(0, new RawStringArgument("name", false));
$this->registerArgument(0, new RawStringArgument("name", false));
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$name = $args["name"];
$oldPluginFilePath = Utils::getPluginFilePath($name);
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$fs = new Filesystem();
$name = $args["name"];
$oldPluginFilePath = Utils::getPluginFilePath($name);
$newPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar");

if (!Utils::doesPluginExist($name)) {
$sender->sendMessage(Language::translate("commands.disable.failed", ["name" => $name]));
return;
if (!Utils::doesPluginExist($name)) {
$sender->sendMessage(Language::translate("commands.disable.failed", ["name" => $name]));
return;
}

move($oldPluginFilePath, $newPluginFilePath);
$fs->rename($oldPluginFilePath, $newPluginFilePath);

$sender->sendMessage(Language::translate("commands.disable.successfully", ["name" => $name]));
}
$sender->sendMessage(Language::translate("commands.disable.successfully", ["name" => $name]));
}
}
52 changes: 26 additions & 26 deletions src/commands/subcommands/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,41 @@

namespace thebigcrafter\omp\commands\subcommands;

use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\Utils;
use function Amp\File\exists;
use function Amp\File\move;

class EnableCommand extends BaseSubCommand
{
protected function prepare() : void
{
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\Utils;

class EnableCommand extends BaseSubCommand
{
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.enable");

$this->registerArgument(0, new RawStringArgument("name", false));
$this->registerArgument(0, new RawStringArgument("name", false));
}

/**
* @param array<string> $args
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$name = $args["name"];
$newPluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$fs = new Filesystem();
$name = $args["name"];
$newPluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");
$oldPluginFilePath = Path::join(OhMyPMMP::getInstance()->getServer()->getPluginPath(), "..", "disabled_plugins", "$name.phar");

if (!exists($oldPluginFilePath)) {
$sender->sendMessage(Language::translate("commands.enable.failed", ["name" => $name]));
return;
if (!$fs->exists($oldPluginFilePath)) {
$sender->sendMessage(Language::translate("commands.enable.failed", ["name" => $name]));
return;
}

move($oldPluginFilePath, $newPluginFilePath);
$fs->rename($oldPluginFilePath, $newPluginFilePath);

$sender->sendMessage(Language::translate("commands.enable.successfully", ["name" => $name]));
}
$sender->sendMessage(Language::translate("commands.enable.successfully", ["name" => $name]));
}
}
6 changes: 3 additions & 3 deletions src/commands/subcommands/ExtractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
use CortexPE\Commando\BaseSubCommand;
use Phar;
use pocketmine\command\CommandSender;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\Utils;

use function Amp\File\exists;

class ExtractCommand extends BaseSubCommand {
protected function prepare() : void
{
Expand All @@ -36,10 +35,11 @@ protected function prepare() : void
*/
public function onRun(CommandSender $sender, string $aliasUsed, array $args) : void
{
$fs = new Filesystem();
$name = $args["name"];
$pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");

if(!exists($pluginFilePath)) {
if(!$fs->exists($pluginFilePath)) {
$sender->sendMessage(Language::translate("commands.extract.failed", ["name" => $name]));
return;
}
Expand Down
23 changes: 9 additions & 14 deletions src/commands/subcommands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@

namespace thebigcrafter\omp\commands\subcommands;

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;
use CortexPE\Commando\args\RawStringArgument;
use CortexPE\Commando\BaseSubCommand;
use pocketmine\command\CommandSender;
use pocketmine\utils\Internet;
use pocketmine\utils\InternetRequestResult;
use Symfony\Component\Filesystem\Path;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\pool\PoggitPluginsPool;
use function Amp\File\openFile;
use function file_put_contents;
use function is_null;

class InstallCommand extends BaseSubCommand {
class InstallCommand extends BaseSubCommand
{
protected function prepare() : void
{
$this->setPermission("oh-my-pmmp.install");
Expand Down Expand Up @@ -60,22 +61,16 @@ public function onRun(CommandSender $sender, string $aliasUsed, array $args) : v
// aka $version if user provides a specified version
$latestVersion = (string) $pluginVersion["version"];

$client = HttpClientBuilder::buildDefault();
$res = Internet::getURL($info->getArtifactUrl());

$res = $client->request(new Request($info->getArtifactUrl()));

if($res->getStatus() !== 200) {
$sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $res->getReason()]));
if (!$res instanceof InternetRequestResult || $res->getCode() !== 200) {
$sender->sendMessage(Language::translate("messages.operation.failed", ["reason" => $res->getCode()]));
return;
}

$pharPath = Path::join(OhMyPMMP::getInstance()->getServer()->getDataPath(), "plugins", "$name.phar");
$phar = openFile($pharPath, "w");

while (($chunk = $res->getBody()->read()) !== null) {
$phar->write((string) $chunk);
}
$phar->end();
file_put_contents($pharPath, $res->getBody());

$sender->sendMessage(Language::translate("commands.install.successfully", ["name" => $name, "version" => $latestVersion]));
}
Expand Down
Loading

0 comments on commit 110ff18

Please sign in to comment.