Structure.php now is not static

This commit is contained in:
ArkQuark 2023-03-03 19:43:08 +03:00
parent 1c606f3e5e
commit 546ca07d95
7 changed files with 155 additions and 176 deletions

View file

@ -171,7 +171,6 @@ abstract class Block extends Position{
public $z = 0; public $z = 0;
public $slipperiness; public $slipperiness;
public function __construct($id, $meta = 0, $name = "Unknown"){ public function __construct($id, $meta = 0, $name = "Unknown"){
$this->id = (int) $id; $this->id = (int) $id;
$this->meta = (int) $meta; $this->meta = (int) $meta;
@ -193,9 +192,11 @@ abstract class Block extends Position{
final public function getID(){ final public function getID(){
return $this->id; return $this->id;
} }
public function setMetadata($i){ public function setMetadata($i){
$this->meta = $i; $this->meta = $i;
} }
final public function getMetadata(){ final public function getMetadata(){
return $this->meta & 0x0F; return $this->meta & 0x0F;
} }

View file

@ -15,7 +15,7 @@ class MyceliumBlock extends SolidBlock{
} }
public function onActivate(Item $item, Player $player){ //uwu 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){ public function getDrops(Item $item, Player $player){

View file

@ -9,53 +9,57 @@ abstract class Structure{
const MAP_NO_KEY = -255; const MAP_NO_KEY = -255;
const LEVEL_RSV1 = 1; const LEVEL_RSV1 = 1;
private static $structure, $tmpStructure; private static $structure, $tmpStructure;
public $api, $pm; private $map;
public static $map, $width, $lenght; 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->pm = ServerAPI::request();
$this->api = $this->pm->api; $this->api = $this->pm->api;
self::$width = $width; $this->width = $width;
self::$lenght = $lenght; $this->lenght = $lenght;
self::$map = $charToBlock; $this->name = $name;
/*foreach($charToBlock as $char => $array){ $this->map = $map;
$this->addMapping($char, , isset($array) ? $array[1] : 0);
}*/
} }
public function addMapping($char, $blockClass, $meta){ public function getName(){
self::$map[$char] = new $blockClass($meta); return $this->name;
} }
public static function getMappingFor($char){ public function setName($name){
return isset(self::$map[$char]) ? self::$map[$char] : Structure::MAP_NO_KEY; $this->name = $name;
} }
protected static function placeBlock($level, $char, &$vector){ 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 function placeBlock($level, $char, &$vector){
if($char == "") return; if($char == "") return;
if($level instanceof Level){ if($level instanceof Level){
$blockClass = is_array(self::$map[$char]) ? self::$map[$char][0] : self::$map[$char]; $block = $this->getMappingFor($char);
$block = new $blockClass(isset(self::$map[$char]) ? self::$map[$char][1] : 0, isset(self::$map[$char][2]) ? self::$map[$char][2] : 0);
//var_dump($block); //var_dump($block);
/*if($block === Structure::MAP_NO_KEY){ if($block === Structure::MAP_NO_KEY){
//console("Failed to receive id"); //console("Failed to get block!");
return false; return false;
}*/ }
$level->setBlock($vector, $block, true, false, true); $level->setBlockRaw($vector, $block);
} }
} }
public static function build($level, $centerX, $centerY, $centerZ, $structure){ public function build($level, $centerX, $centerY, $centerZ, $structure = 0){
//console("b"); //console("building ".$this->name);
$offsetX = 0; $offsetX = 0;
$offsetZ = 0; $offsetZ = 0;
foreach($structure as $offsetY => $blocksXZ){ foreach($structure as $offsetY => $blocksXZ){
foreach($blocksXZ as $blocks){ foreach($blocksXZ as $blocks){
$blocks = rtrim($blocks); $blocks = rtrim($blocks);
foreach(str_split($blocks) as $block){ 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); //$tempVector->setXYZ($x, $centerY + $offsetY, $z);
self::placeBlock($level, $block, $vector); $this->placeBlock($level, $block, $vector);
++$offsetX; ++$offsetX;
} }
++$offsetZ; ++$offsetZ;
@ -63,6 +67,8 @@ abstract class Structure{
} }
$offsetZ = 0; $offsetZ = 0;
} }
//console("builded!");
return true;
} }
} }

View file

