Added PlayerDeathEvent->setKeepXp(), closes #4002 (#4015)

This commit is contained in:
TheNewHEROBRINEX 2022-05-21 17:41:14 +02:00 committed by GitHub
parent a38a5c67f1
commit 02cf6ae46c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -40,6 +40,7 @@ class PlayerDeathEvent extends EntityDeathEvent{
private Translatable|string $deathMessage; private Translatable|string $deathMessage;
private bool $keepInventory = false; private bool $keepInventory = false;
private bool $keepXp = false;
/** /**
* @param Item[] $drops * @param Item[] $drops
@ -78,6 +79,14 @@ class PlayerDeathEvent extends EntityDeathEvent{
$this->keepInventory = $keepInventory; $this->keepInventory = $keepInventory;
} }
public function getKeepXp() : bool{
return $this->keepXp;
}
public function setKeepXp(bool $keepXp) : void{
$this->keepXp = $keepXp;
}
/** /**
* Returns the vanilla death message for the given death cause. * Returns the vanilla death message for the given death cause.
*/ */

View File

@ -33,6 +33,7 @@ use pocketmine\data\java\GameModeIdMap;
use pocketmine\entity\animation\Animation; use pocketmine\entity\animation\Animation;
use pocketmine\entity\animation\ArmSwingAnimation; use pocketmine\entity\animation\ArmSwingAnimation;
use pocketmine\entity\animation\CriticalHitAnimation; use pocketmine\entity\animation\CriticalHitAnimation;
use pocketmine\entity\Attribute;
use pocketmine\entity\effect\VanillaEffects; use pocketmine\entity\effect\VanillaEffects;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\entity\Human; use pocketmine\entity\Human;
@ -2223,8 +2224,10 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
} }
} }
$this->getWorld()->dropExperience($this->location, $ev->getXpDropAmount()); if(!$ev->getKeepXp()){
$this->xpManager->setXpAndProgress(0, 0.0); $this->getWorld()->dropExperience($this->location, $ev->getXpDropAmount());
$this->xpManager->setXpAndProgress(0, 0.0);
}
if($ev->getDeathMessage() != ""){ if($ev->getDeathMessage() != ""){
$this->server->broadcastMessage($ev->getDeathMessage()); $this->server->broadcastMessage($ev->getDeathMessage());
@ -2285,6 +2288,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->setHealth($this->getMaxHealth()); $this->setHealth($this->getMaxHealth());
foreach($this->attributeMap->getAll() as $attr){ foreach($this->attributeMap->getAll() as $attr){
if($attr->getId() === Attribute::EXPERIENCE || $attr->getId() === Attribute::EXPERIENCE_LEVEL){ //we have already reset both of those if needed when the player died
continue;
}
$attr->resetToDefault(); $attr->resetToDefault();
} }