Solved Death Event: "best option"

Discussion in 'Plugin Development' started by DoggyCode™, Dec 1, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    DoggyCode™

    So I know there are a lot of events which can determine if a player died or not, but i want to know which one is better in my scenario:

    When a player dies, I would like to know the DamageCause, and if it is ENTITY_ATTACK, then I'd like to figure out what Entity killed the player. I've tried PlayerDeathEvent, and EntityDeathEvent, but i cannot get the entity which killed the player in these two events.
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    MrGeneralQ

    can't you use the e.getPlayer().getKiller() ?
     
  4. Offline

    DoggyCode™

    public void onDie(EntityDamageByEntityEvent event) {
    if(!(event.getEntity() instanceof Player))return;
    if(!(event.getEntity().isDead()))return;
    }

    whenever i kill the player, the isDead returns false. Why? Do I have to wait a tick?

    This would return a Player, I need to return a Entity in order to find out the entity type that killed the player
     
  5. @DoggyCode™
    Yes, you have to wait a tick. Events are fired before the action happens so they are cancellable.
     
  6. Offline

    DoggyCode™

    I guess that's good, but it's sort of unpractical for an event dont u think? to wait ticks? is there no other alt?

    Ok, so i thought this would work, but obviously it didn't. Because the event is only fired once the entity is damaged by another entity, that's NOT what i want. I want it so that I can check the DamageCause of why the player died, so i can determine wheter it's by fall damage or other. This event wouldn't fire if the player died from fall damage. Using two events would possibly be better?

    EDIT:
    I see that PlayerDeathEvent cannot be cancelled, does that mean that this event is fired after?
     
    Last edited: Dec 1, 2016
  7. @DoggyCode™
    No, it doesn't, all events are fired before the action happens (with the exception of a few weird ones). Also, why is it such an inconvenience to just schedule a BukkitRunnable to run? It's 50ms, I promise you no one will notice.

    Also, you should use the EntityDamageEntityEvent, and check if the player will die from it, and then you'll be able to get the killer.
     
  8. Offline

    DoggyCode™

    Yeah, I fixed that. And wroooooooooong, PlayerDeathEvent fires after as it's non cancellable :) just tested
     
  9. @DoggyCode™
    In what manner did you decide that it fires after? If you look at the code, it fires in the middle of the EntityPlayer#die method, which I count as before, since the player is not fully dead yet. (for example, your inventory can still be open while the event fires)
     
  10. Offline

    DoggyCode™

    Called the isDead method


    Sent from my iPhone using Tapatalk
     
  11. @DoggyCode™
    Well, the isDead method is not a universal way to know if the event is fired before or after, is it? I'd still like to argue that it's before, but whatever, it's unimportant.

    Anyways, good that you solved your problem!
     
  12. Offline

    ipodtouch0218

    @DoggyCode™
    I know that you have solved this, but in PlayerDeathEvent and EntityDeathEvent you can use #getLastDamageCause()#getEntity() if you really don't want to schedule a tick.
     
  13. Offline

    DoggyCode™

    returns the entity which hit me? Tested it?
     
  14. Offline

    I Al Istannen

Thread Status:
Not open for further replies.

Share This Page