mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-07 04:17:07 +00:00
Convert GeneratorManager to singleton
This commit is contained in:
parent
640428c415
commit
c9af5ce7a9
@ -1017,7 +1017,6 @@ class Server{
|
|||||||
$this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName]));
|
$this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName]));
|
||||||
}
|
}
|
||||||
|
|
||||||
GeneratorManager::registerDefaultGenerators();
|
|
||||||
$this->worldManager = new WorldManager($this, $this->dataPath . "/worlds");
|
$this->worldManager = new WorldManager($this, $this->dataPath . "/worlds");
|
||||||
$this->worldManager->setAutoSave($this->getConfigBool("auto-save", $this->worldManager->getAutoSave()));
|
$this->worldManager->setAutoSave($this->getConfigBool("auto-save", $this->worldManager->getAutoSave()));
|
||||||
$this->worldManager->setAutoSaveInterval((int) $this->getProperty("ticks-per.autosave", 6000));
|
$this->worldManager->setAutoSaveInterval((int) $this->getProperty("ticks-per.autosave", 6000));
|
||||||
@ -1040,7 +1039,7 @@ class Server{
|
|||||||
if(!$this->worldManager->loadWorld($name, true)){
|
if(!$this->worldManager->loadWorld($name, true)){
|
||||||
if(isset($options["generator"])){
|
if(isset($options["generator"])){
|
||||||
$generatorOptions = explode(":", $options["generator"]);
|
$generatorOptions = explode(":", $options["generator"]);
|
||||||
$generator = GeneratorManager::getGenerator(array_shift($generatorOptions));
|
$generator = GeneratorManager::getInstance()->getGenerator(array_shift($generatorOptions));
|
||||||
if(count($options) > 0){
|
if(count($options) > 0){
|
||||||
$options["preset"] = implode(":", $generatorOptions);
|
$options["preset"] = implode(":", $generatorOptions);
|
||||||
}
|
}
|
||||||
@ -1063,7 +1062,7 @@ class Server{
|
|||||||
$this->worldManager->generateWorld(
|
$this->worldManager->generateWorld(
|
||||||
$default,
|
$default,
|
||||||
Generator::convertSeed($this->getConfigString("level-seed")),
|
Generator::convertSeed($this->getConfigString("level-seed")),
|
||||||
GeneratorManager::getGenerator($this->getConfigString("level-type")),
|
GeneratorManager::getInstance()->getGenerator($this->getConfigString("level-type")),
|
||||||
["preset" => $this->getConfigString("generator-settings")]
|
["preset" => $this->getConfigString("generator-settings")]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -335,7 +335,7 @@ class World implements ChunkManager{
|
|||||||
$this->worldHeight = $this->provider->getWorldHeight();
|
$this->worldHeight = $this->provider->getWorldHeight();
|
||||||
|
|
||||||
$this->server->getLogger()->info($this->server->getLanguage()->translateString("pocketmine.level.preparing", [$this->displayName]));
|
$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
|
//TODO: validate generator options
|
||||||
|
|
||||||
$this->folderName = $name;
|
$this->folderName = $name;
|
||||||
|
@ -205,7 +205,7 @@ class WorldManager{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try{
|
try{
|
||||||
GeneratorManager::getGenerator($provider->getWorldData()->getGenerator(), true);
|
GeneratorManager::getInstance()->getGenerator($provider->getWorldData()->getGenerator(), true);
|
||||||
}catch(\InvalidArgumentException $e){
|
}catch(\InvalidArgumentException $e){
|
||||||
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [$name, "Unknown generator \"" . $provider->getWorldData()->getGenerator() . "\""]));
|
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [$name, "Unknown generator \"" . $provider->getWorldData()->getGenerator() . "\""]));
|
||||||
return false;
|
return false;
|
||||||
|
@ -101,7 +101,7 @@ class FormatConverter{
|
|||||||
$this->logger->info("Found previous conversion attempt, deleting...");
|
$this->logger->info("Found previous conversion attempt, deleting...");
|
||||||
Filesystem::recursiveUnlink($convertedOutput);
|
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()
|
* @see WritableWorldProvider::__construct()
|
||||||
|
@ -102,7 +102,7 @@ class BedrockWorldData extends BaseNbtWorldData{
|
|||||||
//Additional PocketMine-MP fields
|
//Additional PocketMine-MP fields
|
||||||
->setTag("GameRules", new CompoundTag())
|
->setTag("GameRules", new CompoundTag())
|
||||||
->setByte("hardcore", ($options["hardcore"] ?? false) === true ? 1 : 0)
|
->setByte("hardcore", ($options["hardcore"] ?? false) === true ? 1 : 0)
|
||||||
->setString("generatorName", GeneratorManager::getGeneratorName($generator))
|
->setString("generatorName", GeneratorManager::getInstance()->getGeneratorName($generator))
|
||||||
->setString("generatorOptions", $options["preset"] ?? "");
|
->setString("generatorOptions", $options["preset"] ?? "");
|
||||||
|
|
||||||
$nbt = new LittleEndianNbtSerializer();
|
$nbt = new LittleEndianNbtSerializer();
|
||||||
|
@ -64,7 +64,7 @@ class JavaWorldData extends BaseNbtWorldData{
|
|||||||
->setLong("RandomSeed", $seed)
|
->setLong("RandomSeed", $seed)
|
||||||
->setLong("SizeOnDisk", 0)
|
->setLong("SizeOnDisk", 0)
|
||||||
->setLong("Time", 0)
|
->setLong("Time", 0)
|
||||||
->setString("generatorName", GeneratorManager::getGeneratorName($generator))
|
->setString("generatorName", GeneratorManager::getInstance()->getGeneratorName($generator))
|
||||||
->setString("generatorOptions", $options["preset"] ?? "")
|
->setString("generatorOptions", $options["preset"] ?? "")
|
||||||
->setString("LevelName", $name)
|
->setString("LevelName", $name)
|
||||||
->setTag("GameRules", new CompoundTag());
|
->setTag("GameRules", new CompoundTag());
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\world\generator;
|
namespace pocketmine\world\generator;
|
||||||
|
|
||||||
|
use pocketmine\utils\SingletonTrait;
|
||||||
use pocketmine\utils\Utils;
|
use pocketmine\utils\Utils;
|
||||||
use pocketmine\world\generator\hell\Nether;
|
use pocketmine\world\generator\hell\Nether;
|
||||||
use pocketmine\world\generator\normal\Normal;
|
use pocketmine\world\generator\normal\Normal;
|
||||||
@ -30,21 +31,20 @@ use function array_keys;
|
|||||||
use function strtolower;
|
use function strtolower;
|
||||||
|
|
||||||
final class GeneratorManager{
|
final class GeneratorManager{
|
||||||
|
use SingletonTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string[] name => classname mapping
|
* @var string[] name => classname mapping
|
||||||
* @phpstan-var array<string, class-string<Generator>>
|
* @phpstan-var array<string, class-string<Generator>>
|
||||||
*/
|
*/
|
||||||
private static $list = [];
|
private $list = [];
|
||||||
|
|
||||||
/**
|
public function __construct(){
|
||||||
* Registers the default known generators.
|
$this->addGenerator(Flat::class, "flat");
|
||||||
*/
|
$this->addGenerator(Normal::class, "normal");
|
||||||
public static function registerDefaultGenerators() : void{
|
$this->addGenerator(Normal::class, "default");
|
||||||
self::addGenerator(Flat::class, "flat");
|
$this->addGenerator(Nether::class, "hell");
|
||||||
self::addGenerator(Normal::class, "normal");
|
$this->addGenerator(Nether::class, "nether");
|
||||||
self::addGenerator(Normal::class, "default");
|
|
||||||
self::addGenerator(Nether::class, "hell");
|
|
||||||
self::addGenerator(Nether::class, "nether");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,14 +55,14 @@ final class GeneratorManager{
|
|||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @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);
|
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");
|
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[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public static function getGeneratorList() : array{
|
public function getGeneratorList() : array{
|
||||||
return array_keys(self::$list);
|
return array_keys($this->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -84,9 +84,9 @@ final class GeneratorManager{
|
|||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException if the generator type isn't registered
|
* @throws \InvalidArgumentException if the generator type isn't registered
|
||||||
*/
|
*/
|
||||||
public static function getGenerator(string $name, bool $throwOnMissing = false){
|
public function getGenerator(string $name, bool $throwOnMissing = false){
|
||||||
if(isset(self::$list[$name = strtolower($name)])){
|
if(isset($this->list[$name = strtolower($name)])){
|
||||||
return self::$list[$name];
|
return $this->list[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
if($throwOnMissing){
|
if($throwOnMissing){
|
||||||
@ -103,9 +103,9 @@ final class GeneratorManager{
|
|||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException if the class type cannot be matched to a known alias
|
* @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);
|
Utils::testValidInstance($class, Generator::class);
|
||||||
foreach(self::$list as $name => $c){
|
foreach($this->list as $name => $c){
|
||||||
if($c === $class){
|
if($c === $class){
|
||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
@ -113,8 +113,4 @@ final class GeneratorManager{
|
|||||||
|
|
||||||
throw new \InvalidArgumentException("Generator class $class is not registered");
|
throw new \InvalidArgumentException("Generator class $class is not registered");
|
||||||
}
|
}
|
||||||
|
|
||||||
private function __construct(){
|
|
||||||
//NOOP
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ parameters:
|
|||||||
path: ../../../build/server-phar.php
|
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
|
count: 1
|
||||||
path: ../../../src/Server.php
|
path: ../../../src/Server.php
|
||||||
|
|
||||||
|
@ -25,12 +25,9 @@ use pocketmine\world\format\io\FormatConverter;
|
|||||||
use pocketmine\world\format\io\WorldProvider;
|
use pocketmine\world\format\io\WorldProvider;
|
||||||
use pocketmine\world\format\io\WorldProviderManager;
|
use pocketmine\world\format\io\WorldProviderManager;
|
||||||
use pocketmine\world\format\io\WritableWorldProvider;
|
use pocketmine\world\format\io\WritableWorldProvider;
|
||||||
use pocketmine\world\generator\GeneratorManager;
|
|
||||||
|
|
||||||
require_once dirname(__DIR__) . '/vendor/autoload.php';
|
require_once dirname(__DIR__) . '/vendor/autoload.php';
|
||||||
|
|
||||||
GeneratorManager::registerDefaultGenerators();
|
|
||||||
|
|
||||||
$writableFormats = array_filter(WorldProviderManager::getInstance()->getAvailableProviders(), function(string $class){
|
$writableFormats = array_filter(WorldProviderManager::getInstance()->getAvailableProviders(), function(string $class){
|
||||||
return is_a($class, WritableWorldProvider::class, true);
|
return is_a($class, WritableWorldProvider::class, true);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user