From 546ca07d95ac12abc0d7dffeee88a379a274ee12 Mon Sep 17 00:00:00 2001 From: ArkQuark <74979909+ArkQuark@users.noreply.github.com> Date: Fri, 3 Mar 2023 19:43:08 +0300 Subject: [PATCH] Structure.php now is not static --- src/material/Block.php | 3 +- src/material/block/solid/MyceliumBlock.php | 2 +- src/structure/Structure.php | 58 +++---- src/structure/village/SmallFarmStructure.php | 81 ++++------ src/structure/village/SmallHouseStructure.php | 23 +-- .../village/VillageLibraryStructure.php | 20 +-- src/structure/village/WoodHutStructure.php | 144 +++++++++--------- 7 files changed, 155 insertions(+), 176 deletions(-) diff --git a/src/material/Block.php b/src/material/Block.php index 8617d822..e9a7b3bc 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -171,7 +171,6 @@ abstract class Block extends Position{ public $z = 0; public $slipperiness; - public function __construct($id, $meta = 0, $name = "Unknown"){ $this->id = (int) $id; $this->meta = (int) $meta; @@ -193,9 +192,11 @@ abstract class Block extends Position{ final public function getID(){ return $this->id; } + public function setMetadata($i){ $this->meta = $i; } + final public function getMetadata(){ return $this->meta & 0x0F; } diff --git a/src/material/block/solid/MyceliumBlock.php b/src/material/block/solid/MyceliumBlock.php index 05384769..93433128 100644 --- a/src/material/block/solid/MyceliumBlock.php +++ b/src/material/block/solid/MyceliumBlock.php @@ -15,7 +15,7 @@ class MyceliumBlock extends SolidBlock{ } public function onActivate(Item $item, Player $player){ //uwu - //(new VillageLibraryStructure())::build($this->level, $this->getX(), $this->getY(), $this->getZ()); + //(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ()); } public function getDrops(Item $item, Player $player){ diff --git a/src/structure/Structure.php b/src/structure/Structure.php index bf5e3971..b6e67316 100644 --- a/src/structure/Structure.php +++ b/src/structure/Structure.php @@ -9,53 +9,57 @@ abstract class Structure{ const MAP_NO_KEY = -255; const LEVEL_RSV1 = 1; private static $structure, $tmpStructure; - public $api, $pm; - public static $map, $width, $lenght; + private $map; + public $api, $pm, $width, $lenght, $name; - public function __construct($width, $lenght, $charToBlock = []){ + public function __construct($width = 0, $lenght = 0, $name = "Unknown", $map = []){ $this->pm = ServerAPI::request(); $this->api = $this->pm->api; - self::$width = $width; - self::$lenght = $lenght; - self::$map = $charToBlock; - /*foreach($charToBlock as $char => $array){ - $this->addMapping($char, , isset($array) ? $array[1] : 0); - }*/ - } - - public function addMapping($char, $blockClass, $meta){ - self::$map[$char] = new $blockClass($meta); + $this->width = $width; + $this->lenght = $lenght; + $this->name = $name; + $this->map = $map; } - public static function getMappingFor($char){ - return isset(self::$map[$char]) ? self::$map[$char] : Structure::MAP_NO_KEY; + public function getName(){ + return $this->name; + } + + public function setName($name){ + $this->name = $name; + } + + protected function getMappingFor($char){ + if(!isset($this->map[$char])) return MAP_NO_KEY; + $blockClass = is_array($this->map[$char]) ? $this->map[$char][0] : $this->map[$char]; + return new $blockClass(isset($this->map[$char]) ? $this->map[$char][1] : 0, isset($this->map[$char][2]) ? $this->map[$char][2] : 0); } - protected static function placeBlock($level, $char, &$vector){ + protected function placeBlock($level, $char, &$vector){ if($char == "") return; if($level instanceof Level){ - $blockClass = is_array(self::$map[$char]) ? self::$map[$char][0] : self::$map[$char]; - $block = new $blockClass(isset(self::$map[$char]) ? self::$map[$char][1] : 0, isset(self::$map[$char][2]) ? self::$map[$char][2] : 0); + $block = $this->getMappingFor($char); //var_dump($block); - /*if($block === Structure::MAP_NO_KEY){ - //console("Failed to receive id"); + if($block === Structure::MAP_NO_KEY){ + //console("Failed to get block!"); return false; - }*/ - $level->setBlock($vector, $block, true, false, true); + } + $level->setBlockRaw($vector, $block); } } - public static function build($level, $centerX, $centerY, $centerZ, $structure){ - //console("b"); + public function build($level, $centerX, $centerY, $centerZ, $structure = 0){ + //console("building ".$this->name); $offsetX = 0; $offsetZ = 0; foreach($structure as $offsetY => $blocksXZ){ foreach($blocksXZ as $blocks){ $blocks = rtrim($blocks); foreach(str_split($blocks) as $block){ - $vector = new Vector3($centerX - floor(self::$width / 2) + $offsetX, $centerY + $offsetY, $centerZ + $offsetZ); + 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); - self::placeBlock($level, $block, $vector); + $this->placeBlock($level, $block, $vector); ++$offsetX; } ++$offsetZ; @@ -63,6 +67,8 @@ abstract class Structure{ } $offsetZ = 0; } + //console("builded!"); + return true; } } \ No newline at end of file diff --git a/src/structure/village/SmallFarmStructure.php b/src/structure/village/SmallFarmStructure.php index 36271ad9..5a87db5d 100644 --- a/src/structure/village/SmallFarmStructure.php +++ b/src/structure/village/SmallFarmStructure.php @@ -1,10 +1,11 @@ [ //add sand, clay if here isn't air ], @@ -20,11 +21,22 @@ class SmallFarmStructure{ "WWWWWWW", ] ]; + private $map = [ + "W" => "WoodBlock", + "F" => "FarmlandBlock", + "w" => "WaterBlock", + "H" => ["WheatBlock", 0], + "C" => ["CarrotBlock", 0], + "P" => ["PotatoBlock", 0], + "B" => ["BeetrootBlock", 0], + " " => "AirBlock" + ]; - public static function generateCrops(){ + private function generateCrops(){ self::$tmpStructure = self::$structure; $f = Utils::randomFloat(); if($f <= 0.5){ + parent::setName("Wheat ".$this->name); self::$tmpStructure[1] = [ "", " HH HH ", @@ -38,6 +50,7 @@ class SmallFarmStructure{ ]; } elseif($f <= 0.7){ + parent::setName("Carrot ".$this->name); self::$tmpStructure[1] = [ "", " CC CC ", @@ -51,6 +64,7 @@ class SmallFarmStructure{ ]; } elseif($f <= 0.9){ + parent::setName("Potato ".$this->name); self::$tmpStructure[1] = [ "", " PP PP ", @@ -64,6 +78,7 @@ class SmallFarmStructure{ ]; } else{ + parent::setName("Beetroot ".$this->name); self::$tmpStructure[1] = [ "", " BB BB ", @@ -78,53 +93,13 @@ class SmallFarmStructure{ } } - public static function buildStructure($level, $x, $y, $z){ /*use CENTER positions*/ - self::generateCrops(); + public function __construct(){ + parent::__construct($this->width, $this->lenght, $this->name, $this->map); + } - $offsetX = 0; - $offsetZ = 0; - foreach(self::$tmpStructure as $layerCount => $layer){ - foreach($layer as $line){ - $line = rtrim($line); //remove useless spaces(only from right) - foreach(str_split($line) as $char){ - $vector = new Vector3($x - floor(self::$width / 2) + $offsetX, $y + $layerCount, $z + $offsetZ); - switch($char){ - case "W": - $level->setBlockRaw($vector, new WoodBlock()); - break; - case "F": - $level->setBlockRaw($vector, new FarmlandBlock()); - break; - case "w": - $level->setBlockRaw($vector, new WaterBlock()); - break; - - case "H": - $level->setBlockRaw($vector, new WheatBlock()); - break; - case "C": - $level->setBlockRaw($vector, new CarrotBlock()); - break; - case "P": - $level->setBlockRaw($vector, new PotatoBlock()); - break; - case "B": - $level->setBlockRaw($vector, new BeetrootBlock()); - break; - case " ": - $block = $level->getBlock($vector)->getID(); - if($block === AIR){ - break; - } - $level->setBlockRaw($vector, new AirBlock()); - break; - } - ++$offsetX; - } - ++$offsetZ; - $offsetX = 0; - } - $offsetZ = 0; - } + public function build($level, $x, $y, $z, $structure = 0){ + $this->generateCrops(); + + parent::build($level, $x, $y, $z, self::$tmpStructure); } } \ No newline at end of file diff --git a/src/structure/village/SmallHouseStructure.php b/src/structure/village/SmallHouseStructure.php index 1a49b356..ce869577 100644 --- a/src/structure/village/SmallHouseStructure.php +++ b/src/structure/village/SmallHouseStructure.php @@ -1,10 +1,11 @@ [ "CCCCC", "CCCCC", @@ -41,8 +42,7 @@ class SmallHouseStructure extends Structure{ "WWWWW", ], ]; - - public static $map = [ + private $map = [ "C" => "CobbleStoneBlock", "P" => "PlanksBlock", "W" => "WoodBlock", @@ -53,13 +53,14 @@ class SmallHouseStructure extends Structure{ " " => "AirBlock" ]; - public function __construct($width = 0, $lenght = 0, $charToBlock = []){ - parent::__construct(self::$width, self::$lenght, self::$map); + public function __construct(){ + parent::__construct($this->width, $this->lenght, $this->name, $this->map); } - public static function generateFence(){ + private function generateFence(){ self::$tmpStructure = self::$structure; if(Utils::chance(50)){ + parent::setName($this->name." with Fence"); self::$tmpStructure[1][3] = "P L P"; self::$tmpStructure[2][3] = "P L P"; self::$tmpStructure[3][3] = "P L P"; @@ -74,8 +75,8 @@ class SmallHouseStructure extends Structure{ } } - public static function build($level, $x, $y, $z, $structure = []){ - self::generateFence(); + public function build($level, $x, $y, $z, $structure = 0){ + $this->generateFence(); parent::build($level, $x, $y, $z, self::$tmpStructure); } diff --git a/src/structure/village/VillageLibraryStructure.php b/src/structure/village/VillageLibraryStructure.php index 2e80f947..47f1c49c 100644 --- a/src/structure/village/VillageLibraryStructure.php +++ b/src/structure/village/VillageLibraryStructure.php @@ -1,9 +1,10 @@ [ " S ", "CCCCCCCCC", @@ -95,14 +96,13 @@ class VillageLibraryStructure extends Structure{ "", ] ]; - - public static $map = [ + private $map = [ "S" => ["CobbleStoneStairsBlock", 2], "C" => "CobbleStoneBlock", "P" => "PlanksBlock", "W" => "WoodBlock", - "d" => ["DoorBlock", 64], - "8" => ["DoorBlock", 64, 0x08], + "d" => ["DoorBlock", 64, 0x01], + "8" => ["DoorBlock", 64, 0x09], "G" => "GlassPaneBlock", "B" => "BookshelfBlock", "F" => "FenceBlock", @@ -114,11 +114,11 @@ class VillageLibraryStructure extends Structure{ " " => "AirBlock" ]; - public function __construct($width = 0, $lenght = 0, $charToBlock = []){ - parent::__construct(self::$width, self::$lenght, self::$map); + public function __construct(){ + parent::__construct($this->width, $this->lenght, $this->name, $this->map); } - public static function build($level, $x, $y, $z, $structure = 0){ + 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 235fe752..07e18b64 100644 --- a/src/structure/village/WoodHutStructure.php +++ b/src/structure/village/WoodHutStructure.php @@ -1,9 +1,11 @@ [ " S ", "CCCC", @@ -17,15 +19,15 @@ class WoodHutStructure{ "WdPW", "P P", "P P", - "P FP", + "P P", "WPPW", ], 2 => [ "", - "WdPW", + "W8PW", "P P", "G G", - "P cP", + "P P", "WPPW", ], 3 => [ @@ -36,75 +38,69 @@ class WoodHutStructure{ "P P", "WPPW", ], - 4 => [ - "", - " WW ", - "W W", - "W W", - "W W", - " WW ", - ], - 5 => [ - "", - "", - " WW ", - " WW ", - " WW ", - "", - ] + ]; + private $map = [ + "S" => ["CobbleStoneStairsBlock", 2], + "C" => "CobbleStoneBlock", + "P" => "PlanksBlock", + "W" => "WoodBlock", + "D" => "DirtBlock", + "d" => ["DoorBlock", 64, 0x01], + "8" => ["DoorBlock", 64, 0x09], + "G" => "GlassPaneBlock", + "F" => "FenceBlock", + "c" => ["CarpetBlock", 12], + " " => "AirBlock" ]; - public static function buildStructure($level, $x, $y, $z){ /*use CENTER positions*/ - $offsetX = 0; - $offsetZ = 0; - foreach(self::$structure as $layerCount => $layer){ - foreach($layer as $line){ - $line = rtrim($line); //remove useless spaces(only from right) - foreach(str_split($line) as $char){ - $vector = new Vector3($x - floor(self::$width / 2) + $offsetX, $y + $layerCount, $z + $offsetZ); - switch($char){ - case "S": - $level->setBlockRaw($vector, new CobbleStoneStairsBlock(2)); - break; - case "C": - $level->setBlockRaw($vector, new CobbleStoneBlock()); - break; - case "P": - $level->setBlockRaw($vector, new PlanksBlock()); - break; - case "W": - $level->setBlockRaw($vector, new WoodBlock()); - break; - case "D": - $level->setBlockRaw($vector, new DirtBlock()); - break; - case "d": - if($layerCount == 1) $level->setBlockRaw($vector, new DoorBlock(64)); - else $level->setBlockRaw($vector, new DoorBlock(64, 0x08)); - break; - case "G": - $level->setBlockRaw($vector, new GlassPaneBlock()); - break; - case "F": - $level->setBlockRaw($vector, new FenceBlock()); - break; - case "c": - $level->setBlockRaw($vector, new CarpetBlock(12)); - break; - case " ": - $block = $level->getBlock($vector)->getID(); - if($block === AIR){ - break; - } - $level->setBlockRaw($vector, new AirBlock()); - break; - } - ++$offsetX; - } - ++$offsetZ; - $offsetX = 0; - } - $offsetZ = 0; + public function generateRoof(){ + self::$tmpStructure = self::$structure; + if(Utils::chance(50)){ + self::$tmpStructure[4] = [ + "", + " WW ", + "W W", + "W W", + "W W", + " WW ", + ]; + self::$tmpStructure[5] = [ + "", + "", + " WW ", + " WW ", + " WW ", + "", + ]; + } + else{ + self::$tmpStructure[4] = [ + "", + " WW ", + "WWWW", + "WWWW", + "WWWW", + " WW ", + ]; } } + + public function generateTable(){ + if(Utils::chance(66)){ + parent::setName($this->name." with Table"); + self::$tmpStructure[1][4] = "P FP"; + self::$tmpStructure[2][4] = "P cP"; + } + } + + public function __construct(){ + parent::__construct($this->width, $this->lenght, $this->name, $this->map); + } + + public function build($level, $x, $y, $z, $structure = 0){ + $this->generateRoof(); + $this->generateTable(); + + parent::build($level, $x, $y, $z, self::$tmpStructure); + } } \ No newline at end of file