From 0fabc0c1990f1b7cd9718a96c9594db4d953dd5a Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 21 Mar 2019 19:53:14 +0000 Subject: [PATCH] backport b8d1eb20b: EntityDeathEvent: add XP amount API, closes #2690 --- src/pocketmine/Player.php | 5 ++-- src/pocketmine/entity/Living.php | 5 ++-- .../event/entity/EntityDeathEvent.php | 26 ++++++++++++++++++- .../event/player/PlayerDeathEvent.php | 5 ++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 8aeb09f79..c722aba87 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -3665,7 +3665,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ //main inventory and drops the rest on the ground. $this->doCloseInventory(); - $ev = new PlayerDeathEvent($this, $this->getDrops()); + $ev = new PlayerDeathEvent($this, $this->getDrops(), null, $this->getXpDropAmount()); $ev->call(); if(!$ev->getKeepInventory()){ @@ -3682,8 +3682,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - //TODO: allow this number to be manipulated during PlayerDeathEvent - $this->level->dropExperience($this, $this->getXpDropAmount()); + $this->level->dropExperience($this, $ev->getXpDropAmount()); $this->setXpAndProgress(0, 0); if($ev->getDeathMessage() != ""){ diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index c9030152f..9e0b428af 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -639,15 +639,14 @@ abstract class Living extends Entity implements Damageable{ } protected function onDeath() : void{ - $ev = new EntityDeathEvent($this, $this->getDrops()); + $ev = new EntityDeathEvent($this, $this->getDrops(), $this->getXpDropAmount()); $ev->call(); foreach($ev->getDrops() as $item){ $this->getLevel()->dropItem($this, $item); } //TODO: check death conditions (must have been damaged by player < 5 seconds from death) - //TODO: allow this number to be manipulated during EntityDeathEvent - $this->level->dropExperience($this, $this->getXpDropAmount()); + $this->level->dropExperience($this, $ev->getXpDropAmount()); } protected function onDeathUpdate(int $tickDiff) : bool{ diff --git a/src/pocketmine/event/entity/EntityDeathEvent.php b/src/pocketmine/event/entity/EntityDeathEvent.php index c4ca6111d..aeb7e5574 100644 --- a/src/pocketmine/event/entity/EntityDeathEvent.php +++ b/src/pocketmine/event/entity/EntityDeathEvent.php @@ -29,14 +29,18 @@ use pocketmine\item\Item; class EntityDeathEvent extends EntityEvent{ /** @var Item[] */ private $drops = []; + /** @var int */ + private $xp; /** * @param Living $entity * @param Item[] $drops + * @param int $xp */ - public function __construct(Living $entity, array $drops = []){ + public function __construct(Living $entity, array $drops = [], int $xp = 0){ $this->entity = $entity; $this->drops = $drops; + $this->xp = $xp; } /** @@ -59,4 +63,24 @@ class EntityDeathEvent extends EntityEvent{ public function setDrops(array $drops) : void{ $this->drops = $drops; } + + /** + * Returns how much experience is dropped due to this entity's death. + * @return int + */ + public function getXpDropAmount() : int{ + return $this->xp; + } + + /** + * @param int $xp + * + * @throws \InvalidArgumentException + */ + public function setXpDropAmount(int $xp) : void{ + if($xp < 0){ + throw new \InvalidArgumentException("XP drop amount must not be negative"); + } + $this->xp = $xp; + } } diff --git a/src/pocketmine/event/player/PlayerDeathEvent.php b/src/pocketmine/event/player/PlayerDeathEvent.php index fa93a8ae2..25e8d12ca 100644 --- a/src/pocketmine/event/player/PlayerDeathEvent.php +++ b/src/pocketmine/event/player/PlayerDeathEvent.php @@ -46,9 +46,10 @@ class PlayerDeathEvent extends EntityDeathEvent{ * @param Player $entity * @param Item[] $drops * @param string|TextContainer|null $deathMessage Null will cause the default vanilla message to be used + * @param int $xp */ - public function __construct(Player $entity, array $drops, $deathMessage = null){ - parent::__construct($entity, $drops); + public function __construct(Player $entity, array $drops, $deathMessage = null, int $xp = 0){ + parent::__construct($entity, $drops, $xp); $this->deathMessage = $deathMessage ?? self::deriveMessage($entity->getDisplayName(), $entity->getLastDamageCause()); }