PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
FlatGeneratorOptions.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
29use function array_map;
30use function count;
31use function explode;
32use function preg_match;
33use function preg_match_all;
34
39
46 public function __construct(
47 private array $structure,
48 private int $biomeId,
49 private array $extraOptions = []
50 ){}
51
56 public function getStructure() : array{ return $this->structure; }
57
58 public function getBiomeId() : int{ return $this->biomeId; }
59
64 public function getExtraOptions() : array{ return $this->extraOptions; }
65
72 public static function parseLayers(string $layers) : array{
73 $result = [];
74 $split = array_map('\trim', explode(',', $layers));
75 $y = 0;
76 $itemParser = LegacyStringToItemParser::getInstance();
77 foreach($split as $line){
78 preg_match('#^(?:(\d+)[x|*])?(.+)$#', $line, $matches);
79 if(count($matches) !== 3){
80 throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\"");
81 }
82
83 $cnt = $matches[1] !== "" ? (int) $matches[1] : 1;
84 try{
85 $b = $itemParser->parse($matches[2])->getBlock();
86 }catch(LegacyStringToItemParserException $e){
87 throw new InvalidGeneratorOptionsException("Invalid preset layer \"$line\": " . $e->getMessage(), 0, $e);
88 }
89 for($cY = $y, $y += $cnt; $cY < $y; ++$cY){
90 $result[$cY] = $b->getStateId();
91 }
92 }
93
94 return $result;
95 }
96
100 public static function parsePreset(string $presetString) : self{
101 $preset = explode(";", $presetString);
102 $blocks = $preset[1] ?? "";
103 $biomeId = (int) ($preset[2] ?? BiomeIds::PLAINS);
104 $optionsString = $preset[3] ?? "";
105 $structure = self::parseLayers($blocks);
106
107 $options = [];
108 //TODO: more error checking
109 preg_match_all('#(([0-9a-z_]{1,})\‍(?([0-9a-z_ =:]{0,})\‍)?),?#', $optionsString, $matches);
110 foreach($matches[2] as $i => $option){
111 $params = true;
112 if($matches[3][$i] !== ""){
113 $params = [];
114 $p = explode(" ", $matches[3][$i]);
115 foreach($p as $k){
116 $k = explode("=", $k);
117 if(isset($k[1])){
118 $params[$k[0]] = $k[1];
119 }
120 }
121 }
122 $options[(string) $option] = $params;
123 }
124 return new self($structure, $biomeId, $options);
125 }
126
127}
__construct(private array $structure, private int $biomeId, private array $extraOptions=[])