PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
GeneratorManager.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
25
26use pocketmine\utils\SingletonTrait;
30use function array_keys;
31use function strtolower;
32
33final class GeneratorManager{
34 use SingletonTrait;
35
40 private array $list = [];
41
42 public function __construct(){
43 $this->addGenerator(Flat::class, "flat", function(string $preset) : ?InvalidGeneratorOptionsException{
44 if($preset === ""){
45 return null;
46 }
47 try{
49 return null;
51 return $e;
52 }
53 });
54 $this->addGenerator(Normal::class, "normal", fn() => null);
55 $this->addAlias("normal", "default");
56 $this->addGenerator(Nether::class, "nether", fn() => null);
57 $this->addAlias("nether", "hell");
58 }
59
72 public function addGenerator(string $class, string $name, \Closure $presetValidator, bool $overwrite = false) : void{
73 Utils::testValidInstance($class, Generator::class);
74
75 $name = strtolower($name);
76 if(!$overwrite && isset($this->list[$name])){
77 throw new \InvalidArgumentException("Alias \"$name\" is already assigned");
78 }
79
80 $this->list[$name] = new GeneratorManagerEntry($class, $presetValidator);
81 }
82
87 public function addAlias(string $name, string $alias) : void{
88 $name = strtolower($name);
89 $alias = strtolower($alias);
90 if(!isset($this->list[$name])){
91 throw new \InvalidArgumentException("Alias \"$name\" is not assigned");
92 }
93 if(isset($this->list[$alias])){
94 throw new \InvalidArgumentException("Alias \"$alias\" is already assigned");
95 }
96 $this->list[$alias] = $this->list[$name];
97 }
98
104 public function getGeneratorList() : array{
105 return array_keys($this->list);
106 }
107
111 public function getGenerator(string $name) : ?GeneratorManagerEntry{
112 return $this->list[strtolower($name)] ?? null;
113 }
114
123 public function getGeneratorName(string $class) : string{
124 Utils::testValidInstance($class, Generator::class);
125 foreach(Utils::stringifyKeys($this->list) as $name => $c){
126 if($c->getGeneratorClass() === $class){
127 return $name;
128 }
129 }
130
131 throw new \InvalidArgumentException("Generator class $class is not registered");
132 }
133}
addGenerator(string $class, string $name, \Closure $presetValidator, bool $overwrite=false)