1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

[SOLVED] Cancel the death event?

Discussion in 'Development' started by Redux, Feb 4, 2017.

  1. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    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.
     
  2. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    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
    }
     
    Last edited: Feb 4, 2017
  3. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    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());
        }
    }
     
    DaisukeDaisuke likes this.
  4. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    why should it bug?
     
  5. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    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.
     
    OnTheVerge likes this.
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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.
     
  7. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    Last edited: Feb 15, 2017
  8. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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.
     
  9. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    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.
     
  10. Indexfire

    Indexfire Baby Zombie

    Messages:
    137
    GitHub:
    Indexfire
    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())

       }
    }
     
  11. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    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.
     
  12. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    That's a good point. I hadn't thought about that one.
     
  13. KittyDev

    KittyDev Slime

    Messages:
    96
    GitHub:
    FreakingDev
    If you prevent it. Does it count the death if i use other plugin?
     
  14. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.