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