PlayerDeathEvent

Discussion in 'Plugin Development' started by HeadGam3z, Jun 30, 2014.

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

    HeadGam3z

    I'm using a PlayerDeathEvent to determine if the killer is a player, and if so, send a message; which works sorta OK.

    The problem is, however, when I hit the player, and a zombie hits the player and gets the kill, it still sends the killed a message that he died by me, which it shouldn't since it died by the zombie, right?

    I checked if the killer is an instanceof Player, but it ignores that when I hit the player once, then a zombie gets the kill.

    Is there anything I can do to work around this?

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. Player p = (Player) e.getEntity();
    4. if (e.getEntity().getKiller() instanceof Player) {
    5. p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig()
    6. .getString("Settings.DeathMessage")));
    7. }
    8. }
     
  2. Offline

    ZanderMan9

    Move Player p = etc... inside of your if block. I don't think that's what's causing your issue, but there's no need to create that bit of data if you aren't going to use it.
     
  3. Offline

    HeadGam3z

    ZanderMan9
    Doesn't matter, just a preference.
     
  4. Offline

    ZanderMan9

    I suppose if you are going to add onto this, then yes. Maybe there is another method besides getKiller that you need to use? It might default to the player that attacked it...
     
  5. Offline

    unrealdesign

    If you look at the javadocs, getKiller() returns player.
     
  6. Offline

    HeadGam3z

    unrealdesign
    Yes, and if a zombie killed it, it should return null, right? But it's not, it's still returning as if a player killed the player, but the zombie killed the player.
     
  7. Offline

    unrealdesign

    No? Think about it. If you made the code, you would set the killer to the last person that hit the player. Since getKiller() is not part of the event, it's part of a player instance. The only time the killer would be null is if a player never hit that player, or maybe if he reconnects after being hit, I'm not sure about that part. So to re-iterate: since it isn't part of the event and can't change every time a player gets hit, it just stays with the Player to hit that player. At least, this is how I'm interpreting the code without actually going through it.
     
    bonecrusher238 likes this.
  8. Offline

    3Shawnster

    You could use the EntityDamageByEntityEvent.
    Code:java
    1. public void onEntityDamageByEntity(EntityDamageByEntityEvent e)
    2. {
    3.  
    4. /** Doesn't do anything if either entities aren't players. **/
    5. if(!(e.getEntity() instanceof Player) && !(e.getDamager() instanceof Player))
    6. return;
    7.  
    8. Player p = (Player) e.getEntity();
    9. Player damager = (Player) e.getDamager();
    10.  
    11. /** getHealth() is ambigious, using Damageable fixes that. **/
    12. Damageable d = (Damageable) p;
    13.  
    14. /** If the player dies, again note that this code only works if an entity (player) damaged the target. **/
    15. if(d.getHealth() - e.getDamage() <= 0)
    16. {
    17. p.sendMessage(ChatColor.translateAlternateColorCodes('&', Main.pl.getString("Settings.DeathMessage")));
    18. }
    19.  
    20. }
     
  9. Offline

    AoH_Ruthless

    3Shawnster
    To fix the ambigious health, you should be building against Bukkit; building against Craftbukkit is not endorsed and is not recommended.
     
  10. Offline

    bonecrusher238

    unrealdesign Is correct it would find the player that last killed him
     
  11. Offline

    unrealdesign

    Javadocs doesn't say and I've not tested it. Could bet last Player hit, or yes, last player killer.
     
  12. Offline

    Heirteir

    HeadGam3z
    You can do
    Code:java
    1. e.getPlayer().getLastDamageCause().getEntity()

    and that will give you a more accurate answer.
     
Thread Status:
Not open for further replies.

Share This Page