From 2bf3f0de739747039c2538c89803e12964d6cd17 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Thu, 27 Feb 2014 16:47:49 +0100 Subject: [PATCH] Removed EntityAPI --- src/API/BlockAPI.php | 1 - src/API/EntityAPI.php | 174 ----------------------- src/API/LevelAPI.php | 6 +- src/API/PlayerAPI.php | 10 +- src/API/ServerAPI.php | 5 +- src/Entity.php | 71 +++++++-- src/Player.php | 22 +-- src/Tile.php | 12 +- src/dependencies.php | 4 +- src/material/block/GenericBlock.php | 3 +- src/material/block/misc/TNT.php | 3 +- src/material/block/plant/RedMushroom.php | 1 + src/material/block/plant/Sapling.php | 1 + src/material/block/plant/Sugarcane.php | 1 + src/material/block/plant/Wheat.php | 1 + src/material/block/solid/Leaves.php | 2 + src/material/item/generic/Painting.php | 3 +- src/tile/ChestTile.php | 2 +- src/tile/FurnaceTile.php | 2 +- src/tile/SignTile.php | 2 +- src/tile/SpawnableTile.php | 4 +- src/world/Explosion.php | 6 +- src/world/Level.php | 30 +++- 23 files changed, 142 insertions(+), 224 deletions(-) delete mode 100644 src/API/EntityAPI.php diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 2f49c86c9..6889d4ea4 100644 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -500,7 +500,6 @@ class BlockAPI{ $level = $block->onUpdate($type); if($level === BLOCK_UPDATE_NORMAL){ $this->blockUpdateAround($block, $level); - $this->server->api->entity->updateRadius($pos, 1); }elseif($level === BLOCK_UPDATE_RANDOM){ $this->nextRandomUpdate($pos); } diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php deleted file mode 100644 index 3c3ec38e8..000000000 --- a/src/API/EntityAPI.php +++ /dev/null @@ -1,174 +0,0 @@ -entities = array(); - $this->server = ServerAPI::request(); - } - - public function get($eid){ - if(isset($this->entities[$eid])){ - return $this->entities[$eid]; - } - return false; - } - - public function init(){ - $this->server->schedule(25, array($this, "updateEntities"), array(), true); - } - - public function updateEntities(){ - $l = $this->server->query("SELECT EID FROM entities WHERE hasUpdate = 1;"); - - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $e->update(); - $this->server->query("UPDATE entities SET hasUpdate = 0 WHERE EID = ".$e->eid.";"); - } - } - } - } - - public function updateRadius(Position $center, $radius = 15, $class = false){ - $this->server->query("UPDATE entities SET hasUpdate = 1 WHERE level = '".$center->level->getName()."' ".($class !== false ? "AND class = $class ":"")."AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); - } - - public function getRadius(Position $center, $radius = 15, $class = false){ - $entities = array(); - $l = $this->server->query("SELECT EID FROM entities WHERE level = '".$center->level->getName()."' ".($class !== false ? "AND class = $class ":"")."AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $entities[$e->eid] = $e; - } - } - } - return $entities; - } - - public function getAll($level = null){ - if($level instanceof Level){ - $entities = array(); - $l = $this->server->query("SELECT EID FROM entities WHERE level = '".$level->getName()."';"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $entities[$e->eid] = $e; - } - } - } - return $entities; - } - return $this->entities; - } - - public function heal($eid, $heal = 1, $cause){ - $this->harm($eid, -$heal, $cause); - } - - public function harm($eid, $attack = 1, $cause, $force = false){ - $e = $this->get($eid); - if($e === false or $e->dead === true){ - return false; - } - $e->setHealth($e->getHealth() - $attack, $cause, $force); - } - - public function add(Level $level, $class, $type = 0, $data = array()){ - $eid = $this->eCnt++; - $this->entities[$eid] = new Entity($level, $eid, $class, $type, $data); - $this->server->handle("entity.add", $this->entities[$eid]); - return $this->entities[$eid]; - } - - public function spawnToAll(Entity $e){ - foreach($this->server->api->player->getAll($e->level) as $player){ - if($player->eid !== false and $player->eid !== $e->eid and $e->class !== ENTITY_PLAYER and $e instanceof Entity){ - $e->spawn($player); - } - } - } - - public function drop(Position $pos, Item $item){ - if($item->getID() === AIR or $item->count <= 0){ - return; - } - $data = array( - "x" => $pos->x + mt_rand(-10, 10) / 50, - "y" => $pos->y + 0.19, - "z" => $pos->z + mt_rand(-10, 10) / 50, - "level" => $pos->level, - //"speedX" => mt_rand(-3, 3) / 8, - "speedY" => mt_rand(5, 8) / 2, - //"speedZ" => mt_rand(-3, 3) / 8, - "item" => $item, - ); - if($this->server->api->handle("item.drop", $data) !== false){ - for($count = $item->count; $count > 0; ){ - $item->count = min($item->getMaxStackSize(), $count); - $count -= $item->count; - $e = $this->add($pos->level, ENTITY_ITEM, $item->getID(), $data); - $this->spawnToAll($e); - $this->server->api->handle("entity.motion", $e); - } - } - } - - public function spawnAll(Player $player){ - foreach($this->getAll($player->level) as $e){ - if($e->class !== ENTITY_PLAYER){ - $e->spawn($player); - } - } - } - - public function remove($eid){ - if(isset($this->entities[$eid])){ - $entity = $this->entities[$eid]; - $this->entities[$eid] = null; - unset($this->entities[$eid]); - $entity->closed = true; - $this->server->query("DELETE FROM entities WHERE EID = ".$eid.";"); - if($entity->class === ENTITY_PLAYER){ - $pk = new RemovePlayerPacket; - $pk->eid = $entity->eid; - $pk->clientID = 0; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll(), $pk); - }else{ - $pk = new RemoveEntityPacket; - $pk->eid = $entity->eid; - $this->server->api->player->broadcastPacket($this->server->api->player->getAll($entity->level), $pk); - } - $this->server->api->dhandle("entity.remove", $entity); - $entity = null; - unset($entity); - } - } -} diff --git a/src/API/LevelAPI.php b/src/API/LevelAPI.php index fe1ca26c4..170067147 100644 --- a/src/API/LevelAPI.php +++ b/src/API/LevelAPI.php @@ -155,13 +155,13 @@ class LevelAPI{ console("[ERROR] Could not load level \"".$name."\""); return false; } - $entities = new Config($path."entities.yml", Config::YAML); + //$entities = new Config($path."entities.yml", Config::YAML); if(file_exists($path."tileEntities.yml")){ @rename($path."tileEntities.yml", $path."tiles.yml"); } $blockUpdates = new Config($path."bupdates.yml", Config::YAML); $this->levels[$name] = new Level($level, $name); - foreach($entities->getAll() as $entity){ + /*foreach($entities->getAll() as $entity){ if(!isset($entity["id"])){ break; } @@ -188,7 +188,7 @@ class LevelAPI{ $e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]); $e->setHealth($entity["Health"]); } - } + }*/ if(file_exists($path ."tiles.yml")){ $tiles = new Config($path."tiles.yml", Config::YAML); diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 7be632c17..a1ac81b6e 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -51,7 +51,7 @@ class PlayerAPI{ $result = $this->server->preparedSQL->selectPlayersToHeal->execute(); if($result !== false){ while(($player = $result->fetchArray()) !== false){ - if(($player = $this->server->api->entity->get($player["EID"])) !== false){ + if(($player = Entity::get($player["EID"])) !== false){ if($player->getHealth() <= 0){ continue; } @@ -64,7 +64,7 @@ class PlayerAPI{ break; case "player.death": if(is_numeric($data["cause"])){ - $e = $this->server->api->entity->get($data["cause"]); + $e = Entity::get($data["cause"]); if($e instanceof Entity){ switch($e->class){ case ENTITY_PLAYER: @@ -422,7 +422,7 @@ class PlayerAPI{ public function spawnAllPlayers(Player $player){ foreach($this->getAll() as $p){ if($p !== $player and ($p->entity instanceof Entity)){ - $p->entity->spawn($player); + $p->entity->spawnTo($player); if($p->level !== $player->level){ $pk = new MoveEntityPacket_PosRot; $pk->eid = $p->entity->eid; @@ -440,7 +440,7 @@ class PlayerAPI{ public function spawnToAllPlayers(Player $player){ foreach($this->getAll() as $p){ if($p !== $player and ($p->entity instanceof Entity) and ($player->entity instanceof Entity)){ - $player->entity->spawn($p); + $player->entity->spawnTo($p); if($p->level !== $player->level){ $pk = new MoveEntityPacket_PosRot; $pk->eid = $player->entity->eid; @@ -467,8 +467,8 @@ class PlayerAPI{ if($player->entity instanceof Entity){ unset($player->entity->player); //unset($player->entity); + $player->entity->close(); } - $this->server->api->entity->remove($player->eid); $player = null; unset($player); } diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index de81a5d31..e431d2525 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -205,8 +205,7 @@ class ServerAPI{ $this->loadAPI("level", "LevelAPI"); $this->loadAPI("block", "BlockAPI"); $this->loadAPI("chat", "ChatAPI"); - $this->loadAPI("ban", "BanAPI"); - $this->loadAPI("entity", "EntityAPI"); + $this->loadAPI("ban", "BanAPI"); $this->loadAPI("player", "PlayerAPI"); $this->loadAPI("time", "TimeAPI"); @@ -226,7 +225,7 @@ class ServerAPI{ //Update tiles that need update if(count(Tile::$needUpdate) > 0){ foreach(Tile::$needUpdate as $id => $tile){ - if($tile->update() === false){ + if($tile->onUpdate() === false){ unset(Tile::$needUpdate[$id]); } } diff --git a/src/Entity.php b/src/Entity.php index 0b57a8709..e41f9665c 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -54,22 +54,36 @@ abstract class Entity extends Position{ public $noDamageTicks; private $justCreated; protected $fireProof; - private $invulnerable; + private $invulnerable; - public static function getEntity($entityID){ + public $closed; + + public static function get($entityID){ return isset(Entity::$list[$entityID]) ? Entity::$list[$entityID]:false; } + public static function getAll(){ + return $this->list; + } - public function __construct(Level $level){ + + public function __construct(Level $level, NBTTag_Compound $nbt){ $this->id = Entity::$entityCount++; $this->justCreated = true; + $this->closed = false; + $this->namedtag = $nbt; $this->level = $level; - $this->setPosition(new Vector3(0, 0, 0)); + $this->boundingBox = new AxisAlignedBB(0, 0, 0, 0, 0, 0); + $this->setPosition(new Vector3($this->namedtag->x, $this->namedtag->y, $this->namedtag->z)); + $index = PMFLevel::getIndex($this->x >> 4, $this->z >> 4); + $this->chunkIndex = $index; Entity::$list[$this->id] = $this; + $this->level->entities[$this->id] = $this; + $this->level->chunkEntities[$this->chunkIndex][$this->id] = $this; $this->lastUpdate = microtime(true); $this->initEntity(); + $this->server->api->dhandle("entity.add", $this); } protected abstract function initEntity(); @@ -78,7 +92,10 @@ abstract class Entity extends Position{ abstract function attackEntity($damage, $source = "generic"); - public function onEntityUpdate(){ + public function onUpdate(){ + if($this->closed !== false){ + return false; + } $timeNow = microtime(true); $this->ticksLived += ($now - $this->lastUpdate) * 20; $this->lastUpdate = $timeNow; @@ -114,6 +131,11 @@ abstract class Entity extends Position{ if($this->y < -64){ $this->kill(); } + return false; + } + + public final function scheduleUpdate(){ + Entity::$needUpdate[$this->id] = $this; } public function setOnFire($seconds){ @@ -216,14 +238,43 @@ abstract class Entity extends Position{ public function teleport(Position $pos){ } - - public function equals($object){ - return $object instanceof Entity ? $object->getID() === $this->id : false; - } - + public function getID(){ return $this->id; } + + public function spawnToAll(){ + foreach($this->level->getPlayers() as $player){ + if($player->eid !== false or $player->spawned !== true){ + $this->spawnTo($player); + } + } + } + + public function close(){ + if($this->closed === false){ + $this->closed = true; + unset(Entity::$needUpdate[$this->id]); + unset($this->level->entities[$this->id]); + unset($this->level->chunkEntities[$this->chunkIndex][$this->id]); + unset(Entity::$list[$this->id]); + if($this instanceof HumanEntity){ + $pk = new RemovePlayerPacket; + $pk->eid = $this->id; + $pk->clientID = 0; + $this->server->api->player->broadcastPacket($this->level->getPlayers(), $pk); + }else{ + $pk = new RemoveEntityPacket; + $pk->eid = $this->id; + $this->server->api->player->broadcastPacket($this->level->getPlayers(), $pk); + } + $this->server->api->dhandle("entity.remove", $this); + } + } + + public function __destruct(){ + $this->close(); + } } diff --git a/src/Player.php b/src/Player.php index 0194cd4a4..c3393b706 100644 --- a/src/Player.php +++ b/src/Player.php @@ -198,7 +198,7 @@ class Player{ if(is_array($this->lastChunk)){ foreach($this->level->getChunkTiles($this->lastChunk[0], $this->lastChunk[1]) as $tile){ if($tile instanceof SpawnableTile){ - $tile->spawn($this); + $tile->spawnTo($this); } } $this->lastChunk = false; @@ -918,7 +918,7 @@ class Player{ return false; } - foreach($this->server->api->entity->getAll($this->level) as $e){ + foreach($this->level->getEntities() as $e){ if($e !== $this->entity){ if($e->player instanceof Player){ $pk = new MoveEntityPacket_PosRot; @@ -950,7 +950,8 @@ class Player{ $this->level->freeAllChunks($this); $this->level = $pos->level; $this->chunksLoaded = array(); - $this->server->api->entity->spawnToAll($this->entity); + $this->entity->spawnToAll(); + //TODO $this->server->api->entity->spawnAll($this); $pk = new SetTimePacket; @@ -1459,11 +1460,14 @@ class Player{ break; } $this->entity->setHealth($this->data->get("health"), "spawn", true); - $this->spawned = true; + $this->spawned = true; + //TODO $this->server->api->player->spawnAllPlayers($this); + //TODO $this->server->api->player->spawnToAllPlayers($this); + //TODO $this->server->api->entity->spawnAll($this); - $this->server->api->entity->spawnToAll($this->entity); + $this->entity->spawnToAll(); $this->server->schedule(5, array($this->entity, "update"), array(), true); $this->server->schedule(2, array($this->entity, "updateMovement"), array(), true); @@ -1749,7 +1753,7 @@ class Player{ $e->speedX = $speedX; $e->speedZ = $speedZ; $e->speedY = $speedY; - $this->server->api->entity->spawnToAll($e); + $e->spawnToAll(); } } } @@ -1828,7 +1832,7 @@ class Player{ $data["action"] = $packet->action; $this->craftingItems = array(); $this->toCraft = array(); - $target = $this->server->api->entity->get($packet->target); + $target = Entity::get($packet->target); if($target instanceof Entity and $this->entity instanceof Entity and $this->gamemode !== VIEW and $this->blocked === false and ($target instanceof Entity) and $this->entity->distance($target) <= 8){ $data["targetentity"] = $target; $data["entity"] = $this->entity; @@ -2241,12 +2245,12 @@ class Player{ $t = $this->level->getTile(new Vector3($packet->x, $packet->y, $packet->z)); if($t instanceof SignTile){ if($t->namedtag->creator !== $this->username){ - $t->spawn($this); + $t->spawnTo($this); }else{ $nbt = new NBT(); $nbt->read($packet->namedtag); if($nbt->id !== Tile::SIGN){ - $t->spawn($this); + $t->spawnTo($this); }else{ $t->setText($nbt->Text1, $nbt->Text2, $nbt->Text3, $nbt->Text4); } diff --git a/src/Tile.php b/src/Tile.php index 27de39482..8f3cea721 100644 --- a/src/Tile.php +++ b/src/Tile.php @@ -30,7 +30,6 @@ abstract class Tile extends Position{ public $chunkIndex; public $name; - public $normal; public $id; public $x; public $y; @@ -60,7 +59,6 @@ abstract class Tile extends Position{ $this->server = ServerAPI::request(); $this->level = $level; $this->namedtag = $nbt; - $this->normal = true; $this->closed = false; $this->name = ""; $this->lastUpdate = microtime(true); @@ -72,12 +70,13 @@ abstract class Tile extends Position{ $this->z = (int) $this->namedtag->z; $index = PMFLevel::getIndex($this->x >> 4, $this->z >> 4); - $this->level->tiles[$this->id] = $this; $this->chunkIndex = $index; - $this->level->chunkTiles[$index][$this->id] = $this; + $this->level->tiles[$this->id] = $this; + $this->level->chunkTiles[$this->chunkIndex][$this->id] = $this; + $this->server->api->dhandle("tile.add", $this); } - public function update(){ + public function onUpdate(){ return false; } @@ -88,10 +87,9 @@ abstract class Tile extends Position{ public function close(){ if($this->closed === false){ $this->closed = true; - $index = PMFLevel::getIndex($this->x >> 4, $this->z >> 4); unset(Tile::$needUpdate[$this->id]); unset($this->level->tiles[$this->id]); - unset($this->level->chunkTiles[$index][$this->id]); + unset($this->level->chunkTiles[$this->chunkIndex][$this->id]); unset(Tile::$list[$this->id]); $this->server->api->dhandle("tile.remove", $t); } diff --git a/src/dependencies.php b/src/dependencies.php index ac3167356..4ee90847f 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -62,7 +62,7 @@ if(!extension_loaded("pthreads") and @dl((PHP_SHLIB_SUFFIX === "dll" ? "php_":"" if(version_compare($pthreads_version, "0.1.0") < 0){ console("[ERROR] pthreads >= 0.1.0 is required, while you have $pthreads_version.", true, true, 0); ++$errors; - } + } } if(!extension_loaded("curl") and @dl((PHP_SHLIB_SUFFIX === "dll" ? "php_":"") . "curl." . PHP_SHLIB_SUFFIX) === false){ @@ -96,7 +96,7 @@ require_once(FILE_PATH."/src/math/Vector3.php"); require_once(FILE_PATH."/src/math/Position.php"); require_once(FILE_PATH."/src/pmf/PMF.php"); -require_all(FILE_PATH . "src/", array("entity", "Entity.php")); //REMOVE LATER!!!! +require_all(FILE_PATH . "src/"); $inc = get_included_files(); $inc[] = array_shift($inc); diff --git a/src/material/block/GenericBlock.php b/src/material/block/GenericBlock.php index 6e4e01a2d..f76d00beb 100644 --- a/src/material/block/GenericBlock.php +++ b/src/material/block/GenericBlock.php @@ -83,8 +83,9 @@ class GenericBlock extends Block{ ); $server = ServerAPI::request(); $this->level->setBlock($this, new AirBlock(), false, false, true); + //TODO $e = $server->api->entity->add($this->level, ENTITY_FALLING, FALLING_SAND, $data); - $server->api->entity->spawnToAll($e); + $e->spawnToAll(); $server->api->block->blockUpdateAround(clone $this, BLOCK_UPDATE_NORMAL, 1); } return false; diff --git a/src/material/block/misc/TNT.php b/src/material/block/misc/TNT.php index 8ac827636..45de12c76 100644 --- a/src/material/block/misc/TNT.php +++ b/src/material/block/misc/TNT.php @@ -39,8 +39,9 @@ class TNTBlock extends SolidBlock{ "fuse" => 20 * 4, //4 seconds ); $this->level->setBlock($this, new AirBlock(), false, false, true); + //TODO $e = ServerAPI::request()->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data); - ServerAPI::request()->api->entity->spawnToAll($e); + $e->spawnToAll(); return true; } return false; diff --git a/src/material/block/plant/RedMushroom.php b/src/material/block/plant/RedMushroom.php index 288c4f935..b860857e9 100644 --- a/src/material/block/plant/RedMushroom.php +++ b/src/material/block/plant/RedMushroom.php @@ -28,6 +28,7 @@ class RedMushroomBlock extends FlowableBlock{ public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ if($this->getSide(0)->isTransparent === true){ //Replace with common break method + //TODO ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id)); $this->level->setBlock($this, new AirBlock(), false); return BLOCK_UPDATE_NORMAL; diff --git a/src/material/block/plant/Sapling.php b/src/material/block/plant/Sapling.php index 21eaadbeb..a254a7e3e 100644 --- a/src/material/block/plant/Sapling.php +++ b/src/material/block/plant/Sapling.php @@ -62,6 +62,7 @@ class SaplingBlock extends FlowableBlock{ public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ if($this->getSide(0)->isTransparent === true){ //Replace with common break method + //TODO ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id)); $this->level->setBlock($this, new AirBlock(), false, false, true); return BLOCK_UPDATE_NORMAL; diff --git a/src/material/block/plant/Sugarcane.php b/src/material/block/plant/Sugarcane.php index 0b10d4952..6a8903429 100644 --- a/src/material/block/plant/Sugarcane.php +++ b/src/material/block/plant/Sugarcane.php @@ -56,6 +56,7 @@ class SugarcaneBlock extends FlowableBlock{ if($type === BLOCK_UPDATE_NORMAL){ $down = $this->getSide(0); if($down->isTransparent === true and $down->getID() !== SUGARCANE_BLOCK){ //Replace with common break method + //TODO ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(SUGARCANE)); $this->level->setBlock($this, new AirBlock(), false, false, true); return BLOCK_UPDATE_NORMAL; diff --git a/src/material/block/plant/Wheat.php b/src/material/block/plant/Wheat.php index 1b28ebc23..b049c608f 100644 --- a/src/material/block/plant/Wheat.php +++ b/src/material/block/plant/Wheat.php @@ -51,6 +51,7 @@ class WheatBlock extends FlowableBlock{ public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ if($this->getSide(0)->isTransparent === true){ //Replace with common break method + //TODO ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(WHEAT_SEEDS, 0, 1)); $this->level->setBlock($this, new AirBlock(), false, false, true); return BLOCK_UPDATE_NORMAL; diff --git a/src/material/block/solid/Leaves.php b/src/material/block/solid/Leaves.php index c0cc6fbd1..cef6e32aa 100644 --- a/src/material/block/solid/Leaves.php +++ b/src/material/block/solid/Leaves.php @@ -117,9 +117,11 @@ class LeavesBlock extends TransparentBlock{ }else{ $this->level->setBlock($this, new AirBlock(), false, false, true); if(mt_rand(1,20) === 1){ //Saplings + //TODO ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(SAPLING, $this->meta & 0x03, 1)); } if(($this->meta & 0x03) === LeavesBlock::OAK and mt_rand(1,200) === 1){ //Apples + //TODO ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem(APPLE, 0, 1)); } return BLOCK_UPDATE_NORMAL; diff --git a/src/material/item/generic/Painting.php b/src/material/item/generic/Painting.php index a82a87db3..7e40d7c77 100644 --- a/src/material/item/generic/Painting.php +++ b/src/material/item/generic/Painting.php @@ -72,8 +72,9 @@ class PaintingItem extends Item{ "yaw" => $faces[$face] * 90, "Motive" => $motive[0], ); + //TODO $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_PAINTING, $data); - $server->api->entity->spawnToAll($e); + $e->spawnToAll(); if(($player->gamemode & 0x01) === 0x00){ $player->removeItem($this->getID(), $this->getMetadata(), 1, false); } diff --git a/src/tile/ChestTile.php b/src/tile/ChestTile.php index a0b67eee1..78a79923a 100644 --- a/src/tile/ChestTile.php +++ b/src/tile/ChestTile.php @@ -81,7 +81,7 @@ class ChestTile extends SpawnableTile{ } } - public function spawn(Player $player){ + public function spawnTo(Player $player){ if($this->closed){ return false; } diff --git a/src/tile/FurnaceTile.php b/src/tile/FurnaceTile.php index 841e4d78f..963d379ed 100644 --- a/src/tile/FurnaceTile.php +++ b/src/tile/FurnaceTile.php @@ -46,7 +46,7 @@ class FurnaceTile extends Tile{ } } - public function update(){ + public function onUpdate(){ if($this->closed === true){ return false; } diff --git a/src/tile/SignTile.php b/src/tile/SignTile.php index 69ade050c..8e318e54f 100644 --- a/src/tile/SignTile.php +++ b/src/tile/SignTile.php @@ -49,7 +49,7 @@ class SignTile extends SpawnableTile{ ); } - public function spawn(Player $player){ + public function spawnTo(Player $player){ if($this->closed){ return false; } diff --git a/src/tile/SpawnableTile.php b/src/tile/SpawnableTile.php index 2514d377f..5779080e1 100644 --- a/src/tile/SpawnableTile.php +++ b/src/tile/SpawnableTile.php @@ -20,12 +20,12 @@ */ abstract class SpawnableTile extends Tile{ - public abstract function spawn(Player $player); + public abstract function spawnTo(Player $player); public function spawnToAll(){ foreach($this->level->getPlayers() as $player){ if($player->eid !== false or $player->spawned !== true){ - $this->spawn($player); + $this->spawnTo($player); } } } diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 751b3a735..502c6dc8b 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -86,6 +86,7 @@ class Explosion{ $send = array(); $source = $this->source->floor(); $radius = 2 * $this->size; + //TODO foreach($server->api->entity->getRadius($this->source, $radius) as $entity){ $impact = (1 - $this->source->distance($entity) / $radius) * 0.5; //placeholder, 0.7 should be exposure $damage = (int) (($impact * $impact + $impact) * 8 * $this->size + 1); @@ -102,12 +103,15 @@ class Explosion{ "power" => 4, "fuse" => mt_rand(10, 30), //0.5 to 1.5 seconds ); + //TODO $e = $server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data); - $server->api->entity->spawnToAll($e); + $e->spawnToAll(); }elseif(mt_rand(0, 10000) < ((1/$this->size) * 10000)){ if(isset(self::$specialDrops[$block->getID()])){ + //TODO $server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), BlockAPI::getItem(self::$specialDrops[$block->getID()], 0)); }else{ + //TODO $server->api->entity->drop(new Position($block->x + 0.5, $block->y, $block->z + 0.5, $this->level), BlockAPI::getItem($block->getID(), $this->level->level->getBlockDamage($block->x, $block->y, $block->z))); } } diff --git a/src/world/Level.php b/src/world/Level.php index 0c532b1c2..7c214a45a 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -20,6 +20,8 @@ */ class Level{ + public $players = array(); + public $entities = array(); public $chunkEntities = array(); @@ -204,6 +206,14 @@ class Level{ $nbt->Entities->setTagType(NBTTag::TAG_Compound); $nbt->TileEntities->setTagType(NBTTag::TAG_Compound); + $i = 0; + foreach($this->chunkEntities[$index] as $entity){ + if($entity->closed !== true){ + $nbt->Entities[$i] = $entity->namedtag; + ++$i; + } + } + $i = 0; foreach($this->chunkTiles[$index] as $tile){ if($tile->closed !== true){ @@ -294,7 +304,6 @@ class Level{ if($update === true){ $this->server->api->block->blockUpdateAround($pos, BLOCK_UPDATE_NORMAL, 1); - $this->server->api->entity->updateRadius($pos, 3); } if($tiles === true){ if(($t = $this->getTile($pos)) !== false){ @@ -305,6 +314,18 @@ class Level{ return $ret; } + public function getEntities(){ + return $this->entities; + } + + public function getTiles(){ + return $this->tiles; + } + + public function getPlayers(){ + return $this->players; + } + public function getTile(Vector3 $pos){ if($pos instanceof Position and $pos->level !== $this){ return false; @@ -354,6 +375,8 @@ class Level{ return array(); } + + public function loadChunk($X, $Z){ if(!isset($this->level)){ return false; @@ -365,6 +388,11 @@ class Level{ $this->usedChunks[$index] = array(); $this->chunkTiles[$index] = array(); $this->chunkEntities[$index] = array(); + foreach($this->level->getChunkNBT($X, $Z)->Entities as $nbt){ + switch($nbt->id){ + //TODO: spawn entities + } + } foreach($this->level->getChunkNBT($X, $Z)->TileEntities as $nbt){ switch($nbt->id){ case Tile::CHEST: