Cleaned up Entity->close() handling

This commit is contained in:
Dylan K. Taylor 2019-04-18 17:23:48 +01:00
parent cc01dfe8df
commit 5913d5038b
4 changed files with 61 additions and 50 deletions

View File

@ -2539,21 +2539,20 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
$this->flagForDespawn();
}
final public function close() : void{
if(!$this->closed){
$this->disconnect("Player destroyed");
$this->networkSession = null;
protected function onDispose() : void{
$this->disconnect("Player destroyed");
$this->cursorInventory->removeAllViewers(true);
$this->craftingGrid->removeAllViewers(true);
parent::onDispose();
}
$this->cursorInventory = null;
$this->craftingGrid = null;
$this->spawned = false;
$this->spawnPosition = null;
$this->perm = null;
parent::close();
}
protected function destroyCycles() : void{
$this->networkSession = null;
$this->cursorInventory = null;
$this->craftingGrid = null;
$this->spawnPosition = null;
$this->perm = null;
parent::destroyCycles();
}
public function __debugInfo(){

View File

@ -1897,28 +1897,42 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
*
* WARNING: Entities are unusable after this has been executed!
*/
public function close() : void{
final public function close() : void{
if(!$this->closed){
(new EntityDespawnEvent($this))->call();
$this->closed = true;
(new EntityDespawnEvent($this))->call();
$this->despawnFromAll();
$this->hasSpawned = [];
if($this->chunk !== null){
$this->chunk->removeEntity($this);
$this->chunk = null;
}
if($this->isValid()){
$this->level->removeEntity($this);
$this->setLevel(null);
}
$this->lastDamageCause = null;
$this->onDispose();
$this->destroyCycles();
}
}
/**
* Called when the entity is disposed to clean up things like viewers. This SHOULD NOT destroy internal state,
* because it may be needed by descendent classes.
*/
protected function onDispose() : void{
$this->despawnFromAll();
if($this->chunk !== null){
$this->chunk->removeEntity($this);
}
if($this->isValid()){
$this->level->removeEntity($this);
}
}
/**
* Called when the entity is disposed, after all events have been fired. This should be used to perform destructive
* circular object references and things which could impact memory usage.
*
* It is expected that the object is unusable after this is called.
*/
protected function destroyCycles() : void{
$this->chunk = null;
$this->setLevel(null);
$this->lastDamageCause = null;
}
/**
* @param int $propertyId
* @param int $flagId

View File

@ -883,18 +883,16 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
}
}
public function close() : void{
if(!$this->closed){
if($this->inventory !== null){
$this->inventory->removeAllViewers(true);
$this->inventory = null;
}
if($this->enderChestInventory !== null){
$this->enderChestInventory->removeAllViewers(true);
$this->enderChestInventory = null;
}
parent::close();
}
protected function onDispose() : void{
$this->inventory->removeAllViewers(true);
$this->enderChestInventory->removeAllViewers(true);
parent::onDispose();
}
protected function destroyCycles() : void{
$this->inventory = null;
$this->enderChestInventory = null;
parent::destroyCycles();
}
/**

View File

@ -926,13 +926,13 @@ abstract class Living extends Entity implements Damageable{
$this->armorInventory->sendContents($player);
}
public function close() : void{
if(!$this->closed){
if($this->armorInventory !== null){
$this->armorInventory->removeAllViewers(true);
$this->armorInventory = null;
}
parent::close();
}
protected function onDispose() : void{
$this->armorInventory->removeAllViewers(true);
parent::onDispose();
}
protected function destroyCycles() : void{
$this->armorInventory = null;
parent::destroyCycles();
}
}