PocketMine-MP 5.15.1 git-fb9a74e8799c71ed8292cfa53abe7a4c9204629d
WorldProviderManager.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
24namespace pocketmine\world\format\io;
25
31use function strtolower;
32use function trim;
33
39 protected array $providers = [];
40
41 private WritableWorldProviderManagerEntry $default;
42
43 public function __construct(){
44 $leveldb = new WritableWorldProviderManagerEntry(LevelDB::isValid(...), fn(string $path, \Logger $logger) => new LevelDB($path, $logger), LevelDB::generate(...));
45 $this->default = $leveldb;
46 $this->addProvider($leveldb, "leveldb");
47
48 $this->addProvider(new ReadOnlyWorldProviderManagerEntry(Anvil::isValid(...), fn(string $path, \Logger $logger) => new Anvil($path, $logger)), "anvil");
49 $this->addProvider(new ReadOnlyWorldProviderManagerEntry(McRegion::isValid(...), fn(string $path, \Logger $logger) => new McRegion($path, $logger)), "mcregion");
50 $this->addProvider(new ReadOnlyWorldProviderManagerEntry(PMAnvil::isValid(...), fn(string $path, \Logger $logger) => new PMAnvil($path, $logger)), "pmanvil");
51 }
52
57 return $this->default;
58 }
59
60 public function setDefault(WritableWorldProviderManagerEntry $class) : void{
61 $this->default = $class;
62 }
63
64 public function addProvider(WorldProviderManagerEntry $providerEntry, string $name, bool $overwrite = false) : void{
65 $name = strtolower($name);
66 if(!$overwrite && isset($this->providers[$name])){
67 throw new \InvalidArgumentException("Alias \"$name\" is already assigned");
68 }
69
70 $this->providers[$name] = $providerEntry;
71 }
72
79 public function getMatchingProviders(string $path) : array{
80 $result = [];
81 foreach(Utils::stringifyKeys($this->providers) as $alias => $providerEntry){
82 if($providerEntry->isValid($path)){
83 $result[$alias] = $providerEntry;
84 }
85 }
86 return $result;
87 }
88
93 public function getAvailableProviders() : array{
94 return $this->providers;
95 }
96
100 public function getProviderByName(string $name) : ?WorldProviderManagerEntry{
101 return $this->providers[trim(strtolower($name))] ?? null;
102 }
103}