Player Kill Player Event

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

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


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

        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) {
              "A player killed another player"); // never reaches here
    does anyone know why it doesn't work?

  2. Offline


    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


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


    This for the event Listener
    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 :
    1. getServer().getPluginManager().registerEvents(this, this);
  5. Offline


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


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


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


    Good to know :p
  9. Offline


    Here is the code I ended up using the the playerdeathevent

            if (e.getEntity().getKiller() != null) {
                String killedName = e.getEntity().getName();
                if (tributes.containsKey(killedName)) {
                    String killerName = e.getEntity().getKiller().getName();
           + killedName + " has been slain by " + killerName);
  10. Offline


    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


    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


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


    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