Refactored EntityAPI and TileEntityAPI

This commit is contained in:
Shoghi Cervantes
2013-06-03 19:11:46 +02:00
parent 588379a430
commit 5bba03eb09
5 changed files with 18 additions and 38 deletions

View File

@ -112,21 +112,9 @@ class EntityAPI{
return $this->entities[$eid]; return $this->entities[$eid];
} }
public function spawnTo($eid, $player){ public function spawnToAll(Entity $e){
$e = $this->get($eid); foreach($this->server->api->player->getAll($e->level) as $player){
if($e === false){ if($player->eid !== false and $player->eid !== $e->eid){
return false;
}
$e->spawn($player);
}
public function spawnToAll(Level $level, $eid){
$e = $this->get($eid);
if($e === false){
return false;
}
foreach($this->server->api->player->getAll($level) as $player){
if($player->eid !== false and $player->eid !== $eid){
$e->spawn($player); $e->spawn($player);
} }
} }
@ -150,7 +138,7 @@ class EntityAPI{
$item->count = min($item->getMaxStackSize(), $count); $item->count = min($item->getMaxStackSize(), $count);
$count -= $item->count; $count -= $item->count;
$e = $this->add($pos->level, ENTITY_ITEM, $item->getID(), $data); $e = $this->add($pos->level, ENTITY_ITEM, $item->getID(), $data);
$this->spawnToAll($pos->level, $e->eid); $this->spawnToAll($e);
$this->server->api->handle("entity.motion", $e); $this->server->api->handle("entity.motion", $e);
} }
} }

View File

@ -75,7 +75,7 @@ class TileEntityAPI{
public function add(Level $level, $class, $x, $y, $z, $data = array()){ public function add(Level $level, $class, $x, $y, $z, $data = array()){
$id = $this->tCnt++; $id = $this->tCnt++;
$this->tileEntities[$id] = new TileEntity($level, $id, $class, $x, $y, $z, $data); $this->tileEntities[$id] = new TileEntity($level, $id, $class, $x, $y, $z, $data);
$this->spawnToAll($level, $id); $this->spawnToAll($this->tileEntities[$id]);
return $this->tileEntities[$id]; return $this->tileEntities[$id];
} }
@ -92,20 +92,8 @@ class TileEntityAPI{
)); ));
} }
public function spawnTo($id, $player){ public function spawnToAll(TileEntity $t){
$t = $this->getByID($id); foreach($this->server->api->player->getAll($t->level) as $player){
if($t === false){
return false;
}
$t->spawn($player);
}
public function spawnToAll(Level $level, $id){
$t = $this->getByID($id);
if($t === false){
return false;
}
foreach($this->server->api->player->getAll($level) as $player){
if($player->eid !== false){ if($player->eid !== false){
$t->spawn($player); $t->spawn($player);
} }
@ -126,6 +114,7 @@ class TileEntityAPI{
$t->closed = true; $t->closed = true;
$t->close(); $t->close();
$this->server->query("DELETE FROM tileentities WHERE ID = ".$id.";"); $this->server->query("DELETE FROM tileentities WHERE ID = ".$id.";");
$this->server->api->dhandle("tile.remove", $t);
$t = null; $t = null;
unset($t); unset($t);
} }

View File

@ -171,7 +171,10 @@ class Player{
$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 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).";");
if($tiles !== false and $tiles !== true){ if($tiles !== false and $tiles !== true){
while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){ while(($tile = $tiles->fetchArray(SQLITE3_ASSOC)) !== false){
$this->server->api->tileentity->spawnTo($tile["ID"], $this); $tile = $this->server->api->tileentitiy->getByID($tile["ID"]);
if($tile instanceof TileEntity){
$tile->spawn($this);
}
} }
} }
@ -636,7 +639,7 @@ class Player{
$this->level->freeAllChunks($this); $this->level->freeAllChunks($this);
$this->level = $pos->level; $this->level = $pos->level;
$this->chunksLoaded = array(); $this->chunksLoaded = array();
$this->server->api->entity->spawnToAll($this->level, $this->eid); $this->server->api->entity->spawnToAll($this->entity);
$this->server->api->entity->spawnAll($this); $this->server->api->entity->spawnAll($this);
$terrain = true; $terrain = true;
} }
@ -987,7 +990,7 @@ class Player{
} }
$this->spawned = true; $this->spawned = true;
$this->server->api->entity->spawnAll($this); $this->server->api->entity->spawnAll($this);
$this->server->api->entity->spawnToAll($this->level, $this->eid); $this->server->api->entity->spawnToAll($this->entity);
$this->server->schedule(5, array($this->entity, "update"), array(), true); $this->server->schedule(5, array($this->entity, "update"), array(), true);
$this->sendArmor(); $this->sendArmor();
$this->eventHandler(new Container($this->server->motd), "server.chat"); $this->eventHandler(new Container($this->server->motd), "server.chat");
@ -1089,7 +1092,7 @@ class Player{
"z" => $this->entity->z, "z" => $this->entity->z,
); );
$e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d); $e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d);
$this->server->api->entity->spawnToAll($this->level, $e->eid); $this->server->api->entity->spawnToAll($e);
} }
} }
break; break;
@ -1396,7 +1399,7 @@ class Player{
"meta" => $data[1], "meta" => $data[1],
"stack" => $data[2], "stack" => $data[2],
)); ));
$this->server->api->entity->spawnTo($e->eid, $this); $e->spawn($this);
} }
$this->inventory[$s] = array(AIR, 0, 0); $this->inventory[$s] = array(AIR, 0, 0);
}*/ }*/

View File

@ -55,7 +55,7 @@ class GenericBlock extends Block{
$server = ServerAPI::request(); $server = ServerAPI::request();
$this->level->setBlock($this, new AirBlock(), false, false, true); $this->level->setBlock($this, new AirBlock(), false, false, true);
$e = $server->api->entity->add($this->level, ENTITY_FALLING, FALLING_SAND, $data); $e = $server->api->entity->add($this->level, ENTITY_FALLING, FALLING_SAND, $data);
$server->api->entity->spawnToAll($this->level, $e->eid); $server->api->entity->spawnToAll($e);
$server->api->block->blockUpdateAround(clone $this, BLOCK_UPDATE_NORMAL, 1); $server->api->block->blockUpdateAround(clone $this, BLOCK_UPDATE_NORMAL, 1);
} }
return false; return false;

View File

@ -79,7 +79,7 @@ class PaintingItem extends Item{
"Motive" => $motive[0], "Motive" => $motive[0],
); );
$e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_PAINTING, $data); $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_PAINTING, $data);
$server->api->entity->spawnToAll($level, $e->eid); $server->api->entity->spawnToAll($e);
if(($player->gamemode & 0x01) === 0x00){ if(($player->gamemode & 0x01) === 0x00){
$player->removeItem($this->getID(), $this->getMetadata(), 1); $player->removeItem($this->getID(), $this->getMetadata(), 1);
} }