Added API method Entity->isClosed() and made Entity->closed protected

This commit is contained in:
Dylan K. Taylor 2017-08-16 19:30:23 +01:00
parent 20aa519f3a
commit eebc52e00b
7 changed files with 21 additions and 8 deletions

View File

@ -817,7 +817,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
if($this->spawned){ if($this->spawned){
foreach($this->level->getChunkEntities($x, $z) as $entity){ foreach($this->level->getChunkEntities($x, $z) as $entity){
if($entity !== $this and !$entity->closed and $entity->isAlive()){ if($entity !== $this and !$entity->isClosed() and $entity->isAlive()){
$entity->spawnTo($this); $entity->spawnTo($this);
} }
} }
@ -896,7 +896,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
foreach($this->usedChunks as $index => $c){ foreach($this->usedChunks as $index => $c){
Level::getXZ($index, $chunkX, $chunkZ); Level::getXZ($index, $chunkX, $chunkZ);
foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){ foreach($this->level->getChunkEntities($chunkX, $chunkZ) as $entity){
if($entity !== $this and !$entity->closed and $entity->isAlive()){ if($entity !== $this and !$entity->isClosed() and $entity->isAlive()){
$entity->spawnTo($this); $entity->spawnTo($this);
} }
} }

View File

@ -352,7 +352,7 @@ abstract class Entity extends Location implements Metadatable{
protected $server; protected $server;
/** @var bool */ /** @var bool */
public $closed = false; protected $closed = false;
/** @var TimingsHandler */ /** @var TimingsHandler */
protected $timings; protected $timings;
@ -1826,6 +1826,19 @@ abstract class Entity extends Location implements Metadatable{
} }
} }
/**
* Returns whether the entity has been "closed".
* @return bool
*/
public function isClosed() : bool{
return $this->closed;
}
/**
* Closes the entity and frees attached references.
*
* WARNING: Entities are unusable after this has been executed!
*/
public function close(){ public function close(){
if(!$this->closed){ if(!$this->closed){
$this->server->getPluginManager()->callEvent(new EntityDespawnEvent($this)); $this->server->getPluginManager()->callEvent(new EntityDespawnEvent($this));

View File

@ -722,7 +722,7 @@ class Level implements ChunkManager, Metadatable{
//Update entities that need update //Update entities that need update
Timings::$tickEntityTimer->startTiming(); Timings::$tickEntityTimer->startTiming();
foreach($this->updateEntities as $id => $entity){ foreach($this->updateEntities as $id => $entity){
if($entity->closed or !$entity->onUpdate($currentTick)){ if($entity->isClosed() or !$entity->onUpdate($currentTick)){
unset($this->updateEntities[$id]); unset($this->updateEntities[$id]);
} }
} }

View File

@ -611,7 +611,7 @@ class Chunk{
* @param Entity $entity * @param Entity $entity
*/ */
public function addEntity(Entity $entity){ public function addEntity(Entity $entity){
if($entity->closed){ if($entity->isClosed()){
throw new \InvalidArgumentException("Attempted to add a garbage closed Entity to a chunk"); throw new \InvalidArgumentException("Attempted to add a garbage closed Entity to a chunk");
} }
$this->entities[$entity->getId()] = $entity; $this->entities[$entity->getId()] = $entity;

View File

@ -71,7 +71,7 @@ class Anvil extends McRegion{
$entities = []; $entities = [];
foreach($chunk->getEntities() as $entity){ foreach($chunk->getEntities() as $entity){
if(!($entity instanceof Player) and !$entity->closed){ if(!($entity instanceof Player) and !$entity->isClosed()){
$entity->saveNBT(); $entity->saveNBT();
$entities[] = $entity->namedtag; $entities[] = $entity->namedtag;
} }

View File

@ -89,7 +89,7 @@ class McRegion extends BaseLevelProvider{
$entities = []; $entities = [];
foreach($chunk->getEntities() as $entity){ foreach($chunk->getEntities() as $entity){
if(!($entity instanceof Player) and !$entity->closed){ if(!($entity instanceof Player) and !$entity->isClosed()){
$entity->saveNBT(); $entity->saveNBT();
$entities[] = $entity->namedtag; $entities[] = $entity->namedtag;
} }

View File

@ -74,7 +74,7 @@ class PMAnvil extends Anvil{
$entities = []; $entities = [];
foreach($chunk->getEntities() as $entity){ foreach($chunk->getEntities() as $entity){
if(!($entity instanceof Player) and !$entity->closed){ if(!($entity instanceof Player) and !$entity->isClosed()){
$entity->saveNBT(); $entity->saveNBT();
$entities[] = $entity->namedtag; $entities[] = $entity->namedtag;
} }