Here is a snippet code of what im trying to make: Code: public function onPlayerDeath(EntityDeathEvent $event){ $target = $event->getEntity(); $targeter = $event->getEntity()->getLastDamageCause()->getDamager(); if($targeter instanceof Player){ $targetter->sendMessage("You killed " . $target); }elseif($target instanceof Player){ $target->sendMessage("You were killed by ".$targetter); } } Im not sure if $target and $targetter is right but it doesnt work for me. Any help is appreciated. Thanks.
You'd want $targetter->getName() and $target->getName() for the "Send Message" part. I'm curious if this code would work, because it's better than what I was going to do in my code.
PHP: // Wouldn't you want to stop at getLastDamageCause() ?$targeter = $target->getLastDamageCause()->getName()
Actually you would probably want to do the "getName()" after the "if" statement that establishes whether or not it is an instanceof Player.
No you don't. Check the function's return type. It can be an EntityDamageEvent instance or an integer.
No. $entity->getLastDamageCause() can return an int or an instance of EntityDamageEvent. You have to check that yourself.
PHP: public function onPlayerDeath(EntityDeathEvent $event){ $target = $event->getEntity(); $targeter = $target->getLastDamageCause(); if($targeter instanceof Player){ $targetter->sendMessage("You killed " . $target); }elseif($target instanceof Player){ $target->sendMessage("You were killed by ".$targetter); }}
PHP: public function onDeath(EntityDeathEvent $event){ $victim = $event->getEntity(); $cause = $victim->getLastDamageCause(); if($cause instanceof EntityDamageEvent){ $this->send("$victim killed by " . $cause->getEntity() . "!"); } else{ $this->send("$victim died because of cause ID: $cause"); // $cause is an integer $this->send("Text for $cause: " . $this->getCauseDescription($cause)); }}private function getCauseDescription($in){ foreach((new \ReflectionClass("pocketmine\\event\\entity\\EntityDamageEvent"))->getConstants() as $name => $id){ if($id === $in){ return $name; } } return "unknown";} Note that the __toString() method for a Player now is just Player (eid), which does not display the player name. Use $player->getName() instead, but this function doesn't exist if it is not a Player object.
Correct. However, if entity AI is implemented, it could be a zombie which doesn't have a name. Edit: I meant, both are wrong, but at least mine won't crash.
Haha, okay. Well if you want to get the name of the killer you could always use the damage event, and get the name of the player, and save the most recent damagers name.
This is the strategy I was originally going to take as well. We are working on the exact same problem! Someone should write a plugin for this! I was thinking the "EntityDamageByEntityEvent" would be useful...
$target->getLastDamageCause() works?? Why did I spend all morning writing a function that records when someone gets hurt and who by, for my plugin??