PocketMine-MP 5.43.3 git-4e2b0ce88fd28aa124b51efe324b94ceebb999fe
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
27use pocketmine\utils\SingletonTrait;
31use function array_keys;
32use function strtolower;
33
34final class GeneratorManager{
35 use SingletonTrait;
36
41 private array $list = [];
42
43 public function __construct(){
44 $this->addGenerator(Flat::class, "flat", function(string $preset) : ?InvalidGeneratorOptionsException{
45 if($preset === ""){
46 return null;
47 }
48 try{
50 return null;
52 return $e;
53 }
54 }, fast: true);
55 $this->addGenerator(Normal::class, "normal", fn() => null);
56 $this->addAlias("normal", "default");
57 $this->addGenerator(Nether::class, "nether", fn() => null);
58 $this->addAlias("nether", "hell");
59 $this->addGenerator(VoidGenerator::class, "void", fn() => null, fast: true, spawnPositionProvider: fn(int $seed) => new Vector3(0, -63, 0));
60 }
61
77 public function addGenerator(string $class, string $name, \Closure $presetValidator, bool $overwrite = false, bool $fast = false, ?\Closure $spawnPositionProvider = null) : void{
78 Utils::testValidInstance($class, Generator::class);
79
80 $name = strtolower($name);
81 if(!$overwrite && isset($this->list[$name])){
82 throw new \InvalidArgumentException("Alias \"$name\" is already assigned");
83 }
84
85 $this->list[$name] = new GeneratorManagerEntry($class, $presetValidator, $fast, $spawnPositionProvider);
86 }
87
92 public function addAlias(string $name, string $alias) : void{
93 $name = strtolower($name);
94 $alias = strtolower($alias);
95 if(!isset($this->list[$name])){
96 throw new \InvalidArgumentException("Alias \"$name\" is not assigned");
97 }
98 if(isset($this->list[$alias])){
99 throw new \InvalidArgumentException("Alias \"$alias\" is already assigned");
100 }
101 $this->list[$alias] = $this->list[$name];
102 }
103
109 public function getGeneratorList() : array{
110 return array_keys($this->list);
111 }
112
116 public function getGenerator(string $name) : ?GeneratorManagerEntry{
117 return $this->list[strtolower($name)] ?? null;
118 }
119
128 public function getGeneratorName(string $class) : string{
129 Utils::testValidInstance($class, Generator::class);
130 foreach(Utils::stringifyKeys($this->list) as $name => $c){
131 if($c->getGeneratorClass() === $class){
132 return $name;
133 }
134 }
135
136 throw new \InvalidArgumentException("Generator class $class is not registered");
137 }
138}
addGenerator(string $class, string $name, \Closure $presetValidator, bool $overwrite=false, bool $fast=false, ?\Closure $spawnPositionProvider=null)