From eebc52e00ba6ad38bf1c249a6f0e9415c81d5072 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 16 Aug 2017 19:30:23 +0100 Subject: [PATCH] Added API method Entity->isClosed() and made Entity->closed protected --- src/pocketmine/Player.php | 4 ++-- src/pocketmine/entity/Entity.php | 15 ++++++++++++++- src/pocketmine/level/Level.php | 2 +- src/pocketmine/level/format/Chunk.php | 2 +- src/pocketmine/level/format/io/region/Anvil.php | 2 +- .../level/format/io/region/McRegion.php | 2 +- src/pocketmine/level/format/io/region/PMAnvil.php | 2 +- 7 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 93de2a5b7..b610f4b49 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -817,7 +817,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ if($this->spawned){ 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); } } @@ -896,7 +896,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ foreach($this->usedChunks as $index => $c){ Level::getXZ($index, $chunkX, $chunkZ); 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); } } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 374233673..4e48778df 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -352,7 +352,7 @@ abstract class Entity extends Location implements Metadatable{ protected $server; /** @var bool */ - public $closed = false; + protected $closed = false; /** @var TimingsHandler */ 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(){ if(!$this->closed){ $this->server->getPluginManager()->callEvent(new EntityDespawnEvent($this)); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 136b78bcb..b775b08a8 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -722,7 +722,7 @@ class Level implements ChunkManager, Metadatable{ //Update entities that need update Timings::$tickEntityTimer->startTiming(); foreach($this->updateEntities as $id => $entity){ - if($entity->closed or !$entity->onUpdate($currentTick)){ + if($entity->isClosed() or !$entity->onUpdate($currentTick)){ unset($this->updateEntities[$id]); } } diff --git a/src/pocketmine/level/format/Chunk.php b/src/pocketmine/level/format/Chunk.php index 68a0c4450..f5611e308 100644 --- a/src/pocketmine/level/format/Chunk.php +++ b/src/pocketmine/level/format/Chunk.php @@ -611,7 +611,7 @@ class Chunk{ * @param 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"); } $this->entities[$entity->getId()] = $entity; diff --git a/src/pocketmine/level/format/io/region/Anvil.php b/src/pocketmine/level/format/io/region/Anvil.php index 9a87d40f6..243f70fcb 100644 --- a/src/pocketmine/level/format/io/region/Anvil.php +++ b/src/pocketmine/level/format/io/region/Anvil.php @@ -71,7 +71,7 @@ class Anvil extends McRegion{ $entities = []; foreach($chunk->getEntities() as $entity){ - if(!($entity instanceof Player) and !$entity->closed){ + if(!($entity instanceof Player) and !$entity->isClosed()){ $entity->saveNBT(); $entities[] = $entity->namedtag; } diff --git a/src/pocketmine/level/format/io/region/McRegion.php b/src/pocketmine/level/format/io/region/McRegion.php index e1bfcb6ac..a0ee72980 100644 --- a/src/pocketmine/level/format/io/region/McRegion.php +++ b/src/pocketmine/level/format/io/region/McRegion.php @@ -89,7 +89,7 @@ class McRegion extends BaseLevelProvider{ $entities = []; foreach($chunk->getEntities() as $entity){ - if(!($entity instanceof Player) and !$entity->closed){ + if(!($entity instanceof Player) and !$entity->isClosed()){ $entity->saveNBT(); $entities[] = $entity->namedtag; } diff --git a/src/pocketmine/level/format/io/region/PMAnvil.php b/src/pocketmine/level/format/io/region/PMAnvil.php index 7e1440484..db65e4073 100644 --- a/src/pocketmine/level/format/io/region/PMAnvil.php +++ b/src/pocketmine/level/format/io/region/PMAnvil.php @@ -74,7 +74,7 @@ class PMAnvil extends Anvil{ $entities = []; foreach($chunk->getEntities() as $entity){ - if(!($entity instanceof Player) and !$entity->closed){ + if(!($entity instanceof Player) and !$entity->isClosed()){ $entity->saveNBT(); $entities[] = $entity->namedtag; }