mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-12 08:49:42 +00:00
EntityDeathEvent: add XP amount API, closes #2690
This commit is contained in:
parent
89c0836047
commit
b8d1eb20b0
@ -2926,7 +2926,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
|||||||
//main inventory and drops the rest on the ground.
|
//main inventory and drops the rest on the ground.
|
||||||
$this->doCloseInventory();
|
$this->doCloseInventory();
|
||||||
|
|
||||||
$ev = new PlayerDeathEvent($this, $this->getDrops());
|
$ev = new PlayerDeathEvent($this, $this->getDrops(), $this->getXpDropAmount());
|
||||||
$ev->call();
|
$ev->call();
|
||||||
|
|
||||||
if(!$ev->getKeepInventory()){
|
if(!$ev->getKeepInventory()){
|
||||||
@ -2943,8 +2943,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: allow this number to be manipulated during PlayerDeathEvent
|
$this->level->dropExperience($this, $ev->getXpDropAmount());
|
||||||
$this->level->dropExperience($this, $this->getXpDropAmount());
|
|
||||||
$this->setXpAndProgress(0, 0);
|
$this->setXpAndProgress(0, 0);
|
||||||
|
|
||||||
if($ev->getDeathMessage() != ""){
|
if($ev->getDeathMessage() != ""){
|
||||||
|
@ -641,15 +641,14 @@ abstract class Living extends Entity implements Damageable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function onDeath() : void{
|
protected function onDeath() : void{
|
||||||
$ev = new EntityDeathEvent($this, $this->getDrops());
|
$ev = new EntityDeathEvent($this, $this->getDrops(), $this->getXpDropAmount());
|
||||||
$ev->call();
|
$ev->call();
|
||||||
foreach($ev->getDrops() as $item){
|
foreach($ev->getDrops() as $item){
|
||||||
$this->getLevel()->dropItem($this, $item);
|
$this->getLevel()->dropItem($this, $item);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: check death conditions (must have been damaged by player < 5 seconds from death)
|
//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, $ev->getXpDropAmount());
|
||||||
$this->level->dropExperience($this, $this->getXpDropAmount());
|
|
||||||
|
|
||||||
$this->startDeathAnimation();
|
$this->startDeathAnimation();
|
||||||
}
|
}
|
||||||
|
@ -29,14 +29,18 @@ use pocketmine\item\Item;
|
|||||||
class EntityDeathEvent extends EntityEvent{
|
class EntityDeathEvent extends EntityEvent{
|
||||||
/** @var Item[] */
|
/** @var Item[] */
|
||||||
private $drops = [];
|
private $drops = [];
|
||||||
|
/** @var int */
|
||||||
|
private $xp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Living $entity
|
* @param Living $entity
|
||||||
* @param Item[] $drops
|
* @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->entity = $entity;
|
||||||
$this->drops = $drops;
|
$this->drops = $drops;
|
||||||
|
$this->xp = $xp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,4 +63,24 @@ class EntityDeathEvent extends EntityEvent{
|
|||||||
public function setDrops(array $drops) : void{
|
public function setDrops(array $drops) : void{
|
||||||
$this->drops = $drops;
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user