Skip to content

Commit

Permalink
Skip incompatible plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Nov 24, 2023
1 parent b117fd6 commit 7e49afa
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 72 deletions.
3 changes: 2 additions & 1 deletion resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
language: en_us

pluginsPerPage: 10
pluginsPerPage: 10
skipIncompatiblePlugins: true # It will skip caching outdated API plugins if it is set to true
146 changes: 75 additions & 71 deletions src/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,75 +32,79 @@

class OhMyPMMP extends PluginBase
{
use SingletonTrait;
public function onLoad() : void
{
self::setInstance($this);
Language::loadLanguages();
}

public function onEnable() : void
{
$this->fetchData();
$this->saveDefaultConfig();

$this->getServer()->getCommandMap()->register("OhMyPMMP", new OMPCommand($this, "ohmypmmp", "Oh My PMMP", ["omp", "oh-my-pmmp"]));
}

private function fetchData() : void
{
$this->getLogger()->info(Language::translate("messages.pool.fetching", []));
$client = HttpClientBuilder::buildDefault();

$res = $client->request(new Request(Vars::POGGIT_REPO_URL));

if ($res->getStatus() !== 200) {
return;
}

$data = json_decode($res->getBody()->buffer(), true);

foreach ($data as $pl) {
if (!isset($pl["api"][0])) {
continue;
}

if (PoggitPluginsPool::getItem($pl["name"]) === null) {
PoggitPluginsPool::addItem($pl["name"], new Plugin($pl["license"] ? $pl["license"] : ""));
PoggitPluginsPool::getItem($pl["name"])->addVersion(
$pl["version"],
new PluginVersion(
$pl["html_url"],
$pl["artifact_url"],
$pl["downloads"],
$pl["score"],
$pl["description_url"],
$pl["changelog_url"] ? $pl["changelog_url"] : "",
new API($pl["api"][0]["from"], $pl["api"][0]["to"]),
array_map(function ($dep) {
return new Dependency($dep["name"], $dep["version"], strval($dep["depRelId"]), $dep["isHard"]);
}, $pl["deps"])
)
);
} else {
PoggitPluginsPool::getItem($pl["name"])->addVersion(
$pl["version"],
new PluginVersion(
$pl["html_url"],
$pl["artifact_url"],
$pl["downloads"],
$pl["score"],
$pl["description_url"],
$pl["changelog_url"] ? $pl["changelog_url"] : "",
new API($pl["api"][0]["from"], $pl["api"][0]["to"]),
array_map(function ($dep) {
return new Dependency($dep["name"], $dep["version"], strval($dep["depRelId"]), $dep["isHard"]);
}, $pl["deps"])
)
);
}
}

$this->getLogger()->info(Language::translate("messages.pool.fetched", ["amount" => count(PoggitPluginsPool::getPool())]));
}
use SingletonTrait;
public function onLoad(): void
{
self::setInstance($this);
Language::loadLanguages();
}

public function onEnable(): void
{
$this->fetchData();
$this->saveDefaultConfig();

$this->getServer()->getCommandMap()->register("OhMyPMMP", new OMPCommand($this, "ohmypmmp", "Oh My PMMP", ["omp", "oh-my-pmmp"]));
}

private function fetchData(): void
{
$this->getLogger()->info(Language::translate("messages.pool.fetching", []));
$client = HttpClientBuilder::buildDefault();

$res = $client->request(new Request(Vars::POGGIT_REPO_URL));

if ($res->getStatus() !== 200) {
return;
}

$data = json_decode($res->getBody()->buffer(), true);

foreach ($data as $pl) {
if (!isset($pl["api"][0])) {
continue;
}

if ($this->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(
$pl["version"],
new PluginVersion(
$pl["html_url"],
$pl["artifact_url"],
$pl["downloads"],
$pl["score"],
$pl["description_url"],
$pl["changelog_url"] ? $pl["changelog_url"] : "",
new API($pl["api"][0]["from"], $pl["api"][0]["to"]),
array_map(function ($dep) {
return new Dependency($dep["name"], $dep["version"], strval($dep["depRelId"]), $dep["isHard"]);
}, $pl["deps"])
)
);
} else {
PoggitPluginsPool::getItem($pl["name"])->addVersion(
$pl["version"],
new PluginVersion(
$pl["html_url"],
$pl["artifact_url"],
$pl["downloads"],
$pl["score"],
$pl["description_url"],
$pl["changelog_url"] ? $pl["changelog_url"] : "",
new API($pl["api"][0]["from"], $pl["api"][0]["to"]),
array_map(function ($dep) {
return new Dependency($dep["name"], $dep["version"], strval($dep["depRelId"]), $dep["isHard"]);
}, $pl["deps"])
)
);
}
}

$this->getLogger()->info(Language::translate("messages.pool.fetched", ["amount" => count(PoggitPluginsPool::getPool())]));
}
}
15 changes: 15 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace thebigcrafter\omp;

class Utils
{
public static function isMajorVersionInRange($checkVersion, $minVersion, $maxVersion)
{
$checkMajor = (int) explode('.', $checkVersion)[0];
$minMajor = (int) explode('.', $minVersion)[0];
$maxMajor = (int) explode('.', $maxVersion)[0];

return $checkMajor >= $minMajor && $checkMajor <= $maxMajor;
}
}

0 comments on commit 7e49afa

Please sign in to comment.