Loading [MathJax]/extensions/tex2jax.js
PocketMine-MP 5.25.3 git-afc4a3c7f18d42b41cbfde84ab6a2e4dd7c03045
All Classes Namespaces Functions Variables Enumerations Enumerator Pages
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
30use function array_map;
31use function explode;
32use function preg_match;
33use function preg_match_all;
34use const PHP_INT_MAX;
35
40
47 public function __construct(
48 private array $structure,
49 private int $biomeId,
50 private array $extraOptions = []
51 ){}
52
57 public function getStructure() : array{ return $this->structure; }
58
59 public function getBiomeId() : int{ return $this->biomeId; }
60
65 public function getExtraOptions() : array{ return $this->extraOptions; }
66
73 public static function parseLayers(string $layers) : array{
74 $result = [];
75 $split = array_map('\trim', explode(',', $layers, limit: World::Y_MAX - World::Y_MIN));
76 $y = 0;
77 $itemParser = LegacyStringToItemParser::getInstance();
78 foreach($split as $line){
79 if(preg_match('#^(?:(\d+)[x|*])?(.+)$#', $line, $matches) !== 1){
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, limit: 4);
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], limit: PHP_INT_MAX);
115 foreach($p as $k){
116 //TODO: this should be limited to 2 parts, but 3 preserves old behaviour when given e.g. treecount=20=1
117 $k = explode("=", $k, limit: 3);
118 if(isset($k[1])){
119 $params[$k[0]] = $k[1];
120 }
121 }
122 }
123 $options[$option] = $params;
124 }
125 return new self($structure, $biomeId, $options);
126 }
127
128}
__construct(private array $structure, private int $biomeId, private array $extraOptions=[])