Convert GeneratorManager to singleton

This commit is contained in:
Dylan K. Taylor 2020-05-23 10:13:03 +01:00
parent 640428c415
commit c9af5ce7a9
9 changed files with 28 additions and 36 deletions

View File

@ -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")]
);
}

View File

@ -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;

View File

@ -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;

View File

@ -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()

View File

@ -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();

View File

@ -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());

View File

@ -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<string, class-string<Generator>>
*/
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
}
}

View File

@ -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

View File

@ -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);
});