From c9af5ce7a95b8579a733354027677e97c164f03c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 23 May 2020 10:13:03 +0100 Subject: [PATCH] Convert GeneratorManager to singleton --- src/Server.php | 5 +-- src/world/World.php | 2 +- src/world/WorldManager.php | 2 +- src/world/format/io/FormatConverter.php | 2 +- src/world/format/io/data/BedrockWorldData.php | 2 +- src/world/format/io/data/JavaWorldData.php | 2 +- src/world/generator/GeneratorManager.php | 44 +++++++++---------- tests/phpstan/configs/l8-baseline.neon | 2 +- tools/convert-world.php | 3 -- 9 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/Server.php b/src/Server.php index 4744a2c98..38f192f5f 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1017,7 +1017,6 @@ class Server{ $this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName])); } - GeneratorManager::registerDefaultGenerators(); $this->worldManager = new WorldManager($this, $this->dataPath . "/worlds"); $this->worldManager->setAutoSave($this->getConfigBool("auto-save", $this->worldManager->getAutoSave())); $this->worldManager->setAutoSaveInterval((int) $this->getProperty("ticks-per.autosave", 6000)); @@ -1040,7 +1039,7 @@ class Server{ if(!$this->worldManager->loadWorld($name, true)){ if(isset($options["generator"])){ $generatorOptions = explode(":", $options["generator"]); - $generator = GeneratorManager::getGenerator(array_shift($generatorOptions)); + $generator = GeneratorManager::getInstance()->getGenerator(array_shift($generatorOptions)); if(count($options) > 0){ $options["preset"] = implode(":", $generatorOptions); } @@ -1063,7 +1062,7 @@ class Server{ $this->worldManager->generateWorld( $default, Generator::convertSeed($this->getConfigString("level-seed")), - GeneratorManager::getGenerator($this->getConfigString("level-type")), + GeneratorManager::getInstance()->getGenerator($this->getConfigString("level-type")), ["preset" => $this->getConfigString("generator-settings")] ); } diff --git a/src/world/World.php b/src/world/World.php index 8e88ab79c..3d46df649 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -335,7 +335,7 @@ class World implements ChunkManager{ $this->worldHeight = $this->provider->getWorldHeight(); $this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.level.preparing", [$this->displayName])); - $this->generator = GeneratorManager::getGenerator($this->provider->getWorldData()->getGenerator(), true); + $this->generator = GeneratorManager::getInstance()->getGenerator($this->provider->getWorldData()->getGenerator(), true); //TODO: validate generator options $this->folderName = $name; diff --git a/src/world/WorldManager.php b/src/world/WorldManager.php index d641598fb..02f88d868 100644 --- a/src/world/WorldManager.php +++ b/src/world/WorldManager.php @@ -205,7 +205,7 @@ class WorldManager{ return false; } try{ - GeneratorManager::getGenerator($provider->getWorldData()->getGenerator(), true); + GeneratorManager::getInstance()->getGenerator($provider->getWorldData()->getGenerator(), true); }catch(\InvalidArgumentException $e){ $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [$name, "Unknown generator \"" . $provider->getWorldData()->getGenerator() . "\""])); return false; diff --git a/src/world/format/io/FormatConverter.php b/src/world/format/io/FormatConverter.php index cc01bd161..d60be76b0 100644 --- a/src/world/format/io/FormatConverter.php +++ b/src/world/format/io/FormatConverter.php @@ -101,7 +101,7 @@ class FormatConverter{ $this->logger->info("Found previous conversion attempt, deleting..."); Filesystem::recursiveUnlink($convertedOutput); } - $this->newProvider::generate($convertedOutput, $data->getName(), $data->getSeed(), GeneratorManager::getGenerator($data->getGenerator()), $data->getGeneratorOptions()); + $this->newProvider::generate($convertedOutput, $data->getName(), $data->getSeed(), GeneratorManager::getInstance()->getGenerator($data->getGenerator()), $data->getGeneratorOptions()); /** * @see WritableWorldProvider::__construct() diff --git a/src/world/format/io/data/BedrockWorldData.php b/src/world/format/io/data/BedrockWorldData.php index ad50b9ddd..a3139a90c 100644 --- a/src/world/format/io/data/BedrockWorldData.php +++ b/src/world/format/io/data/BedrockWorldData.php @@ -102,7 +102,7 @@ class BedrockWorldData extends BaseNbtWorldData{ //Additional PocketMine-MP fields ->setTag("GameRules", new CompoundTag()) ->setByte("hardcore", ($options["hardcore"] ?? false) === true ? 1 : 0) - ->setString("generatorName", GeneratorManager::getGeneratorName($generator)) + ->setString("generatorName", GeneratorManager::getInstance()->getGeneratorName($generator)) ->setString("generatorOptions", $options["preset"] ?? ""); $nbt = new LittleEndianNbtSerializer(); diff --git a/src/world/format/io/data/JavaWorldData.php b/src/world/format/io/data/JavaWorldData.php index 271728f5d..f01d1446d 100644 --- a/src/world/format/io/data/JavaWorldData.php +++ b/src/world/format/io/data/JavaWorldData.php @@ -64,7 +64,7 @@ class JavaWorldData extends BaseNbtWorldData{ ->setLong("RandomSeed", $seed) ->setLong("SizeOnDisk", 0) ->setLong("Time", 0) - ->setString("generatorName", GeneratorManager::getGeneratorName($generator)) + ->setString("generatorName", GeneratorManager::getInstance()->getGeneratorName($generator)) ->setString("generatorOptions", $options["preset"] ?? "") ->setString("LevelName", $name) ->setTag("GameRules", new CompoundTag()); diff --git a/src/world/generator/GeneratorManager.php b/src/world/generator/GeneratorManager.php index b55640750..58811f05f 100644 --- a/src/world/generator/GeneratorManager.php +++ b/src/world/generator/GeneratorManager.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\generator; +use pocketmine\utils\SingletonTrait; use pocketmine\utils\Utils; use pocketmine\world\generator\hell\Nether; use pocketmine\world\generator\normal\Normal; @@ -30,21 +31,20 @@ use function array_keys; use function strtolower; final class GeneratorManager{ + use SingletonTrait; + /** * @var string[] name => classname mapping * @phpstan-var array> */ - private static $list = []; + private $list = []; - /** - * Registers the default known generators. - */ - public static function registerDefaultGenerators() : void{ - self::addGenerator(Flat::class, "flat"); - self::addGenerator(Normal::class, "normal"); - self::addGenerator(Normal::class, "default"); - self::addGenerator(Nether::class, "hell"); - self::addGenerator(Nether::class, "nether"); + public function __construct(){ + $this->addGenerator(Flat::class, "flat"); + $this->addGenerator(Normal::class, "normal"); + $this->addGenerator(Normal::class, "default"); + $this->addGenerator(Nether::class, "hell"); + $this->addGenerator(Nether::class, "nether"); } /** @@ -55,14 +55,14 @@ final class GeneratorManager{ * * @throws \InvalidArgumentException */ - public static function addGenerator(string $class, string $name, bool $overwrite = false) : void{ + public function addGenerator(string $class, string $name, bool $overwrite = false) : void{ Utils::testValidInstance($class, Generator::class); - if(!$overwrite and isset(self::$list[$name = strtolower($name)])){ + if(!$overwrite and isset($this->list[$name = strtolower($name)])){ throw new \InvalidArgumentException("Alias \"$name\" is already assigned"); } - self::$list[$name] = $class; + $this->list[$name] = $class; } /** @@ -70,8 +70,8 @@ final class GeneratorManager{ * * @return string[] */ - public static function getGeneratorList() : array{ - return array_keys(self::$list); + public function getGeneratorList() : array{ + return array_keys($this->list); } /** @@ -84,9 +84,9 @@ final class GeneratorManager{ * * @throws \InvalidArgumentException if the generator type isn't registered */ - public static function getGenerator(string $name, bool $throwOnMissing = false){ - if(isset(self::$list[$name = strtolower($name)])){ - return self::$list[$name]; + public function getGenerator(string $name, bool $throwOnMissing = false){ + if(isset($this->list[$name = strtolower($name)])){ + return $this->list[$name]; } if($throwOnMissing){ @@ -103,9 +103,9 @@ final class GeneratorManager{ * * @throws \InvalidArgumentException if the class type cannot be matched to a known alias */ - public static function getGeneratorName(string $class) : string{ + public function getGeneratorName(string $class) : string{ Utils::testValidInstance($class, Generator::class); - foreach(self::$list as $name => $c){ + foreach($this->list as $name => $c){ if($c === $class){ return $name; } @@ -113,8 +113,4 @@ final class GeneratorManager{ throw new \InvalidArgumentException("Generator class $class is not registered"); } - - private function __construct(){ - //NOOP - } } diff --git a/tests/phpstan/configs/l8-baseline.neon b/tests/phpstan/configs/l8-baseline.neon index fe34314b6..bf2e09ae2 100644 --- a/tests/phpstan/configs/l8-baseline.neon +++ b/tests/phpstan/configs/l8-baseline.neon @@ -11,7 +11,7 @@ parameters: path: ../../../build/server-phar.php - - message: "#^Parameter \\#1 \\$name of static method pocketmine\\\\world\\\\generator\\\\GeneratorManager\\:\\:getGenerator\\(\\) expects string, string\\|null given\\.$#" + message: "#^Parameter \\#1 \\$name of method pocketmine\\\\world\\\\generator\\\\GeneratorManager\\:\\:getGenerator\\(\\) expects string, string\\|null given\\.$#" count: 1 path: ../../../src/Server.php diff --git a/tools/convert-world.php b/tools/convert-world.php index f4e376853..e524b1b6f 100644 --- a/tools/convert-world.php +++ b/tools/convert-world.php @@ -25,12 +25,9 @@ use pocketmine\world\format\io\FormatConverter; use pocketmine\world\format\io\WorldProvider; use pocketmine\world\format\io\WorldProviderManager; use pocketmine\world\format\io\WritableWorldProvider; -use pocketmine\world\generator\GeneratorManager; require_once dirname(__DIR__) . '/vendor/autoload.php'; -GeneratorManager::registerDefaultGenerators(); - $writableFormats = array_filter(WorldProviderManager::getInstance()->getAvailableProviders(), function(string $class){ return is_a($class, WritableWorldProvider::class, true); });