Okay, so I've been having troubles with this.. I can't get who killed a player :/, this is what I tried: PHP: public function onDamage(EntityDamageByEntityEvent $e){ $p = $e->getEntity(); $d = $p->getDamager(); if($p instanceof Player){ if($d instanceof Player){ switch($p->getHealth()){ case 0: $d->getInventory()->addItem(Item::get(1,0,1)); break; } } }} No luck. Any ideas? Tips? Do you know how to do it? Help appreciated. xD
PHP: public function onDamage(EntityDamageEvent $e){ $p = $e->getEntity(); if($p instanceof Player && $e instanceof EntityDamageByEntityEvent){ $d = $e->getDamager(); if($d instanceof Player){ if($e->getDamage() >= $p->getHealth() && $p->getHealth() > 0){ $d->getInventory()->addItem(Item::get(1,0,1)); } }}}
Well, use PlayerDeathEvent instead EntityDamageEvent. PHP: public function OnDeath(PlayerDeathEvent $event){if(($entity = $event->getEntity()) instanceof Player){$cause = $entity->getLastDamageCause();if($cause instanceof EntityDamageByEntityEvent && ($damager = $cause->getDamager()) instanceof Player){// Now you have the killer and the entity.}}
Right, it is more sensible like that. If you are using EntityDamageEvent for MUST BE WE KNOW YOU WON'T reasons, make sure to handle at priority MONITOR and enable ignoreCancelled.
I did it my way so that players dont need to click the respawn button(this code is not inside what i posted)