mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-11 12:27:51 +00:00
Flat: Move preset handling to a FlatGeneratorOptions unit
This commit is contained in:
parent
859cdfa5d2
commit
70deea0ef9
@ -39,32 +39,22 @@ use function preg_match;
|
|||||||
use function preg_match_all;
|
use function preg_match_all;
|
||||||
|
|
||||||
class Flat extends Generator{
|
class Flat extends Generator{
|
||||||
|
|
||||||
/** @var Chunk */
|
/** @var Chunk */
|
||||||
private $chunk;
|
private $chunk;
|
||||||
/** @var Populator[] */
|
/** @var Populator[] */
|
||||||
private $populators = [];
|
private $populators = [];
|
||||||
/**
|
|
||||||
* @var int[]
|
|
||||||
* @phpstan-var array<int, int>
|
|
||||||
*/
|
|
||||||
private $structure;
|
|
||||||
/** @var int */
|
|
||||||
private $biome;
|
|
||||||
|
|
||||||
/**
|
private FlatGeneratorOptions $options;
|
||||||
* @var mixed[]
|
|
||||||
* @phpstan-var array<string, mixed>
|
|
||||||
*/
|
|
||||||
private array $options = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws InvalidGeneratorOptionsException
|
* @throws InvalidGeneratorOptionsException
|
||||||
*/
|
*/
|
||||||
public function __construct(int $seed, string $preset){
|
public function __construct(int $seed, string $preset){
|
||||||
parent::__construct($seed, $preset !== "" ? $preset : "2;bedrock,2xdirt,grass;1;");
|
parent::__construct($seed, $preset !== "" ? $preset : "2;bedrock,2xdirt,grass;1;");
|
||||||
$this->parsePreset();
|
$this->options = FlatGeneratorOptions::parsePreset($this->preset);
|
||||||
|
|
||||||
if(isset($this->options["decoration"])){
|
if(isset($this->options->getExtraOptions()["decoration"])){
|
||||||
$ores = new Ore();
|
$ores = new Ore();
|
||||||
$stone = VanillaBlocks::STONE();
|
$stone = VanillaBlocks::STONE();
|
||||||
$ores->setOreTypes([
|
$ores->setOreTypes([
|
||||||
@ -83,76 +73,22 @@ class Flat extends Generator{
|
|||||||
$this->generateBaseChunk();
|
$this->generateBaseChunk();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int[]
|
|
||||||
* @phpstan-return array<int, int>
|
|
||||||
*
|
|
||||||
* @throws InvalidGeneratorOptionsException
|
|
||||||
*/
|
|
||||||
public static function parseLayers(string $layers) : array{
|
|
||||||
$result = [];
|
|
||||||
$split = array_map('\trim', explode(',', $layers));
|
|
||||||
$y = 0;
|
|
||||||
$itemParser = LegacyStringToItemParser::getInstance();
|
|
||||||
foreach($split as $line){
|
|
||||||
preg_match('#^(?:(\d+)[x|*])?(.+)$#', $line, $matches);
|
|
||||||
if(count($matches) !== 3){
|
|
||||||
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
|
|
||||||
try{
|
|
||||||
$b = $itemParser->parse($matches[2])->getBlock();
|
|
||||||
}catch(LegacyStringToItemParserException $e){
|
|
||||||
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
|
|
||||||
}
|
|
||||||
for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
|
|
||||||
$result[$cY] = $b->getFullId();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function parsePreset() : void{
|
|
||||||
$preset = explode(";", $this->preset);
|
|
||||||
$blocks = $preset[1] ?? "";
|
|
||||||
$this->biome = (int) ($preset[2] ?? 1);
|
|
||||||
$options = $preset[3] ?? "";
|
|
||||||
$this->structure = self::parseLayers($blocks);
|
|
||||||
|
|
||||||
//TODO: more error checking
|
|
||||||
preg_match_all('#(([0-9a-z_]{1,})\(?([0-9a-z_ =:]{0,})\)?),?#', $options, $matches);
|
|
||||||
foreach($matches[2] as $i => $option){
|
|
||||||
$params = true;
|
|
||||||
if($matches[3][$i] !== ""){
|
|
||||||
$params = [];
|
|
||||||
$p = explode(" ", $matches[3][$i]);
|
|
||||||
foreach($p as $k){
|
|
||||||
$k = explode("=", $k);
|
|
||||||
if(isset($k[1])){
|
|
||||||
$params[$k[0]] = $k[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->options[$option] = $params;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function generateBaseChunk() : void{
|
protected function generateBaseChunk() : void{
|
||||||
$this->chunk = new Chunk();
|
$this->chunk = new Chunk();
|
||||||
|
|
||||||
|
$biomeId = $this->options->getBiomeId();
|
||||||
for($Z = 0; $Z < Chunk::EDGE_LENGTH; ++$Z){
|
for($Z = 0; $Z < Chunk::EDGE_LENGTH; ++$Z){
|
||||||
for($X = 0; $X < Chunk::EDGE_LENGTH; ++$X){
|
for($X = 0; $X < Chunk::EDGE_LENGTH; ++$X){
|
||||||
$this->chunk->setBiomeId($X, $Z, $this->biome);
|
$this->chunk->setBiomeId($X, $Z, $biomeId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$count = count($this->structure);
|
$structure = $this->options->getStructure();
|
||||||
|
$count = count($structure);
|
||||||
for($sy = 0; $sy < $count; $sy += SubChunk::EDGE_LENGTH){
|
for($sy = 0; $sy < $count; $sy += SubChunk::EDGE_LENGTH){
|
||||||
$subchunk = $this->chunk->getSubChunk($sy >> SubChunk::COORD_BIT_SIZE);
|
$subchunk = $this->chunk->getSubChunk($sy >> SubChunk::COORD_BIT_SIZE);
|
||||||
for($y = 0; $y < SubChunk::EDGE_LENGTH and isset($this->structure[$y | $sy]); ++$y){
|
for($y = 0; $y < SubChunk::EDGE_LENGTH and isset($structure[$y | $sy]); ++$y){
|
||||||
$id = $this->structure[$y | $sy];
|
$id = $structure[$y | $sy];
|
||||||
|
|
||||||
for($Z = 0; $Z < SubChunk::EDGE_LENGTH; ++$Z){
|
for($Z = 0; $Z < SubChunk::EDGE_LENGTH; ++$Z){
|
||||||
for($X = 0; $X < SubChunk::EDGE_LENGTH; ++$X){
|
for($X = 0; $X < SubChunk::EDGE_LENGTH; ++$X){
|
||||||
|
127
src/world/generator/FlatGeneratorOptions.php
Normal file
127
src/world/generator/FlatGeneratorOptions.php
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\world\generator;
|
||||||
|
|
||||||
|
use pocketmine\item\LegacyStringToItemParser;
|
||||||
|
use pocketmine\item\LegacyStringToItemParserException;
|
||||||
|
use function array_map;
|
||||||
|
use function count;
|
||||||
|
use function explode;
|
||||||
|
use function preg_match;
|
||||||
|
use function preg_match_all;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class FlatGeneratorOptions{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int[] $structure
|
||||||
|
* @param mixed[] $extraOptions
|
||||||
|
* @phpstan-param array<int, int> $structure
|
||||||
|
* @phpstan-param array<string, array<string, string>|true> $extraOptions
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
private array $structure,
|
||||||
|
private int $biomeId,
|
||||||
|
private array $extraOptions = []
|
||||||
|
){}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int[]
|
||||||
|
* @phpstan-return array<int, int>
|
||||||
|
*/
|
||||||
|
public function getStructure() : array{ return $this->structure; }
|
||||||
|
|
||||||
|
public function getBiomeId() : int{ return $this->biomeId; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed[]
|
||||||
|
* @phpstan-return array<string, array<string, string>|true>
|
||||||
|
*/
|
||||||
|
public function getExtraOptions() : array{ return $this->extraOptions; }
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int[]
|
||||||
|
* @phpstan-return array<int, int>
|
||||||
|
*
|
||||||
|
* @throws InvalidGeneratorOptionsException
|
||||||
|
*/
|
||||||
|
public static function parseLayers(string $layers) : array{
|
||||||
|
$result = [];
|
||||||
|
$split = array_map('\trim', explode(',', $layers));
|
||||||
|
$y = 0;
|
||||||
|
$itemParser = LegacyStringToItemParser::getInstance();
|
||||||
|
foreach($split as $line){
|
||||||
|
preg_match('#^(?:(\d+)[x|*])?(.+)$#', $line, $matches);
|
||||||
|
if(count($matches) !== 3){
|
||||||
|
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
$cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
|
||||||
|
try{
|
||||||
|
$b = $itemParser->parse($matches[2])->getBlock();
|
||||||
|
}catch(LegacyStringToItemParserException $e){
|
||||||
|
throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
|
||||||
|
}
|
||||||
|
for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
|
||||||
|
$result[$cY] = $b->getFullId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws InvalidGeneratorOptionsException
|
||||||
|
*/
|
||||||
|
public static function parsePreset(string $presetString) : self{
|
||||||
|
$preset = explode(";", $presetString);
|
||||||
|
$blocks = $preset[1] ?? "";
|
||||||
|
$biomeId = (int) ($preset[2] ?? 1);
|
||||||
|
$optionsString = $preset[3] ?? "";
|
||||||
|
$structure = self::parseLayers($blocks);
|
||||||
|
|
||||||
|
$options = [];
|
||||||
|
//TODO: more error checking
|
||||||
|
preg_match_all('#(([0-9a-z_]{1,})\(?([0-9a-z_ =:]{0,})\)?),?#', $optionsString, $matches);
|
||||||
|
foreach($matches[2] as $i => $option){
|
||||||
|
$params = true;
|
||||||
|
if($matches[3][$i] !== ""){
|
||||||
|
$params = [];
|
||||||
|
$p = explode(" ", $matches[3][$i]);
|
||||||
|
foreach($p as $k){
|
||||||
|
$k = explode("=", $k);
|
||||||
|
if(isset($k[1])){
|
||||||
|
$params[$k[0]] = $k[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$options[(string) $option] = $params;
|
||||||
|
}
|
||||||
|
return new self($structure, $biomeId, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user