How can i do that? PHP: public function onDeath(PlayerDeathEvent $event){} This is what i already coded, but i cant find getDamager()
If the player isn't killed by another entity this won't work and will throw an error. You have to make sure that the player was last damaged by an Entity: PHP: public function onDeath(pocketmine\event\player\PlayerDeathEvent $event) { if($event->getPlayer()->getLastDamageCause() instanceof pocketmine\event\entity\EntityDamageByEntityEvent) { if($event->getPlayer()->getLastDamageCause()->getDamager() instanceof pocketmine\Player) { $event->getPlayer()->getLastDamageCause()->getDamager()->sendMessage("You killed " . $event->getPlayer()->getName() . "!"); } }}
If you have other questions like this, try to look through the source code to find your answer. The server has a default death message so you could have tried to look for that and see how they get the two player names. Im not saying dont post on here, just make sure you do research first and dont always rely on the forums
The death event doesn't have a damager. The death event only has an entity. And that entity may have a reason of death, or no. And the reason of death may be related to another entity, or no. And the other entity may be a player, or no. And the player may still be online, or no.
Found a Solution PHP: public function onPlayerDeath(PlayerDeathEvent $event){ $player = $event->getPlayer(); $cid = $player->getLastDamageCause()->getCause(); $killedby = $player->getLastDamageCause()->getEntity()->getNameTag(); switch($cid){ case EntityDamageEvent::CAUSE_ENTITY_ATTACK: $this->getServer()->broadcastMessage("Du wurdest von $killedby getötet"); break; } } Thanks dudes
I already reminded you so many checks you have to make... Also, please post in English on this website. What you post aren't for one person only - if they are, please use conversations.
Okay, ill post in English next time. Ive noticed that i only have the Name of the Damager, but not the Damager itself, like $event->getPlayer is. I need to change the items in the inventory of the killer, but i dont know how, because there isnt getDamager(). Sorry for my bad English, im from Switzerland.
The last damage cause's entity is the player is the $player itself! Check if getLastDamageCause() is null! If the player suicides using /kill or a plugin uses kill() directly, there would be no damage cause. Check if getLastDamageCause() is an EntityDamageByEntityEvent! Check if the EntityDamageByEntityEvent->getDamager() is a Player object or just another entity! Check if the player isOnline(). (I'm confirming if this is needed; this may be a vulnerability to intentional malicious memory leaks)
Yep. getLastDamageCause() returns an EntityDamageEvent The entity in the EntityDamageEvent is obviously the entity that is damaged. There only exists a damager if this EntityDamageEvent is an EntityDamageByEntityEvent.
I recoded the last code into the EntityDamageByEntityEvent, but getDamager() is an Entity, i need a Player :/
A player is an entity. Just use a check ( PHP: if($damager instanceof Player) { ) to see if it is a player.
If you did it correctly you can access the inventory. Just use $damager->getInventory() after you performed the check if it is a player.