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 $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;
}

View file

@ -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){

View file

@ -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;
}
}

View file

@ -1,10 +1,11 @@
<?php
class SmallFarmStructure{
public static $width = 7;
public static $lenght = 9;
public static $tmpStructure;
public static $structure = [
class SmallFarmStructure extends Structure{
public $width = 7;
public $lenght = 9;
public $name = "Small Farm";
private static $tmpStructure;
private static $structure = [
-1 => [
//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);
}
}

View file

@ -1,10 +1,11 @@
<?php
class SmallHouseStructure extends Structure{
public static $width = 5;
public static $lenght = 5;
public static $tmpStructure;
public static $structure = [
public $width = 5;
public $lenght = 5;
public $name = "Small House";
private static $tmpStructure;
private static $structure = [
0 => [
"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);
}

View file

@ -1,9 +1,10 @@
<?php
class VillageLibraryStructure extends Structure{
public static $width = 9;
public static $lenght = 8;
public static $structure = [
public $width = 9;
public $lenght = 8;
public $name = "Library";
private static $structure = [
0 => [
" 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);
}
}

View file

@ -1,9 +1,11 @@
<?php
class WoodHutStructure{
public static $width = 4;
public static $lenght = 6;
public static $structure = [
class WoodHutStructure extends Structure{
public $width = 4;
public $lenght = 6;
public $name = "Wood Hut";
private static $tmpStructure;
private static $structure = [
0 => [
" 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);
}
}