add boilerplate code to check for generator validity

perhaps we should use an enum for this...?
This commit is contained in:
Dylan K. Taylor 2019-03-05 09:36:22 +00:00
parent 2cad7166b1
commit 2795ad674b
5 changed files with 13 additions and 0 deletions

View File

@ -25,6 +25,7 @@ namespace pocketmine\level\format\io\data;
use pocketmine\level\format\io\exception\UnsupportedLevelFormatException;
use pocketmine\level\generator\Flat;
use pocketmine\level\generator\Generator;
use pocketmine\level\generator\GeneratorManager;
use pocketmine\level\Level;
use pocketmine\nbt\LittleEndianNbtSerializer;
@ -36,6 +37,7 @@ use pocketmine\nbt\tag\LongTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\utils\Binary;
use pocketmine\utils\Utils;
use function file_get_contents;
use function file_put_contents;
use function strlen;
@ -51,6 +53,7 @@ class BedrockLevelData extends BaseNbtLevelData{
public const GENERATOR_FLAT = 2;
public static function generate(string $path, string $name, int $seed, string $generator, array $options = []) : void{
Utils::testValidInstance($generator, Generator::class);
switch($generator){
case Flat::class:
$generatorType = self::GENERATOR_FLAT;

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\level\format\io\data;
use pocketmine\level\generator\Generator;
use pocketmine\level\generator\GeneratorManager;
use pocketmine\level\Level;
use pocketmine\nbt\BigEndianNbtSerializer;
@ -32,6 +33,7 @@ use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\LongTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\utils\Utils;
use function ceil;
use function file_get_contents;
use function file_put_contents;
@ -40,6 +42,7 @@ use function microtime;
class JavaLevelData extends BaseNbtLevelData{
public static function generate(string $path, string $name, int $seed, string $generator, array $options = [], int $version = 19133) : void{
Utils::testValidInstance($generator, Generator::class);
//TODO, add extra details
$levelData = new CompoundTag("Data", [
new ByteTag("hardcore", ($options["hardcore"] ?? false) === true ? 1 : 0),

View File

@ -32,12 +32,14 @@ use pocketmine\level\format\io\exception\UnsupportedChunkFormatException;
use pocketmine\level\format\io\exception\UnsupportedLevelFormatException;
use pocketmine\level\format\io\LevelData;
use pocketmine\level\format\SubChunk;
use pocketmine\level\generator\Generator;
use pocketmine\nbt\LittleEndianNbtSerializer;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\utils\Binary;
use pocketmine\utils\BinaryDataException;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\Utils;
use function array_values;
use function chr;
use function defined;
@ -119,6 +121,7 @@ class LevelDB extends BaseLevelProvider{
}
public static function generate(string $path, string $name, int $seed, string $generator, array $options = []) : void{
Utils::testValidInstance($generator, Generator::class);
self::checkForLevelDBExtension();
if(!file_exists($path . "/db")){

View File

@ -28,7 +28,9 @@ use pocketmine\level\format\io\BaseLevelProvider;
use pocketmine\level\format\io\data\JavaLevelData;
use pocketmine\level\format\io\exception\CorruptedChunkException;
use pocketmine\level\format\io\LevelData;
use pocketmine\level\generator\Generator;
use pocketmine\level\Level;
use pocketmine\utils\Utils;
use function assert;
use function file_exists;
use function is_dir;
@ -69,6 +71,7 @@ abstract class RegionLevelProvider extends BaseLevelProvider{
}
public static function generate(string $path, string $name, int $seed, string $generator, array $options = []) : void{
Utils::testValidInstance($generator, Generator::class);
if(!file_exists($path)){
mkdir($path, 0777, true);
}

View File

@ -94,6 +94,7 @@ final class GeneratorManager{
* @throws \InvalidArgumentException if the class type cannot be matched to a known alias
*/
public static function getGeneratorName(string $class) : string{
Utils::testValidInstance($class, Generator::class);
foreach(self::$list as $name => $c){
if($c === $class){
return $name;