Fixed possible memory leak with projectiles, use owner/target metadata

This commit is contained in:
Dylan K. Taylor
2017-05-07 16:27:58 +01:00
parent c383c7b0dd
commit 4ccd955647
3 changed files with 80 additions and 14 deletions

View File

@ -36,16 +36,13 @@ abstract class Projectile extends Entity{
const DATA_SHOOTER_ID = 17;
/** @var Entity */
public $shootingEntity = null;
protected $damage = 0;
public $hadCollision = false;
public function __construct(Level $level, CompoundTag $nbt, Entity $shootingEntity = null){
$this->shootingEntity = $shootingEntity;
if($shootingEntity !== null){
$this->setDataProperty(self::DATA_SHOOTER_ID, self::DATA_TYPE_LONG, $shootingEntity->getId());
$this->setOwningEntity($shootingEntity);
}
parent::__construct($level, $nbt);
}
@ -83,10 +80,10 @@ abstract class Projectile extends Entity{
$damage = $this->getResultDamage();
if($this->shootingEntity === null){
if($this->getOwningEntity() === null){
$ev = new EntityDamageByEntityEvent($this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
}else{
$ev = new EntityDamageByChildEntityEvent($this->shootingEntity, $this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
$ev = new EntityDamageByChildEntityEvent($this->getOwningEntity(), $this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
}
$entity->attack($ev->getFinalDamage(), $ev);
@ -140,7 +137,7 @@ abstract class Projectile extends Entity{
foreach($list as $entity){
if(/*!$entity->canCollideWith($this) or */
($entity === $this->shootingEntity and $this->ticksLived < 5)
($entity->getId() === $this->getOwningEntityId() and $this->ticksLived < 5)
){
continue;
}