mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 07:54:19 +00:00
Convert WorldProviderManager to singleton
This commit is contained in:
parent
e2232dd8d4
commit
640428c415
@ -1007,12 +1007,12 @@ class Server{
|
||||
$this->pluginManager->registerInterface(new PharPluginLoader($this->autoloader));
|
||||
$this->pluginManager->registerInterface(new ScriptPluginLoader());
|
||||
|
||||
WorldProviderManager::init();
|
||||
$providerManager = WorldProviderManager::getInstance();
|
||||
if(
|
||||
($format = WorldProviderManager::getProviderByName($formatName = (string) $this->getProperty("level-settings.default-format"))) !== null and
|
||||
($format = $providerManager->getProviderByName($formatName = (string) $this->getProperty("level-settings.default-format"))) !== null and
|
||||
is_a($format, WritableWorldProvider::class, true)
|
||||
){
|
||||
WorldProviderManager::setDefault($format);
|
||||
$providerManager->setDefault($format);
|
||||
}elseif($formatName !== ""){
|
||||
$this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName]));
|
||||
}
|
||||
|
@ -179,7 +179,7 @@ class WorldManager{
|
||||
|
||||
$path = $this->getWorldPath($name);
|
||||
|
||||
$providers = WorldProviderManager::getMatchingProviders($path);
|
||||
$providers = WorldProviderManager::getInstance()->getMatchingProviders($path);
|
||||
if(count($providers) !== 1){
|
||||
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [
|
||||
$name,
|
||||
@ -216,7 +216,7 @@ class WorldManager{
|
||||
}
|
||||
$this->server->getLogger()->notice("Upgrading world \"$name\" to new format. This may take a while.");
|
||||
|
||||
$converter = new FormatConverter($provider, WorldProviderManager::getDefault(), $this->server->getDataPath() . "world_conversion_backups", $this->server->getLogger());
|
||||
$converter = new FormatConverter($provider, WorldProviderManager::getInstance()->getDefault(), $this->server->getDataPath() . "world_conversion_backups", $this->server->getLogger());
|
||||
$provider = $converter->execute();
|
||||
|
||||
$this->server->getLogger()->notice("Upgraded world \"$name\" to new format successfully. Backed up pre-conversion world at " . $converter->getBackupPath());
|
||||
@ -251,7 +251,7 @@ class WorldManager{
|
||||
|
||||
Utils::testValidInstance($generator, Generator::class);
|
||||
|
||||
$providerClass = WorldProviderManager::getDefault();
|
||||
$providerClass = WorldProviderManager::getInstance()->getDefault();
|
||||
|
||||
$path = $this->getWorldPath($name);
|
||||
/** @var WritableWorldProvider $providerClass */
|
||||
@ -307,7 +307,7 @@ class WorldManager{
|
||||
}
|
||||
$path = $this->getWorldPath($name);
|
||||
if(!($this->getWorldByName($name) instanceof World)){
|
||||
return count(WorldProviderManager::getMatchingProviders($path)) > 0;
|
||||
return count(WorldProviderManager::getInstance()->getMatchingProviders($path)) > 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format\io;
|
||||
|
||||
use pocketmine\utils\SingletonTrait;
|
||||
use pocketmine\utils\Utils;
|
||||
use pocketmine\world\format\io\leveldb\LevelDB;
|
||||
use pocketmine\world\format\io\region\Anvil;
|
||||
@ -31,20 +32,22 @@ use pocketmine\world\format\io\region\PMAnvil;
|
||||
use function strtolower;
|
||||
use function trim;
|
||||
|
||||
abstract class WorldProviderManager{
|
||||
final class WorldProviderManager{
|
||||
use SingletonTrait;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
* @phpstan-var array<string, class-string<WorldProvider>>
|
||||
*/
|
||||
protected static $providers = [];
|
||||
protected $providers = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @phpstan-var class-string<WritableWorldProvider>
|
||||
*/
|
||||
private static $default = LevelDB::class;
|
||||
private $default = LevelDB::class;
|
||||
|
||||
public static function init() : void{
|
||||
public function __construct(){
|
||||
self::addProvider(Anvil::class, "anvil");
|
||||
self::addProvider(McRegion::class, "mcregion");
|
||||
self::addProvider(PMAnvil::class, "pmanvil");
|
||||
@ -56,8 +59,8 @@ abstract class WorldProviderManager{
|
||||
*
|
||||
* @phpstan-return class-string<WritableWorldProvider>
|
||||
*/
|
||||
public static function getDefault() : string{
|
||||
return self::$default;
|
||||
public function getDefault() : string{
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,25 +71,25 @@ abstract class WorldProviderManager{
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function setDefault(string $class) : void{
|
||||
public function setDefault(string $class) : void{
|
||||
Utils::testValidInstance($class, WritableWorldProvider::class);
|
||||
|
||||
self::$default = $class;
|
||||
$this->default = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-param class-string<WorldProvider> $class
|
||||
*/
|
||||
public static function addProvider(string $class, string $name, bool $overwrite = false) : void{
|
||||
public function addProvider(string $class, string $name, bool $overwrite = false) : void{
|
||||
Utils::testValidInstance($class, WorldProvider::class);
|
||||
|
||||
$name = strtolower($name);
|
||||
if(!$overwrite and isset(self::$providers[$name])){
|
||||
if(!$overwrite and isset($this->providers[$name])){
|
||||
throw new \InvalidArgumentException("Alias \"$name\" is already assigned");
|
||||
}
|
||||
|
||||
/** @var WorldProvider $class */
|
||||
self::$providers[$name] = $class;
|
||||
$this->providers[$name] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,9 +98,9 @@ abstract class WorldProviderManager{
|
||||
* @return string[]
|
||||
* @phpstan-return array<string, class-string<WorldProvider>>
|
||||
*/
|
||||
public static function getMatchingProviders(string $path) : array{
|
||||
public function getMatchingProviders(string $path) : array{
|
||||
$result = [];
|
||||
foreach(self::$providers as $alias => $provider){
|
||||
foreach($this->providers as $alias => $provider){
|
||||
if($provider::isValid($path)){
|
||||
$result[$alias] = $provider;
|
||||
}
|
||||
@ -109,8 +112,8 @@ abstract class WorldProviderManager{
|
||||
* @return string[]
|
||||
* @phpstan-return array<string, class-string<WorldProvider>>
|
||||
*/
|
||||
public static function getAvailableProviders() : array{
|
||||
return self::$providers;
|
||||
public function getAvailableProviders() : array{
|
||||
return $this->providers;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,7 +121,7 @@ abstract class WorldProviderManager{
|
||||
*
|
||||
* @phpstan-return class-string<WorldProvider>|null
|
||||
*/
|
||||
public static function getProviderByName(string $name) : ?string{
|
||||
return self::$providers[trim(strtolower($name))] ?? null;
|
||||
public function getProviderByName(string $name) : ?string{
|
||||
return $this->providers[trim(strtolower($name))] ?? null;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
parameters:
|
||||
ignoreErrors:
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\>, class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\> given\\.$#"
|
||||
message: "#^Parameter \\#1 \\$class of method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\>, class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\> given\\.$#"
|
||||
count: 1
|
||||
path: ../../../src/Server.php
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
parameters:
|
||||
ignoreErrors:
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:addProvider\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\>, string given\\.$#"
|
||||
message: "#^Parameter \\#1 \\$class of method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:addProvider\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\>, string given\\.$#"
|
||||
count: 2
|
||||
path: ../../phpunit/world/format/io/LevelProviderManagerTest.php
|
||||
|
||||
|
@ -27,27 +27,34 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class LevelProviderManagerTest extends TestCase{
|
||||
|
||||
/** @var WorldProviderManager */
|
||||
private $providerManager;
|
||||
|
||||
protected function setUp() : void{
|
||||
$this->providerManager = new WorldProviderManager();
|
||||
}
|
||||
|
||||
public function testAddNonClassProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
WorldProviderManager::addProvider("lol", "nope");
|
||||
$this->providerManager->addProvider("lol", "nope");
|
||||
}
|
||||
|
||||
public function testAddAbstractClassProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
WorldProviderManager::addProvider(AbstractWorldProvider::class, "abstract");
|
||||
$this->providerManager->addProvider(AbstractWorldProvider::class, "abstract");
|
||||
}
|
||||
|
||||
public function testAddInterfaceProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
WorldProviderManager::addProvider(InterfaceWorldProvider::class, "interface");
|
||||
$this->providerManager->addProvider(InterfaceWorldProvider::class, "interface");
|
||||
}
|
||||
|
||||
public function testAddWrongClassProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
WorldProviderManager::addProvider(LevelProviderManagerTest::class, "bad_class");
|
||||
$this->providerManager->addProvider(LevelProviderManagerTest::class, "bad_class");
|
||||
}
|
||||
}
|
||||
|
@ -29,10 +29,9 @@ use pocketmine\world\generator\GeneratorManager;
|
||||
|
||||
require_once dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
WorldProviderManager::init();
|
||||
GeneratorManager::registerDefaultGenerators();
|
||||
|
||||
$writableFormats = array_filter(WorldProviderManager::getAvailableProviders(), function(string $class){
|
||||
$writableFormats = array_filter(WorldProviderManager::getInstance()->getAvailableProviders(), function(string $class){
|
||||
return is_a($class, WritableWorldProvider::class, true);
|
||||
});
|
||||
$requiredOpts = [
|
||||
@ -63,7 +62,7 @@ if((!@mkdir($backupPath, 0777, true) and !is_dir($backupPath)) or !is_writable($
|
||||
die("Backup file path " . $backupPath . " is not writable (permission error or doesn't exist), aborting");
|
||||
}
|
||||
|
||||
$oldProviderClasses = WorldProviderManager::getMatchingProviders($inputPath);
|
||||
$oldProviderClasses = WorldProviderManager::getInstance()->getMatchingProviders($inputPath);
|
||||
if(count($oldProviderClasses) === 0){
|
||||
die("Unknown input world format");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user