diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 6d3dacd0..b0beab38 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -338,7 +338,7 @@ class BlockAPI{ return $this->cancelAction($block, $player); } if($hand->getID() === SIGN_POST or $hand->getID() === WALL_SIGN){ - $t = $this->server->api->tileentity->addSign($player->level, $block->x, $block->y, $block->z); + $t = $this->server->api->tile->addSign($player->level, $block->x, $block->y, $block->z); $t->data["creator"] = $player->username; } diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index 4605e7a1..40eb95ac 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -136,7 +136,7 @@ class LevelAPI{ $entity->close(); } } - foreach($this->server->api->tileentity->getAll($level) as $tile){ + foreach($this->server->api->tile->getAll($level) as $tile){ $tile->close(); } unset($this->levels[$name]); @@ -154,8 +154,8 @@ class LevelAPI{ console("[INFO] Preparing level \"".$name."\""); $level = new PMFLevel($path."level.pmf"); $entities = new Config($path."entities.yml", CONFIG_YAML); - $tileEntities = new Config($path."tileEntities.yml", CONFIG_YAML); - $this->levels[$name] = new Level($level, $entities, $tileEntities, $name); + $tiles = new Config($path."tiles.yml", CONFIG_YAML); + $this->levels[$name] = new Level($level, $entities, $tiles, $name); foreach($entities->getAll() as $entity){ if(!isset($entity["id"])){ break; @@ -185,11 +185,11 @@ class LevelAPI{ } } - foreach($tileEntities->getAll() as $tile){ + foreach($tiles->getAll() as $tile){ if(!isset($tile["id"])){ break; } - $t = $this->server->api->tileentity->add($this->levels[$name], $tile["id"], $tile["x"], $tile["y"], $tile["z"], $tile); + $t = $this->server->api->tile->add($this->levels[$name], $tile["id"], $tile["x"], $tile["y"], $tile["z"], $tile); } } diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 4a75303a..6bc37fe7 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -153,7 +153,7 @@ class ServerAPI{ $this->loadAPI("chat", "ChatAPI"); $this->loadAPI("ban", "BanAPI"); $this->loadAPI("entity", "EntityAPI"); - $this->loadAPI("tileentity", "TileEntityAPI"); + $this->loadAPI("tile", "TileAPI"); $this->loadAPI("player", "PlayerAPI"); $this->loadAPI("time", "TimeAPI"); $this->loadAPI("mob", "MobAPI"); diff --git a/src/API/Tile.php b/src/API/Tile.php new file mode 100644 index 00000000..810263cd --- /dev/null +++ b/src/API/Tile.php @@ -0,0 +1,122 @@ +tiles = array(); + $this->server = ServerAPI::request(); + } + + public function get(Position $pos){ + $tile = $this->server->query("SELECT * FROM tiles WHERE level = '".$pos->level->getName()."' AND x = {$pos->x} AND y = {$pos->y} AND z = {$pos->z};", true); + if($tile !== false and $tile !== true and ($tile = $this->getByID($tile["ID"])) !== false){ + return $tile; + } + return false; + } + + public function getByID($id){ + if($id instanceof Tile){ + return $id; + }elseif(isset($this->tiles[$id])){ + return $this->tiles[$id]; + } + return false; + } + + public function init(){ + + } + + public function getAll($level = null){ + if($level instanceof Level){ + $tiles = array(); + $l = $this->server->query("SELECT ID FROM tiles WHERE level = '".$level->getName()."';"); + if($l !== false and $l !== true){ + while(($t = $l->fetchArray(SQLITE3_ASSOC)) !== false){ + $t = $this->getByID($t["ID"]); + if($t instanceof Tile){ + $tiles[$t->id] = $t; + } + } + } + return $tiles; + } + return $this->tiles; + } + + public function add(Level $level, $class, $x, $y, $z, $data = array()){ + $id = $this->tCnt++; + $this->tiles[$id] = new Tile($level, $id, $class, $x, $y, $z, $data); + $this->spawnToAll($this->tiles[$id]); + return $this->tiles[$id]; + } + + public function addSign(Level $level, $x, $y, $z, $lines = array("", "", "", "")){ + return $this->add($level, TILE_SIGN, $x, $y, $z, $data = array( + "id" => "Sign", + "x" => $x, + "y" => $y, + "z" => $z, + "Text1" => $lines[0], + "Text2" => $lines[1], + "Text3" => $lines[2], + "Text4" => $lines[3], + )); + } + + public function spawnToAll(Tile $t){ + foreach($this->server->api->player->getAll($t->level) as $player){ + if($player->eid !== false){ + $t->spawn($player); + } + } + } + + public function spawnAll(Player $player){ + foreach($this->getAll($player->level) as $t){ + $t->spawn($player); + } + } + + public function remove($id){ + if(isset($this->tiles[$id])){ + $t = $this->tiles[$id]; + $this->tiles[$id] = null; + unset($this->tiles[$id]); + $t->closed = true; + $t->close(); + $this->server->query("DELETE FROM tiles WHERE ID = ".$id.";"); + $this->server->api->dhandle("tile.remove", $t); + $t = null; + unset($t); + } + } +} \ No newline at end of file diff --git a/src/Player.php b/src/Player.php index 2b8662e0..c66b82c5 100644 --- a/src/Player.php +++ b/src/Player.php @@ -168,11 +168,11 @@ class Player{ "data" => $this->level->getOrderedMiniChunk($X, $Z, $Y), )); - $tiles = $this->server->query("SELECT ID FROM tileentities WHERE spawnable = 1 AND level = '".$this->level->getName()."' AND x >= ".($x - 1)." AND x < ".($x + 17)." AND z >= ".($z - 1)." AND z < ".($z + 17)." AND y >= ".($y - 1)." AND y < ".($y + 17).";"); + $tiles = $this->server->query("SELECT ID FROM tiles WHERE spawnable = 1 AND level = '".$this->level->getName()."' AND x >= ".($x - 1)." AND x < ".($x + 17)." AND z >= ".($z - 1)." AND z < ".($z + 17)." AND y >= ".($y - 1)." AND y < ".($y + 17).";"); if($tiles !== false and $tiles !== true){ while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ - $tile = $this->server->api->tileentitiy->getByID($tile["ID"]); - if($tile instanceof TileEntity){ + $tile = $this->server->api->tile->getByID($tile["ID"]); + if($tile instanceof Tile){ $tile->spawn($this); } } @@ -1290,8 +1290,8 @@ class Player{ if($this->spawned === false){ break; } - $t = $this->server->api->tileentity->get(new Position($data["x"], $data["y"], $data["z"], $this->level)); - if(($t instanceof TileEntity) and $t->class === TILE_SIGN){ + $t = $this->server->api->tile->get(new Position($data["x"], $data["y"], $data["z"], $this->level)); + if(($t instanceof Tile) and $t->class === TILE_SIGN){ if($t->data["creator"] !== $this->username){ $t->spawn($this); }else{ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 273fcfa5..78dc937e 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -52,7 +52,7 @@ class PocketMinecraftServer{ $this->invisible = false; $this->levelData = false; $this->difficulty = 1; - $this->tileEntities = array(); + $this->tiles = array(); $this->entities = array(); $this->custom = array(); $this->evCnt = 1; @@ -119,7 +119,7 @@ class PocketMinecraftServer{ $this->query("PRAGMA secure_delete = OFF;"); $this->query("CREATE TABLE players (clientID INTEGER PRIMARY KEY, EID NUMERIC, ip TEXT, port NUMERIC, name TEXT UNIQUE COLLATE NOCASE);"); $this->query("CREATE TABLE entities (EID INTEGER PRIMARY KEY, level TEXT, type NUMERIC, class NUMERIC, hasUpdate NUMERIC, name TEXT, x NUMERIC, y NUMERIC, z NUMERIC, yaw NUMERIC, pitch NUMERIC, health NUMERIC);"); - $this->query("CREATE TABLE tileentities (ID INTEGER PRIMARY KEY, level TEXT, class TEXT, x NUMERIC, y NUMERIC, z NUMERIC, spawnable NUMERIC);"); + $this->query("CREATE TABLE tiles (ID INTEGER PRIMARY KEY, level TEXT, class TEXT, x NUMERIC, y NUMERIC, z NUMERIC, spawnable NUMERIC);"); $this->query("CREATE TABLE actions (ID INTEGER PRIMARY KEY, interval NUMERIC, last NUMERIC, code TEXT, repeat NUMERIC);"); $this->query("CREATE TABLE handlers (ID INTEGER PRIMARY KEY, name TEXT, priority NUMERIC);"); $this->query("CREATE TABLE blockUpdates (level TEXT, x INTEGER, y INTEGER, z INTEGER, delay NUMERIC);"); diff --git a/src/material/block/solid/BurningFurnace.php b/src/material/block/solid/BurningFurnace.php index 1611c5ec..2f7cecbc 100644 --- a/src/material/block/solid/BurningFurnace.php +++ b/src/material/block/solid/BurningFurnace.php @@ -52,12 +52,12 @@ class BurningFurnaceBlock extends SolidBlock{ public function onActivate(Item $item, Player $player){ $server = ServerAPI::request(); - $t = $server->api->tileentity->get($this); + $t = $server->api->tile->get($this); $furnace = false; if($t !== false){ $furnace = $t; }else{ - $furnace = $server->api->tileentity->add($this->level, TILE_FURNACE, $this->x, $this->y, $this->z, array( + $furnace = $server->api->tile->add($this->level, TILE_FURNACE, $this->x, $this->y, $this->z, array( "Items" => array(), "id" => TILE_FURNACE, "x" => $this->x, diff --git a/src/material/block/solid/Chest.php b/src/material/block/solid/Chest.php index ef06fc1d..559d3dbb 100644 --- a/src/material/block/solid/Chest.php +++ b/src/material/block/solid/Chest.php @@ -45,7 +45,7 @@ class ChestBlock extends TransparentBlock{ $this->meta = $faces[$player->entity->getDirection()]; $this->level->setBlock($block, $this); $server = ServerAPI::request(); - $server->api->tileentity->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( + $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( "Items" => array(), "id" => TILE_CHEST, "x" => $this->x, @@ -70,12 +70,12 @@ class ChestBlock extends TransparentBlock{ } $server = ServerAPI::request(); - $t = $server->api->tileentity->get($this); + $t = $server->api->tile->get($this); $chest = false; if($t !== false){ $chest = $t; }else{ - $chest = $server->api->tileentity->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( + $chest = $server->api->tile->add($this->level, TILE_CHEST, $this->x, $this->y, $this->z, array( "Items" => array(), "id" => TILE_CHEST, "x" => $this->x, diff --git a/src/pmf/Level.php b/src/pmf/Level.php index 0ec97cd5..69dec65d 100644 --- a/src/pmf/Level.php +++ b/src/pmf/Level.php @@ -115,9 +115,9 @@ class PMFLevel extends PMF{ $entities = new Config(dirname($this->file)."/entities.yml", CONFIG_YAML); $entities->save(); } - if(!file_exists(dirname($this->file)."/tileEntities.yml")){ - $tileEntities = new Config(dirname($this->file)."/tileEntities.yml", CONFIG_YAML); - $tileEntities->save(); + if(!file_exists(dirname($this->file)."/tiles.yml")){ + $tiles = new Config(dirname($this->file)."/tiles.yml", CONFIG_YAML); + $tiles->save(); } } diff --git a/src/world/Level.php b/src/world/Level.php index e2d9b169..92451165 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -26,14 +26,14 @@ the Free Software Foundation, either version 3 of the License, or */ class Level{ - public $entities, $tileEntities, $nextSave, $players = array(); + public $entities, $tiles, $nextSave, $players = array(); private $level, $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount; - public function __construct(PMFLevel $level, Config $entities, Config $tileEntities, $name){ + public function __construct(PMFLevel $level, Config $entities, Config $tiles, $name){ $this->server = ServerAPI::request(); $this->level = $level; $this->entities = $entities; - $this->tileEntities = $tileEntities; + $this->tiles = $tiles; $this->startTime = $this->time = (int) $this->level->getData("time"); $this->nextSave = $this->startCheck = microtime(true); $this->nextSave += 90; @@ -216,11 +216,11 @@ class Level{ $this->entities->setAll($entities); $this->entities->save(); $tiles = array(); - foreach($this->server->api->tileentity->getAll($this) as $tile){ + foreach($this->server->api->tile->getAll($this) as $tile){ $tiles[] = $tile->data; } - $this->tileEntities->setAll($tiles); - $this->tileEntities->save(); + $this->tiles->setAll($tiles); + $this->tiles->save(); $this->level->setData("time", (int) $this->time); $this->level->doSaveRound(); @@ -295,7 +295,7 @@ class Level{ $this->server->api->entity->updateRadius($pos, 3); } if($tiles === true){ - if(($t = $this->server->api->tileentity->get($pos)) !== false){ + if(($t = $this->server->api->tile->get($pos)) !== false){ $t->close(); } } diff --git a/src/world/LevelImport.php b/src/world/LevelImport.php index 0d7436c1..2ced0c5a 100644 --- a/src/world/LevelImport.php +++ b/src/world/LevelImport.php @@ -32,13 +32,13 @@ class LevelImport{ } public function import(){ - if(file_exists($this->path."tileEntities.dat")){ //OldPM + if(file_exists($this->path."tiles.dat")){ //OldPM $level = unserialize(file_get_contents($this->path."level.dat")); console("[INFO] Importing OldPM level \"".$level["LevelName"]."\" to PMF format"); $entities = new Config($this->path."entities.yml", CONFIG_YAML, unserialize(file_get_contents($this->path."entities.dat"))); $entities->save(); - $tileEntities = new Config($this->path."tileEntities.yml", CONFIG_YAML, unserialize(file_get_contents($this->path."tileEntities.dat"))); - $tileEntities->save(); + $tiles = new Config($this->path."tiles.yml", CONFIG_YAML, unserialize(file_get_contents($this->path."tiles.dat"))); + $tiles->save(); }elseif(file_exists($this->path."chunks.dat") and file_exists($this->path."level.dat")){ //Pocket $nbt = new NBT(); $level = parseNBTData($nbt->loadFile($this->path."level.dat")); @@ -51,12 +51,12 @@ class LevelImport{ if(!isset($entities["TileEntities"])){ $entities["TileEntities"] = array(); } - $tileEntities = $entities["TileEntities"]; + $tiles = $entities["TileEntities"]; $entities = $entities["Entities"]; $entities = new Config($this->path."entities.yml", CONFIG_YAML, $entities); $entities->save(); - $tileEntities = new Config($this->path."tileEntities.yml", CONFIG_YAML, $tileEntities); - $tileEntities->save(); + $tiles = new Config($this->path."tiles.yml", CONFIG_YAML, $tiles); + $tiles->save(); }else{ return false; } @@ -113,8 +113,8 @@ class LevelImport{ @unlink($this->path."entities.dat"); @unlink($this->path."chunks.dat"); @unlink($this->path."chunks.dat.gz"); - @unlink($this->path."tileEntities.dat"); - unset($chunks, $level, $entities, $tileEntities, $nbt); + @unlink($this->path."tiles.dat"); + unset($chunks, $level, $entities, $tiles, $nbt); return true; } diff --git a/src/world/TileEntity.php b/src/world/TileEntity.php index afe1df33..be9b82d1 100644 --- a/src/world/TileEntity.php +++ b/src/world/TileEntity.php @@ -25,7 +25,7 @@ the Free Software Foundation, either version 3 of the License, or */ -class TileEntity extends Position{ +class Tile extends Position{ public $name; public $normal; public $id; @@ -55,10 +55,10 @@ class TileEntity extends Position{ $this->x = (int) $x; $this->y = (int) $y; $this->z = (int) $z; - $this->server->query("INSERT OR REPLACE INTO tileentities (ID, level, class, x, y, z) VALUES (".$this->id.", '".$this->level->getName()."', '".$this->class."', ".$this->x.", ".$this->y.", ".$this->z.");"); + $this->server->query("INSERT OR REPLACE INTO tiles (ID, level, class, x, y, z) VALUES (".$this->id.", '".$this->level->getName()."', '".$this->class."', ".$this->x.", ".$this->y.", ".$this->z.");"); switch($this->class){ case TILE_SIGN: - $this->server->query("UPDATE tileentities SET spawnable = 1 WHERE ID = ".$this->id.";"); + $this->server->query("UPDATE tiles SET spawnable = 1 WHERE ID = ".$this->id.";"); break; case TILE_FURNACE: if(!isset($this->data["BurnTime"]) or $this->data["BurnTime"] < 0){ @@ -234,7 +234,7 @@ class TileEntity extends Position{ public function close(){ if($this->closed === false){ $this->closed = true; - $this->server->api->tileentity->remove($this->id); + $this->server->api->tile->remove($this->id); } } @@ -250,12 +250,12 @@ class TileEntity extends Position{ public function setPosition(Vector3 $pos){ if($pos instanceof Position){ $this->level = $pos->level; - $this->server->query("UPDATE tileentities SET level = '".$this->level->getName()."' WHERE ID = ".$this->id.";"); + $this->server->query("UPDATE tiles SET level = '".$this->level->getName()."' WHERE ID = ".$this->id.";"); } $this->x = (int) $pos->x; $this->y = (int) $pos->y; $this->z = (int) $pos->z; - $this->server->query("UPDATE tileentities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z." WHERE ID = ".$this->id.";"); + $this->server->query("UPDATE tiles SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z." WHERE ID = ".$this->id.";"); } } diff --git a/src/world/generator/WorldGenerator.php b/src/world/generator/WorldGenerator.php index f8dd937c..08adefaf 100644 --- a/src/world/generator/WorldGenerator.php +++ b/src/world/generator/WorldGenerator.php @@ -46,8 +46,8 @@ class WorldGenerator{ "height" => $this->height )); $entities = new Config($this->path."entities.yml", CONFIG_YAML); - $tileEntities = new Config($this->path."tileEntities.yml", CONFIG_YAML); - $this->level = new Level($level, $entities, $tileEntities, $name); + $tiles = new Config($this->path."tiles.yml", CONFIG_YAML); + $this->level = new Level($level, $entities, $tiles, $name); } public function generate(){