From 96a62953fa2c7a504d745597835f69ec421fed8a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 4 Mar 2023 17:32:15 +0300 Subject: [PATCH] Structure.php rewrite, all structures(except smallfarm) are probably broken now --- src/PocketMinecraftServer.php | 1 + src/material/block/solid/MyceliumBlock.php | 6 + src/structure/Structure.php | 117 ++++++++------- .../stronghold/StrongholdLibraryStructure.php | 11 +- .../StrongholdPortalRoomStructure.php | 13 +- src/structure/village/SmallFarmStructure.php | 134 +++++++----------- src/structure/village/SmallHouseStructure.php | 17 +-- .../village/VillageLibraryStructure.php | 13 +- src/structure/village/WoodHutStructure.php | 15 +- 9 files changed, 141 insertions(+), 186 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 102738d3..1ceaee07 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -32,6 +32,7 @@ class PocketMinecraftServer{ console("[INFO] Starting Minecraft PE server on " . ($this->serverip === "0.0.0.0" ? "*" : $this->serverip) . ":" . $this->port); EntityRegistry::registerEntities(); + Structures::initialize(); define("BOOTUP_RANDOM", Utils::getRandomBytes(16)); $this->serverID = $this->serverID === false ? Utils::readLong(substr(Utils::getUniqueID(true, $this->serverip . $this->port), 8)) : $this->serverID; $this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : $this->seed; diff --git a/src/material/block/solid/MyceliumBlock.php b/src/material/block/solid/MyceliumBlock.php index 93433128..1831c4d7 100644 --- a/src/material/block/solid/MyceliumBlock.php +++ b/src/material/block/solid/MyceliumBlock.php @@ -15,6 +15,12 @@ class MyceliumBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ //uwu + /*if(mt_rand(0, 1) === 0){ + Structures::$SMALLFARM_VILLAGE->rotate90deg($this->level, $this->x, $this->y, $this->z)->build($this->level, $this->x, $this->y, $this->z); + }else{ + Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z); + }*/ + //(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ()); } diff --git a/src/structure/Structure.php b/src/structure/Structure.php index d1264081..6a7bdd8f 100644 --- a/src/structure/Structure.php +++ b/src/structure/Structure.php @@ -1,77 +1,74 @@ x+ | -↓z+ +|z+ */ abstract class Structure{ - const MAP_NO_KEY = -255; - const LEVEL_RSV1 = 1; - private static $structure, $tmpStructure; - private $map; - public $api, $pm, $width, $length, $name; + const MAP_NO_KEY = -127; + protected $structure; + protected $map; + public $width, $length; + public $name; - public function __construct($width = 0, $length = 0, $name = "Unknown", $map = []){ - $this->pm = ServerAPI::request(); - $this->api = $this->pm->api; - $this->width = $width; - $this->length = $length; - $this->name = $name; - $this->map = $map; - } - - public function getName(){ - return $this->name; - } - - public function setName($name){ - $this->name = $name; - } - - public function parseMeta($meta){ - if(!is_numeric($meta)){ - $arrmeta = explode(" ", $meta); - if($arrmeta[0] === "random"){ - $meta = mt_rand($arrmeta[1], $arrmeta[2]); - }else{ - $meta = 0; //undefined - } - } - return $meta; - } - - protected function getMappingFor($char){ - if(!isset($this->map[$char])) return Structure::MAP_NO_KEY; - $blockClass = is_array($this->map[$char]) ? $this->map[$char][0] : $this->map[$char]; - return new $blockClass(isset($this->map[$char][1]) ? $this->parseMeta($this->map[$char][1]) : 0, isset($this->map[$char][2]) ? $this->map[$char][2] : 0); - } - - protected function placeBlock($level, $char, &$vector){ - if($char == "") return; - if($level instanceof Level){ - $block = $this->getMappingFor($char); - //var_dump($block); - if($block === Structure::MAP_NO_KEY){ - //console("Failed to get block!"); - return false; + public function __construct(){ + foreach($this->structure as &$s){ + foreach($s as &$e){ + if(strlen($e) < $this->length){ + $e .= str_repeat(" ", $this->length - strlen($e)); + } } - $level->setBlockRaw($vector, $block); } } - public function build($level, $centerX, $centerY, $centerZ, $structure = 0){ - //console("building ".$this->name); - $offsetX = 0; - $offsetZ = 0; - foreach($structure as $offsetY => $blocksXZ){ + protected function getMappingFor($char){ + return nullsafe($this->map[$char], self::MAP_NO_KEY); + } + + protected function placeBlock(Level $level, $char, $vector){ + if($char === "" || (($mk = $this->getMappingFor($char)) === self::MAP_NO_KEY)) return; + $id = is_array($mk) ? $mk[0] : $mk; + $meta = is_array($mk) ? $mk[1] : 0; + return $level->setBlock($vector, BlockAPI::get($id, $meta)); + } + + protected function init(){} + + protected function getFinalStructure(Level $level, $x, $y, $z){ + return $this->structure; + } + public function rotate90deg(Level $level, $x, $y, $z){ + $str = $this->getFinalStructure($level, $x, $y, $z); + foreach($str as &$arr){ + foreach($arr as &$e){ + $e = str_split($e); + } + $new = []; + for($i = 0; $i < $this->length; ++$i){ + $new[] = implode("", array_reverse(array_column($arr, $i))); + } + $arr = $new; + } + return (clone $this)->setStructure($str, $this->length, $this->width); + } + private function setStructure($struct, $width, $length){ + $this->structure = $struct; + $this->width = $width; + $this->length = $length; + console("Modifed"); + return $this; + } + public function build(Level $level, $centerX, $centerY, $centerZ){ + $offsetX = $offsetZ = 0; + $centWidth = floor($this->width / 2); + $centLength = floor($this->length / 2); + foreach($this->getFinalStructure($level, $centerX, $centerY, $centerZ) as $offsetY => $blocksXZ){ foreach($blocksXZ as $blocks){ $blocks = rtrim($blocks); foreach(str_split($blocks) as $block){ if($centerY + $offsetY == 128) return false; - $vector = new Vector3($centerX - floor($this->width / 2) + $offsetX, $centerY + $offsetY, $centerZ + $offsetZ); - //$tempVector->setXYZ($x, $centerY + $offsetY, $z); - $this->placeBlock($level, $block, $vector); + $this->placeBlock($level, $block, new Vector3($centerX - $centWidth + $offsetX, $centerY + $offsetY, $centerZ - $centLength + $offsetZ)); ++$offsetX; } ++$offsetZ; @@ -79,8 +76,6 @@ abstract class Structure{ } $offsetZ = 0; } - //console("builded!"); - return true; } } \ No newline at end of file diff --git a/src/structure/stronghold/StrongholdLibraryStructure.php b/src/structure/stronghold/StrongholdLibraryStructure.php index 2fcc0def..f89e796b 100644 --- a/src/structure/stronghold/StrongholdLibraryStructure.php +++ b/src/structure/stronghold/StrongholdLibraryStructure.php @@ -1,10 +1,9 @@ [ "SSSSSSSSSSSSSS", "SSSSSSSSSSSSSS", @@ -194,7 +193,7 @@ class StrongholdLibraryStructure{ ] ]; - public static function replaceStoneBricks(){ + public function replaceStoneBricks(){ foreach(self::$structure as $layerInt => $layer){ foreach($layer as $key => $str){ $line = str_split($str); @@ -214,7 +213,7 @@ class StrongholdLibraryStructure{ } } - public static function placeCobweb(){ + public function placeCobweb(){ foreach(self::$tmpStructure as $layerInt => $layer){ if($layerInt >= 0 and $layerInt <= 3){ foreach($layer as $key => $str){ diff --git a/src/structure/stronghold/StrongholdPortalRoomStructure.php b/src/structure/stronghold/StrongholdPortalRoomStructure.php index a4f72efa..b5e9775b 100644 --- a/src/structure/stronghold/StrongholdPortalRoomStructure.php +++ b/src/structure/stronghold/StrongholdPortalRoomStructure.php @@ -2,10 +2,9 @@ class StrongholdPortalRoomStructure extends Structure{ public $width = 11; - public $length = 16; - public $name = "Portal Room"; - private static $tmpStructure; - private static $structure = [ + public $length = 16; + public $name = "Portal Room"; + protected $structure = [ -1 => [ "SSSSSSSSSSS", "SSSSSSSSSSS", @@ -133,7 +132,7 @@ class StrongholdPortalRoomStructure extends Structure{ "SSSSSSSSSSS", ] ]; - private $map = [ + protected $map = [ "S" => "StoneBricksBlock", "M" => ["StoneBricksBlock", 1], "C" => ["StoneBricksBlock", 2], @@ -195,10 +194,10 @@ class StrongholdPortalRoomStructure extends Structure{ parent::__construct($this->width, $this->length, $this->name, $this->map); } - public function build($level, $x, $y, $z, $structure = 0){ + /*public function build($level, $x, $y, $z, $structure = 0){ $this->replaceStoneBricks(); $this->placeEyes(); parent::build($level, $x, $y, $z, self::$tmpStructure); - } + }*/ // TODO } \ No newline at end of file diff --git a/src/structure/village/SmallFarmStructure.php b/src/structure/village/SmallFarmStructure.php index 3730a90b..f9d74084 100644 --- a/src/structure/village/SmallFarmStructure.php +++ b/src/structure/village/SmallFarmStructure.php @@ -1,14 +1,10 @@ [ - //add sand, clay if here isn't air - ], + protected $structure = [ 0 => [ "WWWWWWW", "WFFwFFW", @@ -19,87 +15,55 @@ class SmallFarmStructure extends Structure{ "WFFwFFW", "WFFwFFW", "WWWWWWW", - ] + ], + 1 => [ + "", + " RR RR ", + " RR RR ", + " RR RR ", + " RR RR ", + " RR RR ", + " RR RR ", + " RR RR ", + "", + ], ]; - private $map = [ - "W" => "WoodBlock", - "F" => "FarmlandBlock", - "w" => "WaterBlock", - "H" => ["WheatBlock", 0], - "C" => ["CarrotBlock", 0], - "P" => ["PotatoBlock", 0], - "B" => ["BeetrootBlock", 0], - " " => "AirBlock" + + protected $map = [ + "W" => WOOD, + "F" => FARMLAND, + "w" => WATER, + " " => 0, + "D" => DIRT ]; - - private function generateCrops(){ - self::$tmpStructure = self::$structure; - $f = Utils::randomFloat(); - if($f <= 0.5){ - parent::setName("Wheat ".$this->name); - self::$tmpStructure[1] = [ - "", - " HH HH ", - " HH HH ", - " HH HH ", - " HH HH ", - " HH HH ", - " HH HH ", - " HH HH ", - "", - ]; - } - elseif($f <= 0.7){ - parent::setName("Carrot ".$this->name); - self::$tmpStructure[1] = [ - "", - " CC CC ", - " CC CC ", - " CC CC ", - " CC CC ", - " CC CC ", - " CC CC ", - " CC CC ", - "", - ]; - } - elseif($f <= 0.9){ - parent::setName("Potato ".$this->name); - self::$tmpStructure[1] = [ - "", - " PP PP ", - " PP PP ", - " PP PP ", - " PP PP ", - " PP PP ", - " PP PP ", - " PP PP ", - "", - ]; - } - else{ - parent::setName("Beetroot ".$this->name); - self::$tmpStructure[1] = [ - "", - " BB BB ", - " BB BB ", - " BB BB ", - " BB BB ", - " BB BB ", - " BB BB ", - " BB BB ", - "", - ]; + protected function getFinalStructure(Level $level, $x, $y, $z) + { + if(!$level->getBlockWithoutVector($x, $y - 1, $z)->isSolid){ + $structCopy = $this->structure; + $structCopy[-1] = array_fill(0, $this->length, str_repeat("D", $this->width)); //TODO sand or clay, check for the whole row of water? + return $structCopy; } + return parent::getFinalStructure($level, $x, $y, $z); } - - public function __construct(){ - parent::__construct($this->width, $this->length, $this->name, $this->map); - } - - public function build($level, $x, $y, $z, $structure = 0){ - $this->generateCrops(); - - parent::build($level, $x, $y, $z, self::$tmpStructure); + + + protected function getMappingFor($char){ + if($char === "R"){ + $f = Utils::randomFloat(); + + if($f <= 0.5){ + return [WHEAT_BLOCK, 0]; + } + if($f <= 0.7){ + return [CARROT_BLOCK, 0]; + } + if($f <= 0.9){ + return [POTATO_BLOCK, 0]; + } + return [BEETROOT_BLOCK, 0]; + } + return parent::getMappingFor($char); } + + } \ No newline at end of file diff --git a/src/structure/village/SmallHouseStructure.php b/src/structure/village/SmallHouseStructure.php index 2e7502a5..822af498 100644 --- a/src/structure/village/SmallHouseStructure.php +++ b/src/structure/village/SmallHouseStructure.php @@ -1,11 +1,10 @@ [ "CCCCC", "CCCCC", @@ -42,7 +41,7 @@ class SmallHouseStructure extends Structure{ "WWWWW", ], ]; - private $map = [ + protected $map = [ "C" => "CobbleStoneBlock", "P" => "PlanksBlock", "W" => "WoodBlock", @@ -53,10 +52,6 @@ class SmallHouseStructure extends Structure{ " " => "AirBlock" ]; - public function __construct(){ - parent::__construct($this->width, $this->length, $this->name, $this->map); - } - private function generateFence(){ self::$tmpStructure = self::$structure; if(Utils::chance(50)){ @@ -75,9 +70,9 @@ class SmallHouseStructure extends Structure{ } } - public function build($level, $x, $y, $z, $structure = 0){ + /*public function build($level, $x, $y, $z, $structure = 0){ $this->generateFence(); parent::build($level, $x, $y, $z, self::$tmpStructure); - } + }*/ //TODO } \ No newline at end of file diff --git a/src/structure/village/VillageLibraryStructure.php b/src/structure/village/VillageLibraryStructure.php index 2cd123ef..edab3644 100644 --- a/src/structure/village/VillageLibraryStructure.php +++ b/src/structure/village/VillageLibraryStructure.php @@ -1,10 +1,10 @@ [ " S ", "CCCCCCCCC", @@ -96,7 +96,7 @@ class VillageLibraryStructure extends Structure{ "", ] ]; - private $map = [ + protected $map = [ "S" => ["CobbleStoneStairsBlock", 2], "C" => "CobbleStoneBlock", "P" => "PlanksBlock", @@ -115,10 +115,11 @@ class VillageLibraryStructure extends Structure{ ]; public function __construct(){ - parent::__construct($this->width, $this->length, $this->name, $this->map); + parent::__construct(); } - + /* public function build($level, $x, $y, $z, $structure = 0){ parent::build($level, $x, $y, $z, self::$structure); } + */ } \ No newline at end of file diff --git a/src/structure/village/WoodHutStructure.php b/src/structure/village/WoodHutStructure.php index 450af06b..b69d438d 100644 --- a/src/structure/village/WoodHutStructure.php +++ b/src/structure/village/WoodHutStructure.php @@ -1,11 +1,10 @@ [ " S ", "CCCC", @@ -39,7 +38,7 @@ class WoodHutStructure extends Structure{ "WPPW", ], ]; - private $map = [ + protected $map = [ "S" => ["CobbleStoneStairsBlock", 2], "C" => "CobbleStoneBlock", "P" => "PlanksBlock", @@ -93,14 +92,10 @@ class WoodHutStructure extends Structure{ } } - public function __construct(){ - parent::__construct($this->width, $this->length, $this->name, $this->map); - } - - public function build($level, $x, $y, $z, $structure = 0){ + /*public function build($level, $x, $y, $z, $structure = 0){ $this->generateRoof(); $this->generateTable(); parent::build($level, $x, $y, $z, self::$tmpStructure); - } + }*/ //TODO } \ No newline at end of file