@ -1,10 +1,11 @@
<?php <?php
class SmallFarmStructure{ class SmallFarmStructure extends Structure{
public static $width = 7; public $width = 7;
public static $lenght = 9; public $lenght = 9;
public static $tmpStructure; public $name = "Small Farm";
public static $structure = [ private static $tmpStructure;
private static $structure = [
-1 => [ -1 => [
//add sand, clay if here isn't air //add sand, clay if here isn't air
], ],
@ -20,11 +21,22 @@ class SmallFarmStructure{
"WWWWWWW", "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; self::$tmpStructure = self::$structure;
$f = Utils::randomFloat(); $f = Utils::randomFloat();
if($f <= 0.5){ if($f <= 0.5){
parent::setName("Wheat ".$this->name);
self::$tmpStructure[1] = [ self::$tmpStructure[1] = [
"", "",
" HH HH ", " HH HH ",
@ -38,6 +50,7 @@ class SmallFarmStructure{
]; ];
} }
elseif($f <= 0.7){ elseif($f <= 0.7){
parent::setName("Carrot ".$this->name);
self::$tmpStructure[1] = [ self::$tmpStructure[1] = [
"", "",
" CC CC ", " CC CC ",
@ -51,6 +64,7 @@ class SmallFarmStructure{
]; ];
} }
elseif($f <= 0.9){ elseif($f <= 0.9){
parent::setName("Potato ".$this->name);
self::$tmpStructure[1] = [ self::$tmpStructure[1] = [
"", "",
" PP PP ", " PP PP ",
@ -64,6 +78,7 @@ class SmallFarmStructure{
]; ];
} }
else{ else{
parent::setName("Beetroot ".$this->name);
self::$tmpStructure[1] = [ self::$tmpStructure[1] = [
"", "",
" BB BB ", " BB BB ",
@ -78,53 +93,13 @@ class SmallFarmStructure{
} }
} }
public static function buildStructure($level, $x, $y, $z){ /*use CENTER positions*/ public function __construct(){
self::generateCrops(); parent::__construct($this->width, $this->lenght, $this->name, $this->map);
}
$offsetX = 0; public function build($level, $x, $y, $z, $structure = 0){
$offsetZ = 0; $this->generateCrops();
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": parent::build($level, $x, $y, $z, self::$tmpStructure);
$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;
}
} }
} }

View file

@ -1,10 +1,11 @@
<?php <?php
class SmallHouseStructure extends Structure{ class SmallHouseStructure extends Structure{
public static $width = 5; public $width = 5;
public static $lenght = 5; public $lenght = 5;
public static $tmpStructure; public $name = "Small House";
public static $structure = [ private static $tmpStructure;
private static $structure = [
0 => [ 0 => [
"CCCCC", "CCCCC",
"CCCCC", "CCCCC",
@ -41,8 +42,7 @@ class SmallHouseStructure extends Structure{
"WWWWW", "WWWWW",
], ],
]; ];
private $map = [
public static $map = [
"C" => "CobbleStoneBlock", "C" => "CobbleStoneBlock",
"P" => "PlanksBlock", "P" => "PlanksBlock",
"W" => "WoodBlock", "W" => "WoodBlock",
@ -53,13 +53,14 @@ class SmallHouseStructure extends Structure{
" " => "AirBlock" " " => "AirBlock"
]; ];
public function __construct($width = 0, $lenght = 0, $charToBlock = []){ public function __construct(){
parent::__construct(self::$width, self::$lenght, self::$map); parent::__construct($this->width, $this->lenght, $this->name, $this->map);
} }
public static function generateFence(){ private function generateFence(){
self::$tmpStructure = self::$structure; self::$tmpStructure = self::$structure;
if(Utils::chance(50)){ if(Utils::chance(50)){
parent::setName($this->name." with Fence");
self::$tmpStructure[1][3] = "P L P"; self::$tmpStructure[1][3] = "P L P";
self::$tmpStructure[2][3] = "P L P"; self::$tmpStructure[2][3] = "P L P";
self::$tmpStructure[3][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 = []){ public function build($level, $x, $y, $z, $structure = 0){
self::generateFence(); $this->generateFence();
parent::build($level, $x, $y, $z, self::$tmpStructure); parent::build($level, $x, $y, $z, self::$tmpStructure);
} }

View file

@ -1,9 +1,10 @@
<?php <?php
class VillageLibraryStructure extends Structure{ class VillageLibraryStructure extends Structure{
public static $width = 9; public $width = 9;
public static $lenght = 8; public $lenght = 8;
public static $structure = [ public $name = "Library";
private static $structure = [
0 => [ 0 => [
" S ", " S ",
"CCCCCCCCC", "CCCCCCCCC",
@ -95,14 +96,13 @@ class VillageLibraryStructure extends Structure{
"", "",
] ]
]; ];
private $map = [
public static $map = [
"S" => ["CobbleStoneStairsBlock", 2], "S" => ["CobbleStoneStairsBlock", 2],
"C" => "CobbleStoneBlock", "C" => "CobbleStoneBlock",
"P" => "PlanksBlock", "P" => "PlanksBlock",
"W" => "WoodBlock", "W" => "WoodBlock",
"d" => ["DoorBlock", 64], "d" => ["DoorBlock", 64, 0x01],
"8" => ["DoorBlock", 64, 0x08], "8" => ["DoorBlock", 64, 0x09],
"G" => "GlassPaneBlock", "G" => "GlassPaneBlock",
"B" => "BookshelfBlock", "B" => "BookshelfBlock",
"F" => "FenceBlock", "F" => "FenceBlock",
@ -114,11 +114,11 @@ class VillageLibraryStructure extends Structure{
" " => "AirBlock" " " => "AirBlock"
]; ];
public function __construct($width = 0, $lenght = 0, $charToBlock = []){ public function __construct(){
parent::__construct(self::$width, self::$lenght, self::$map); 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); parent::build($level, $x, $y, $z, self::$structure);
} }
} }

View file

@ -1,9 +1,11 @@
<?php <?php
class WoodHutStructure{ class WoodHutStructure extends Structure{
public static $width = 4; public $width = 4;
public static $lenght = 6; public $lenght = 6;
public static $structure = [ public $name = "Wood Hut";
private static $tmpStructure;
private static $structure = [
0 => [ 0 => [
" S ", " S ",
"CCCC", "CCCC",
@ -17,15 +19,15 @@ class WoodHutStructure{
"WdPW", "WdPW",
"P P", "P P",
"P P", "P P",
"P FP", "P P",
"WPPW", "WPPW",
], ],
2 => [ 2 => [
"", "",
"WdPW", "W8PW",
"P P", "P P",
"G G", "G G",
"P cP", "P P",
"WPPW", "WPPW",
], ],
3 => [ 3 => [
@ -36,75 +38,69 @@ class WoodHutStructure{
"P P", "P P",
"WPPW", "WPPW",
], ],
4 => [ ];
"", private $map = [
" WW ", "S" => ["CobbleStoneStairsBlock", 2],
"W W", "C" => "CobbleStoneBlock",
"W W", "P" => "PlanksBlock",
"W W", "W" => "WoodBlock",
" WW ", "D" => "DirtBlock",
], "d" => ["DoorBlock", 64, 0x01],
5 => [ "8" => ["DoorBlock", 64, 0x09],
"", "G" => "GlassPaneBlock",
"", "F" => "FenceBlock",
" WW ", "c" => ["CarpetBlock", 12],
" WW ", " " => "AirBlock"
" WW ",
"",
]
]; ];
public static function buildStructure($level, $x, $y, $z){ /*use CENTER positions*/ public function generateRoof(){
$offsetX = 0; self::$tmpStructure = self::$structure;
$offsetZ = 0; if(Utils::chance(50)){
foreach(self::$structure as $layerCount => $layer){ self::$tmpStructure[4] = [
foreach($layer as $line){ "",
$line = rtrim($line); //remove useless spaces(only from right) " WW ",
foreach(str_split($line) as $char){ "W W",
$vector = new Vector3($x - floor(self::$width / 2) + $offsetX, $y + $layerCount, $z + $offsetZ); "W W",
switch($char){ "W W",
case "S": " WW ",
$level->setBlockRaw($vector, new CobbleStoneStairsBlock(2)); ];
break; self::$tmpStructure[5] = [
case "C": "",
$level->setBlockRaw($vector, new CobbleStoneBlock()); "",
break; " WW ",
case "P": " WW ",
$level->setBlockRaw($vector, new PlanksBlock()); " WW ",
break; "",
case "W": ];
$level->setBlockRaw($vector, new WoodBlock()); }
break; else{
case "D": self::$tmpStructure[4] = [
$level->setBlockRaw($vector, new DirtBlock()); "",
break; " WW ",
case "d": "WWWW",
if($layerCount == 1) $level->setBlockRaw($vector, new DoorBlock(64)); "WWWW",
else $level->setBlockRaw($vector, new DoorBlock(64, 0x08)); "WWWW",
break; " WW ",
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 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);
}
} }