Cleanup Entity age handling, fixed arrows despawning too quickly after long flight

This commit is contained in:
Dylan K. Taylor 2018-09-08 14:23:06 +01:00
parent a3b78236eb
commit c7d58db7eb
9 changed files with 39 additions and 35 deletions

View File

@ -408,8 +408,6 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
public $boundingBox; public $boundingBox;
/** @var bool */ /** @var bool */
public $onGround; public $onGround;
/** @var int */
protected $age = 0;
/** @var float */ /** @var float */
public $eyeHeight = null; public $eyeHeight = null;
@ -1027,7 +1025,6 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
} }
} }
$this->age += $tickDiff;
$this->ticksLived += $tickDiff; $this->ticksLived += $tickDiff;
return $hasUpdate; return $hasUpdate;

View File

@ -88,6 +88,9 @@ class ExperienceOrb extends Entity{
public $gravity = 0.04; public $gravity = 0.04;
public $drag = 0.02; public $drag = 0.02;
/** @var int */
protected $age = 0;
/** /**
* @var int * @var int
* Ticker used for determining interval in which to look for new target players. * Ticker used for determining interval in which to look for new target players.
@ -159,6 +162,7 @@ class ExperienceOrb extends Entity{
public function entityBaseTick(int $tickDiff = 1) : bool{ public function entityBaseTick(int $tickDiff = 1) : bool{
$hasUpdate = parent::entityBaseTick($tickDiff); $hasUpdate = parent::entityBaseTick($tickDiff);
$this->age += $tickDiff;
if($this->age > 6000){ if($this->age > 6000){
$this->flagForDespawn(); $this->flagForDespawn();
return true; return true;

View File

@ -53,6 +53,9 @@ class ItemEntity extends Entity{
public $canCollide = false; public $canCollide = false;
/** @var int */
protected $age = 0;
protected function initEntity() : void{ protected function initEntity() : void{
parent::initEntity(); parent::initEntity();
@ -82,14 +85,13 @@ class ItemEntity extends Entity{
$hasUpdate = parent::entityBaseTick($tickDiff); $hasUpdate = parent::entityBaseTick($tickDiff);
if(!$this->isFlaggedForDespawn()){ if(!$this->isFlaggedForDespawn() and $this->pickupDelay > -1 and $this->pickupDelay < 32767){ //Infinite delay
if($this->pickupDelay > 0 and $this->pickupDelay < 32767){ //Infinite delay
$this->pickupDelay -= $tickDiff; $this->pickupDelay -= $tickDiff;
if($this->pickupDelay < 0){ if($this->pickupDelay < 0){
$this->pickupDelay = 0; $this->pickupDelay = 0;
} }
}
$this->age += $tickDiff;
if($this->age > 6000){ if($this->age > 6000){
$this->server->getPluginManager()->callEvent($ev = new ItemDespawnEvent($this)); $this->server->getPluginManager()->callEvent($ev = new ItemDespawnEvent($this));
if($ev->isCancelled()){ if($ev->isCancelled()){
@ -99,7 +101,6 @@ class ItemEntity extends Entity{
$hasUpdate = true; $hasUpdate = true;
} }
} }
} }
return $hasUpdate; return $hasUpdate;

View File

@ -48,11 +48,24 @@ class Arrow extends Projectile{
protected $damage = 2; protected $damage = 2;
/** @var int */
protected $collideTicks = 0;
public function __construct(Level $level, CompoundTag $nbt, ?Entity $shootingEntity = null, bool $critical = false){ public function __construct(Level $level, CompoundTag $nbt, ?Entity $shootingEntity = null, bool $critical = false){
parent::__construct($level, $nbt, $shootingEntity); parent::__construct($level, $nbt, $shootingEntity);
$this->setCritical($critical); $this->setCritical($critical);
} }
protected function initEntity() : void{
parent::initEntity();
$this->collideTicks = $this->namedtag->getShort("life", $this->collideTicks);
}
public function saveNBT() : void{
parent::saveNBT();
$this->namedtag->setShort("life", $this->collideTicks);
}
public function isCritical() : bool{ public function isCritical() : bool{
return $this->getGenericFlag(self::DATA_FLAG_CRITICAL); return $this->getGenericFlag(self::DATA_FLAG_CRITICAL);
} }
@ -77,10 +90,15 @@ class Arrow extends Projectile{
$hasUpdate = parent::entityBaseTick($tickDiff); $hasUpdate = parent::entityBaseTick($tickDiff);
if($this->age > 1200){ if($this->isCollided){
$this->collideTicks += $tickDiff;
if($this->collideTicks > 1200){
$this->flagForDespawn(); $this->flagForDespawn();
$hasUpdate = true; $hasUpdate = true;
} }
}else{
$this->collideTicks = 0;
}
return $hasUpdate; return $hasUpdate;
} }

View File

@ -66,7 +66,5 @@ class EnderPearl extends Throwable{
$owner->attack(new EntityDamageEvent($owner, EntityDamageEvent::CAUSE_FALL, 5)); $owner->attack(new EntityDamageEvent($owner, EntityDamageEvent::CAUSE_FALL, 5));
} }
$this->flagForDespawn();
} }
} }

View File

@ -42,7 +42,5 @@ class ExperienceBottle extends Throwable{
$this->level->broadcastLevelSoundEvent($this, LevelSoundEventPacket::SOUND_GLASS); $this->level->broadcastLevelSoundEvent($this, LevelSoundEventPacket::SOUND_GLASS);
$this->level->dropExperience($this, mt_rand(3, 11)); $this->level->dropExperience($this, mt_rand(3, 11));
$this->flagForDespawn();
} }
} }

View File

@ -73,7 +73,6 @@ abstract class Projectile extends Entity{
$this->setMaxHealth(1); $this->setMaxHealth(1);
$this->setHealth(1); $this->setHealth(1);
$this->age = $this->namedtag->getShort("Age", $this->age);
do{ do{
$blockHit = null; $blockHit = null;
@ -123,8 +122,6 @@ abstract class Projectile extends Entity{
public function saveNBT() : void{ public function saveNBT() : void{
parent::saveNBT(); parent::saveNBT();
$this->namedtag->setShort("Age", $this->age);
if($this->blockHit !== null){ if($this->blockHit !== null){
$this->namedtag->setInt("tileX", $this->blockHit->x); $this->namedtag->setInt("tileX", $this->blockHit->x);
$this->namedtag->setInt("tileY", $this->blockHit->y); $this->namedtag->setInt("tileY", $this->blockHit->y);

View File

@ -124,8 +124,6 @@ class SplashPotion extends Throwable{
} }
} }
} }
$this->flagForDespawn();
} }
/** /**

View File

@ -23,6 +23,9 @@ declare(strict_types=1);
namespace pocketmine\entity\projectile; namespace pocketmine\entity\projectile;
use pocketmine\block\Block;
use pocketmine\math\RayTraceResult;
abstract class Throwable extends Projectile{ abstract class Throwable extends Projectile{
public $width = 0.25; public $width = 0.25;
@ -31,18 +34,8 @@ abstract class Throwable extends Projectile{
protected $gravity = 0.03; protected $gravity = 0.03;
protected $drag = 0.01; protected $drag = 0.01;
public function entityBaseTick(int $tickDiff = 1) : bool{ protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
if($this->closed){ parent::onHitBlock($blockHit, $hitResult);
return false;
}
$hasUpdate = parent::entityBaseTick($tickDiff);
if($this->age > 1200 or $this->isCollided){
$this->flagForDespawn(); $this->flagForDespawn();
$hasUpdate = true;
}
return $hasUpdate;
} }
} }