GeneratorManager::getGenerator() now returns null for unknown generator aliases

instead of returning Normal::class (indistinguishable from successful match) or throwing an exception (pain in the ass to handle).
This commit is contained in:
Dylan K. Taylor 2021-10-11 16:04:36 +01:00
parent e62794e4cf
commit 7b6632941d
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
5 changed files with 15 additions and 20 deletions

View File

@ -106,6 +106,7 @@ use pocketmine\world\format\io\WorldProviderManager;
use pocketmine\world\format\io\WritableWorldProviderManagerEntry;
use pocketmine\world\generator\Generator;
use pocketmine\world\generator\GeneratorManager;
use pocketmine\world\generator\normal\Normal;
use pocketmine\world\World;
use pocketmine\world\WorldCreationOptions;
use pocketmine\world\WorldManager;
@ -979,7 +980,7 @@ class Server{
if(isset($options["generator"])){
$generatorOptions = explode(":", $options["generator"]);
$creationOptions->setGeneratorClass(GeneratorManager::getInstance()->getGenerator(array_shift($generatorOptions)));
$creationOptions->setGeneratorClass(GeneratorManager::getInstance()->getGenerator(array_shift($generatorOptions)) ?? Normal::class);
if(count($generatorOptions) > 0){
$creationOptions->setGeneratorOptions(implode(":", $generatorOptions));
}
@ -1010,7 +1011,7 @@ class Server{
}
if(!$this->worldManager->loadWorld($default, true)){
$creationOptions = WorldCreationOptions::create()
->setGeneratorClass(GeneratorManager::getInstance()->getGenerator($this->configGroup->getConfigString("level-type")))
->setGeneratorClass(GeneratorManager::getInstance()->getGenerator($this->configGroup->getConfigString("level-type")) ?? Normal::class)
->setGeneratorOptions($this->configGroup->getConfigString("generator-settings"));
$convertedSeed = Generator::convertSeed($this->configGroup->getConfigString("level-seed"));
if($convertedSeed !== null){

View File

@ -411,7 +411,8 @@ class World implements ChunkManager{
$this->maxY = $this->provider->getWorldMaxY();
$this->server->getLogger()->info($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_level_preparing($this->displayName)));
$this->generator = GeneratorManager::getInstance()->getGenerator($this->provider->getWorldData()->getGenerator(), true);
$this->generator = GeneratorManager::getInstance()->getGenerator($this->provider->getWorldData()->getGenerator()) ??
throw new AssumptionFailedError("WorldManager should already have checked that the generator exists");
//TODO: validate generator options
$this->chunkPopulationRequestQueue = new \SplQueue();
$this->addOnUnloadCallback(function() : void{

View File

@ -220,9 +220,8 @@ class WorldManager{
)));
return false;
}
try{
GeneratorManager::getInstance()->getGenerator($provider->getWorldData()->getGenerator(), true);
}catch(\InvalidArgumentException $e){
if(GeneratorManager::getInstance()->getGenerator($provider->getWorldData()->getGenerator()) === null){
$this->server->getLogger()->error($this->server->getLanguage()->translate(KnownTranslationFactory::pocketmine_level_loadError(
$name,
KnownTranslationFactory::pocketmine_level_unknownGenerator($provider->getWorldData()->getGenerator())

View File

@ -25,6 +25,7 @@ namespace pocketmine\world\format\io;
use pocketmine\utils\Filesystem;
use pocketmine\world\generator\GeneratorManager;
use pocketmine\world\generator\normal\Normal;
use pocketmine\world\WorldCreationOptions;
use Webmozart\PathUtil\Path;
use function basename;
@ -112,7 +113,9 @@ class FormatConverter{
Filesystem::recursiveUnlink($convertedOutput);
}
$this->newProvider->generate($convertedOutput, $data->getName(), WorldCreationOptions::create()
->setGeneratorClass(GeneratorManager::getInstance()->getGenerator($data->getGenerator()))
//TODO: defaulting to NORMAL here really isn't very good behaviour, but it's consistent with what we already
//did previously; besides, WorldManager checks for unknown generators before this is reached anyway.
->setGeneratorClass(GeneratorManager::getInstance()->getGenerator($data->getGenerator()) ?? Normal::class)
->setGeneratorOptions($data->getGeneratorOptions())
->setSeed($data->getSeed())
->setSpawnPosition($data->getSpawn())

View File

@ -79,20 +79,11 @@ final class GeneratorManager{
*
* @param bool $throwOnMissing @deprecated this is for backwards compatibility only
*
* @return string Name of class that extends Generator
* @phpstan-return class-string<Generator>
*
* @throws \InvalidArgumentException if the generator type isn't registered
* @return string|null Name of class that extends Generator, or null if no generator is mapped to that name
* @phpstan-return class-string<Generator>|null
*/
public function getGenerator(string $name, bool $throwOnMissing = false){
if(isset($this->list[$name = strtolower($name)])){
return $this->list[$name];
}
if($throwOnMissing){
throw new \InvalidArgumentException("Alias \"$name\" does not map to any known generator");
}
return Normal::class;
public function getGenerator(string $name, bool $throwOnMissing = false) : ?string{
return $this->list[strtolower($name)] ?? null;
}
/**