Added methods to add/remove Entities/Tiles from Levels

This commit is contained in:
Shoghi Cervantes 2014-05-24 13:49:58 +02:00
parent f9353a0ecd
commit d8ea2e744f
3 changed files with 34 additions and 24 deletions

View File

@ -50,11 +50,6 @@ use pocketmine\Server;
abstract class Entity extends Position implements Metadatable{
public static $entityCount = 1;
/**
* @var Entity[]
*/
public static $list = [];
/**
* @var Entity[]
*/
@ -139,12 +134,10 @@ abstract class Entity extends Position implements Metadatable{
$index = LevelFormat::getIndex($this->x >> 4, $this->z >> 4);
$this->chunkIndex = $index;
Entity::$list[$this->id] = $this;
$this->getLevel()->entities[$this->id] = $this;
$this->getLevel()->addEntity($this);
$this->getLevel()->chunkEntities[$this->chunkIndex][$this->id] = $this;
$this->lastUpdate = microtime(true);
$this->initEntity();
Server::getInstance()->getPluginManager()->callEvent(new EntitySpawnEvent($this));
$this->server->getPluginManager()->callEvent(new EntitySpawnEvent($this));
}
public function saveNBT(){
@ -400,7 +393,8 @@ abstract class Entity extends Position implements Metadatable{
if($ev->isCancelled()){
return false;
}
unset($this->getLevel()->entities[$this->id]);
$this->getLevel()->removeEntity($this);
unset($this->getLevel()->chunkEntities[$this->chunkIndex][$this->id]);
$this->despawnFromAll();
if($this instanceof Player){
@ -418,7 +412,7 @@ abstract class Entity extends Position implements Metadatable{
}
}
$this->setLevel($targetLevel, true); //Hard reference
$this->getLevel()->entities[$this->id] = $this; //Oops, TODO!!
$this->getLevel()->addEntity($this);
if($this instanceof Player){
$this->chunksLoaded = [];
$pk = new SetTimePacket();
@ -576,9 +570,8 @@ abstract class Entity extends Position implements Metadatable{
if($this->closed === false){
$this->closed = true;
unset(Entity::$needUpdate[$this->id]);
unset($this->getLevel()->entities[$this->id]);
$this->getLevel()->removeEntity($this);
unset($this->getLevel()->chunkEntities[$this->chunkIndex][$this->id]);
unset(Entity::$list[$this->id]);
$this->despawnFromAll();
Server::getInstance()->getPluginManager()->callEvent(new EntityDespawnEvent($this));
}

View File

@ -73,7 +73,7 @@ class Level{
public $chunkEntities = [];
/** @var Tile[] */
public $tiles = [];
protected $tiles = [];
/** @var Tile[][] */
public $chunkTiles = [];
@ -816,6 +816,22 @@ class Level{
return $this->entities;
}
public function addEntity(Entity $entity){
$this->entities[$entity->getID()] = $entity;
}
public function removeEntity(Entity $entity){
unset($this->entities[$entity->getID()]);
}
public function addTile(Tile $tile){
$this->tiles[$tile->getID()] = $tile;
}
public function removeTile(Tile $tile){
unset($this->tiles[$tile->getID()]);
}
/**
* Returns a list of the Tile entities in this level
*
@ -825,6 +841,15 @@ class Level{
return $this->tiles;
}
/**
* @param $tileId
*
* @return Tile
*/
public function getTileById($tileId){
return isset($this->tiles[$tileId]) ? $this->tiles[$tileId] : null;
}
/**
* Returns a list of the players in this level
*

View File

@ -63,14 +63,6 @@ abstract class Tile extends Position{
protected $lastUpdate;
protected $server;
public static function getByID($tileID){
return isset(Tile::$list[$tileID]) ? Tile::$list[$tileID] : false;
}
public static function getAll(){
return Tile::$list;
}
public function getID(){
return $this->id;
}
@ -91,7 +83,7 @@ abstract class Tile extends Position{
$index = LevelFormat::getIndex($this->x >> 4, $this->z >> 4);
$this->chunkIndex = $index;
$this->getLevel()->tiles[$this->id] = $this;
$this->getLevel()->addTile($this);
$this->getLevel()->chunkTiles[$this->chunkIndex][$this->id] = $this;
}
@ -113,7 +105,7 @@ abstract class Tile extends Position{
if($this->closed === false){
$this->closed = true;
unset(Tile::$needUpdate[$this->id]);
unset($this->getLevel()->tiles[$this->id]);
$this->getLevel()->removeTile($this);
unset($this->getLevel()->chunkTiles[$this->chunkIndex][$this->id]);
unset(Tile::$list[$this->id]);
}