classname mapping * @phpstan-var array> */ private $list = []; 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"); } /** * @param string $class Fully qualified name of class that extends \pocketmine\world\generator\Generator * @param string $name Alias for this generator type that can be written in configs * @param bool $overwrite Whether to force overwriting any existing registered generator with the same name * @phpstan-param class-string $class * * @throws \InvalidArgumentException */ public function addGenerator(string $class, string $name, bool $overwrite = false) : void{ Utils::testValidInstance($class, Generator::class); if(!$overwrite and isset($this->list[$name = strtolower($name)])){ throw new \InvalidArgumentException("Alias \"$name\" is already assigned"); } $this->list[$name] = $class; } /** * Returns a list of names for registered generators. * * @return string[] */ public function getGeneratorList() : array{ return array_keys($this->list); } /** * Returns a class name of a registered Generator matching the given name. * * @param bool $throwOnMissing @deprecated this is for backwards compatibility only * * @return string Name of class that extends Generator * @phpstan-return class-string * * @throws \InvalidArgumentException if the generator type isn't registered */ 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; } /** * Returns the registered name of the given Generator class. * * @param string $class Fully qualified name of class that extends \pocketmine\world\generator\Generator * @phpstan-param class-string $class * * @throws \InvalidArgumentException if the class type cannot be matched to a known alias */ public function getGeneratorName(string $class) : string{ Utils::testValidInstance($class, Generator::class); foreach($this->list as $name => $c){ if($c === $class){ return $name; } } throw new \InvalidArgumentException("Generator class $class is not registered"); } }