Player Death Event

Discussion in 'Plugin Development' started by gamer9726, Jul 30, 2013.

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

    gamer9726

    In my plugin, I'd like something to happen whenever a player is killed by a zombie. Here are some snippets of my code:

    My onEnable:
    Code:java
    1. @Override
    2. public void onEnable(){
    3. getLogger().info("TAC Plagues has been enabled.");
    4. PluginManager manager = getServer().getPluginManager();
    5. manager.registerEvents(this, this);
    6. }

    The actual event part:
    Code:java
    1. @EventHandler
    2. public void onzombiedeath(PlayerDeathEvent event){
    3. Player player = event.getEntity();
    4. Entity killer = player.getKiller();
    5. if(killer instanceof Zombie){
    6. Bukkit.broadcastMessage(ChatColor.AQUA + "Testing Testing 123"); //For debug purposes
    7. }
    8. }

    I've tried testing this by letting myself get killed by a zombie, but whenever I die nothing is broadcasting. Can I get some help?

    Edit: And yes, the class implements listener. I have another listener working just fine.
     
  2. Offline

    xxMOxMOxx

    gamer9726
    I think you want (Killer instanceof EntityType.Zombie)
     
  3. Offline

    EcMiner

    That won't work, cause killer is an entity not an entitytype
     
  4. Offline

    The_Doctor_123

    Not sure if it makes a difference, but getKiller() returns a LivingEntity.
     
  5. Offline

    xTrollxDudex

  6. Offline

    gamer9726

    xTrollxDudex

    Ah, yes, I forgot to mention that. Yes, the class does implement Listener.

    And also,
    The zombie would count as a living entity, right?
     
  7. Offline

    Jakpok

    Yup, every mob and every player is Living Entity.
     
  8. Offline

    gamer9726

    Jakpok

    Yeah, that's what I thought. That means I'm still stumped :/. My other listener for PlayerJoinEvent worked fine so I'm pretty sure that it is something from within the onzombiedeath method messing everything up. I'm just not sure what it is.
     
  9. Offline

    iFamasssxD

    You shouldnt really cast the killer to Entity before checking. If the player dies from fire or falling it will throw a NPE.
    Code:
     @EventHandler
    public void onzombiedeath(PlayerDeathEvent event){
    Player player = event.getEntity();
    if(event.getKiller() instanceof Zombie){
    Bukkit.broadcastMessage(ChatColor.AQUA + "Testing Testing 123"); //For debug purposes
    }
    }
     
  10. Offline

    xxMOxMOxx

    Run an if first to make sure it is indeed an entity
     
  11. Offline

    gamer9726

    iFamasssxD

    I changed it to player.getKiller() (I couldn't do event.getKiller(), I'm not sure if that was a typo or not.) Still no luck.
    I'm starting to get pretty confused now. I can post all of my code here; I'm not sure how much it will help since I don't really reference this part anywhere else yet. As far as I know, only the onEnable and actual method part matter. I have reread my code a few times, but any additional help would be appreciated.

    Here is the method now:
    Code:java
    1. @EventHandler
    2. public void onZombieKill(PlayerDeathEvent event){
    3. Player player = event.getEntity();
    4. if(player.getKiller() instanceof Zombie){
    5. Bukkit.broadcastMessage(ChatColor.AQUA + "Testing Testing 123");
    6. }
    7. }
     
  12. Offline

    EcMiner

    they way this won't work is because player.getKiller() returns a Player not an entity, i thought that there used to be a .getKiller() method but that seemed to be disappeared
     
  13. Offline

    Awesomeman2

    Can you put the code up for how you pit the listener in?
     
  14. Offline

    Compressions

    gamer9726 You will want to use:
    Code:
    event.getEntity().getKiller();
    to retrieve the killer involved in the event. Do check if the killer is null also.
     
  15. Offline

    EcMiner

    that won't work, as i said getKiller() returns a player
     
  16. Offline

    Awesomeman2

    EcMiner
    I think that should work, but what do you suggest?
     
  17. Offline

    Tarestudio

    gamer9726
    You could check the Deathmessage for containing Zombie
     
  18. Offline

    Compressions

    EcMiner Then you would use EntityDamageByEntityEvent and check if the damager kills the player.
     
  19. Offline

    EcMiner


    It won't work, and i have no suggestions i have tried a lot of methods, also checking with EntityDamageByEntityEvent, but it failed every time, for some reason when i tried to broadcast who killedthe player it keep saying EntityType: PLAYER


    Checked that to, i did if(event.getEntityType == EntityType.PLAYER && event.getDamager.getType() == EntityType.ZOMBIE && event.getEntity().isDead()) it still doesn't work. For some reason this seems impossible, but it isn't cause DeathTPplus also has something like this, and Bukkit can also broadcast the Entity that killed him

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  20. Offline

    Compressions

    EcMiner Hmm. Odd. What if you did instanceof checks like so:
    Code:
    if(e.getDamager() instanceof Zombie) {
     
    }
     
  21. Offline

    EcMiner


    That does the same as .getType() == EntityType.ZOMBIE, but the problem is, is that you can't do the .isDead() cause you can't attack someone when he is dead
     
  22. Offline

    Tarestudio

    EcMiner
    At the moment of getting damage (EntityDamageByEntityEvent), the player is not dead, but you can get player health, the events tells you the damage the player gets -> health - damage < 0 = Player will be dead (if not cancelled). because other listener could cancel this event, and you just want to react to the event, not change it at this point, register with priority monitor.
     
  23. Offline

    EcMiner


    You are right, i will test this right now
     
  24. Offline

    Compressions

    EcMiner Isn't that what I said lol...
     
  25. Offline

    EcMiner

    Thanks to Tarestudio it finally worked, here is the code:

    Code:java
    1. @EventHandler
    2. public void onzombiedeath(EntityDamageByEntityEvent event) {
    3. if (event.getEntity().getType() == EntityType.PLAYER && event.getDamager().getType() == EntityType.ZOMBIE) {
    4. Player p = (Player) event.getEntity();
    5. Damageable dmg = p;
    6. if (dmg.getHealth() - event.getDamage() <= 0D) {
    7. Bukkit.broadcastMessage("┬žbTest");
    8. }
    9. }
    10.  
    11. }



    Nope that's not what you said you said

    XD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  26. Offline

    Tarestudio

    EcMiner
    You still should check if the event was cancelled
     
  27. Offline

    EcMiner

    The one who made this post can do it, i've done enough here :p
     
  28. Offline

    gamer9726

    EcMiner

    Thank you, that worked!

    Also, on a slightly related note, is there a way to stop potion effects from leaving on death without messing around with their duration?
     
  29. Offline

    Garris0n

    Another way to do this(in my opinion cleaner) would be to use .getLastDamageCause() on the player, which returns their last damage event, and use that to get the killer. This way you guarantee that the player is dead(something else could potentially control the damage event otherwise, unless you're on monitor).
     
  30. Offline

    EcMiner


    I have tried this, but for some odd reason it didn't work, when i broadcasted the EntityType of the killer it said PLAYER event though it was a zombie
     
Thread Status:
Not open for further replies.

Share This Page