Is there any way to prevent the death event completely? I've tried `$event->setCancelled()`, but it errors saying that the event isn't cancellable :L Should I instead look into forcing a respawn and then just teleport the player back to the death location, so gives the illusion that it was cancelled? If possible I'd rather find some way to cancel the event or bypass it completely.
Nevermind! Solved it myself. I'll leave this post up in case anyone else has this same issue! PHP: public function onDeath(PlayerDeathEvent $event) { $player = $event->getPlayer(); $player->setHealth($player->getMaxHealth()); // Prevents the death event}
this should be bug sometimes, you need to detect if the player get more damage than he has life in the EntityDamageEvent ^^ Example: PHP: public function onDamage(EntityDamageEvent $event){ if($event->getDamage() >= $event->getPlayer()->getHealth()){ $event->setCancelled(); $event->getPlayer()->setHealth($event->getPlayer()->getMaxHealth()); }}
I have been using my method extensively throughout the testing phase of a plugin I'm making. I have yet to encounter any bug with my method. And I fail to see how your method is any better than mine.
When you think about this logically it's obvious why we can't cancel the player death event. We have the earlier opportunity to prevent the event being called altogether by using our brain power.... EntityDamageEvent! It is called before damage is applied to the player (or any other entity) and is cancellable. PHP: public function onDamage(EntityDamageEvent $event) { $victim = $event->getEntity(); if($event->getFinalDamage() >= $victim->getHealth()) { $event->setCancelled(); // Now the player can't die! // I would advise you set the players health back to full and handle the respawn in a way that won't confuse players (TP to spawn, clear inv, etc) }} Works but very hacky lol You should explain why it works better or provide some evidence that it will work better.
I thought that the event only will be called when a player already died lol here you can see it ^^ https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/Player.php#L3324-L3326
All events are called before the action of an event is actually handled. It would be extremely pointless and resource consuming if the server damaged an entity, called the damage event only to have it be cancelled by a plugin and then have to revert the damage applied to the entity. It only makes sense that events are called before the changes based around the event are actually made, pretty much all events allow plugins to modify the outcome of an event whether it be through cancelling the event altogether or simply modifying one aspect of the event.
In what way? I'm "canceling" (for lack of a better term) the event in the event itself, which is what I wanted. No one has yet to say why handling it in the death event itself is any worse than using the damage event. I much prefer handling it in the actual death event anyway, since to me it feels more organized and "to the point" of what I'm doing.
I know its been solved but i want to try it myself PHP: public function onDamage(EntityDamageEvent $event) {$player = $event->getEntity();if($event->getFinalDamage() >= $player->getHealth()) {$event->setCancelled();$player->teleport(spawnPoint)$player->setHealth($player->getMaxHealth()) }}
It really depends exactly what your plugin is trying to achieve, and how you want it to work with other plugins. You might, for example, want to use your plugin along with other plugins that use the death event (or EntityDamageEvent for that matter), in which case using one or the other might be more appropriate.
This is where this comes in handy: https://forums.pmmp.io/threads/getting-your-priorities-straight.3382/