mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-20 16:00:20 +00:00
Remove LevelProvider::getProviderName()
This is problematic because child level providers can forget to override the provider name of their parents, and then override them by error. Instead, they should be used in a mapping fashion to make sure they are unique and not inherited. Also, the old method did not permit registering multiple aliases for the same provider. This now makes that possible.
This commit is contained in:
parent
65b49dec71
commit
f787552e97
@ -33,13 +33,6 @@ interface LevelProvider{
|
||||
*/
|
||||
public function __construct(string $path);
|
||||
|
||||
/**
|
||||
* Returns the full provider name, like "anvil" or "mcregion", will be used to find the correct format.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getProviderName() : string;
|
||||
|
||||
/**
|
||||
* Gets the build height limit of this world
|
||||
*
|
||||
|
@ -36,10 +36,10 @@ abstract class LevelProviderManager{
|
||||
private static $default = PMAnvil::class;
|
||||
|
||||
public static function init() : void{
|
||||
self::addProvider(Anvil::class);
|
||||
self::addProvider(McRegion::class);
|
||||
self::addProvider(PMAnvil::class);
|
||||
self::addProvider(LevelDB::class);
|
||||
self::addProvider(Anvil::class, "anvil");
|
||||
self::addProvider(McRegion::class, "mcregion");
|
||||
self::addProvider(PMAnvil::class, "pmanvil");
|
||||
self::addProvider(LevelDB::class, "leveldb");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -61,20 +61,25 @@ abstract class LevelProviderManager{
|
||||
public static function setDefault(string $class) : void{
|
||||
Utils::testValidInstance($class, LevelProvider::class);
|
||||
|
||||
self::addProvider($class);
|
||||
self::$default = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @param string $name
|
||||
* @param bool $overwrite
|
||||
*/
|
||||
public static function addProvider(string $class){
|
||||
public static function addProvider(string $class, string $name, bool $overwrite = false) : void{
|
||||
Utils::testValidInstance($class, LevelProvider::class);
|
||||
|
||||
$name = strtolower($name);
|
||||
if(!$overwrite and isset(self::$providers[$name])){
|
||||
throw new \InvalidArgumentException("Alias \"$name\" is already assigned");
|
||||
}
|
||||
|
||||
/** @var LevelProvider $class */
|
||||
self::$providers[strtolower($class::getProviderName())] = $class;
|
||||
self::$providers[$name] = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -153,10 +153,6 @@ class LevelDB extends BaseLevelProvider{
|
||||
$db->close();
|
||||
}
|
||||
|
||||
public static function getProviderName() : string{
|
||||
return "leveldb";
|
||||
}
|
||||
|
||||
public function getWorldHeight() : int{
|
||||
return 256;
|
||||
}
|
||||
|
@ -49,10 +49,6 @@ class Anvil extends RegionLevelProvider{
|
||||
);
|
||||
}
|
||||
|
||||
public static function getProviderName() : string{
|
||||
return "anvil";
|
||||
}
|
||||
|
||||
protected static function getRegionFileExtension() : string{
|
||||
return "mca";
|
||||
}
|
||||
|
@ -172,10 +172,6 @@ class McRegion extends RegionLevelProvider{
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function getProviderName() : string{
|
||||
return "mcregion";
|
||||
}
|
||||
|
||||
protected static function getRegionFileExtension() : string{
|
||||
return "mcr";
|
||||
}
|
||||
|
@ -52,10 +52,6 @@ class PMAnvil extends RegionLevelProvider{
|
||||
);
|
||||
}
|
||||
|
||||
public static function getProviderName() : string{
|
||||
return "pmanvil";
|
||||
}
|
||||
|
||||
protected static function getRegionFileExtension() : string{
|
||||
return "mcapm";
|
||||
}
|
||||
|
@ -30,24 +30,24 @@ class LevelProviderManagerTest extends TestCase{
|
||||
public function testAddNonClassProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
LevelProviderManager::addProvider("lol");
|
||||
LevelProviderManager::addProvider("lol", "nope");
|
||||
}
|
||||
|
||||
public function testAddAbstractClassProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
LevelProviderManager::addProvider(AbstractLevelProvider::class);
|
||||
LevelProviderManager::addProvider(AbstractLevelProvider::class, "abstract");
|
||||
}
|
||||
|
||||
public function testAddInterfaceProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
LevelProviderManager::addProvider(InterfaceLevelProvider::class);
|
||||
LevelProviderManager::addProvider(InterfaceLevelProvider::class, "interface");
|
||||
}
|
||||
|
||||
public function testAddWrongClassProvider() : void{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
|
||||
LevelProviderManager::addProvider(LevelProviderManagerTest::class);
|
||||
LevelProviderManager::addProvider(LevelProviderManagerTest::class, "bad_class");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user