Player Kill Player Event

Discussion in 'Plugin Development' started by theone15247, May 12, 2012.

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

    theone15247

    For my plugin I need an event the detects when a player kills another player. I have

    Code:
        @EventHandler
        public void onEntityDamagedByEntity(EntityDamageByEntityEvent e) {
            if (e.getDamager() instanceof Player) {
                Player killer = (Player) e.getDamager();
                if (e.getEntityType() == EntityType.PLAYER) {
                    Player killed = (Player) e.getEntity();
                    if (killed.isDead() || killed.getHealth() <= 0) {
                        log.info("A player killed another player"); // never reaches here
                    }
                }
            }
        }
    does anyone know why it doesn't work?

    Thanks
     
  2. Offline

    Nitnelave

    What doesn't work? do you get an error, or does it just not show anything? If it doesn't show anything, you might want to display a message at every level to understand where it screws up
     
  3. Offline

    theone15247

    No error, just nothing shows up. I'll try adding more messages
     
  4. Offline

    Monowii

    This for the event Listener
    Code:java
    1. @EventHandler
    2. public void onKill(PlayerDeathEvent e)
    3. {
    4. String killed = e.getEntity().getName();
    5. String killer = e.getEntity().getKiller().getName();
    6. e.setDeathMessage(ChatColor.RED + killed + " has been slain by " + killer);
    7. }


    And be sure you have register event onEnable() with :
    Code:java
    1. getServer().getPluginManager().registerEvents(this, this);
     
  5. Offline

    colony88

    Shouldn't you use EntityDeathEvent? As I recall, the PlayerDeathEvent doesn't work...
     
  6. Offline

    Monowii

    PlayerDeathEvent Work perfectly on bukkit 1.2.5-R1.0
     
  7. Offline

    theone15247

    thanks monowii I didn't know playerdeathevent worked :D
     
  8. Offline

    colony88

    Good to know :p
     
  9. Offline

    theone15247

    Here is the code I ended up using the the playerdeathevent

    Code:
            if (e.getEntity().getKiller() != null) {
                String killedName = e.getEntity().getName();
                if (tributes.containsKey(killedName)) {
                    String killerName = e.getEntity().getKiller().getName();
                    log.info(ChatColor.RED + killedName + " has been slain by " + killerName);
                }
            }
     
  10. Offline

    CorrieKay

    Just to explain why your original code didn't work, Events fire before the event happens. This allows plugins to modify the event to their liking, so technically, if a player is dealt fatal damage at this point, your event is firing before the damage is applied, and the player is not dead yet ^ ^
     
  11. Offline

    SparkleHoof

    question, is there a way i could extend on this playerdeathevent thing to somehow include what the player was killed with? is there an event extention for that?
     
  12. Offline

    Woobie

    Try adding
    Code:
    + killerName().getInventory().getItemInHand()); 
    To the message.
    So for example:
    Code:
    Bukkit.getServer().broadcastMessage(killedName + " has been slain by " + killerName + " with " + killerName().getInventory().getItemInHand()));
     
  13. Offline

    CorrieKay

    an idea i had expanded on was to log all of the deaths on my server, in detail, of how it happened.
    It really helps when we have people who come and murder animal farms on our server. Check it out!
     
Thread Status:
Not open for further replies.

Share This Page