Solved Get damager from damage event.

Discussion in 'Plugin Development' started by flash110, Jun 2, 2016.

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

    flash110

    Hello guys, it's flash110 (again) and I have another question for the bukkit community!
    I have been working on a cool death plugin that includes instant respawning without using packets, so I will not be using the player death event. I am using the EntityDamageEvent and I can pick and choose between the different causes of damage just fine, but I can't seem to find a way to get the damager name. I have tried to get the name with a player and entity, but no luck. This is what I have so far:
    Code:
        @EventHandler
        public void findDeath(EntityDamageEvent e) {
            if (e.getEntity() instanceof Player) {
                Player p = (Player) e.getEntity();
                Player damager = (Player) p.getLastDamageCause();
                if(e.getDamage() >= p.getHealth()) {
                    if((e.getCause() == DamageCause.FIRE_TICK) || (e.getCause() == DamageCause.FIRE)) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + p.getDisplayName() + " was burnt to a crisp!");
                        p.getPlayer().getLocation().getWorld().playEffect(e.getEntity().getLocation().add(0.0D, 1.0D, 0.0D), Effect.STEP_SOUND, 152);
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setFoodLevel(20);
                    }
                    else if (e.getCause() == DamageCause.FALL) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + p.getDisplayName() + " hit the ground");
                        p.getPlayer().getLocation().getWorld().playEffect(e.getEntity().getLocation().add(0.0D, 1.0D, 0.0D), Effect.STEP_SOUND, 152);
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setFoodLevel(20);
                    }
                    else if (e.getCause() == DamageCause.VOID) {
                        Bukkit.broadcastMessage(ChatColor.GREEN + p.getDisplayName() + " fell into the void");
                        p.getPlayer().getLocation().getWorld().playEffect(e.getEntity().getLocation().add(0.0D, 1.0D, 0.0D), Effect.STEP_SOUND, 152);
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setFoodLevel(20);
                    }
                    else if (e.getCause() == DamageCause.ENTITY_ATTACK) {
    //This is the line!!!! >>>>     Bukkit.broadcastMessage(ChatColor.GREEN + p.getDisplayName() + " was killed by " + RIGHT HERE!!!! );
                        p.getPlayer().getLocation().getWorld().playEffect(e.getEntity().getLocation().add(0.0D, 1.0D, 0.0D), Effect.STEP_SOUND, 152);
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setFoodLevel(20);
                    }
                    else {
                        Bukkit.broadcastMessage("statement not added yet.");
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setFoodLevel(20);
                    }
                }
            }
        }
    
     
  2. Offline

    Xerox262

    @flash110 If the damage cause was that of an entity you should be able to cast the event to a entity damage event then get the damager from that.
     
  3. Offline

    flash110

    @Xerox262 Ok, I'll try that out right now.

    @Xerox262 Tried this, returns null.
    Code:
    else if (e.getCause() == DamageCause.ENTITY_ATTACK) {
                        Entity damager = (Entity) p.getLastDamageCause().getEntity();
                        Bukkit.broadcastMessage(ChatColor.GREEN + p.getDisplayName() + " was killed by " + damager.getName());
                        p.getPlayer().getLocation().getWorld().playEffect(e.getEntity().getLocation().add(0.0D, 1.0D, 0.0D), Effect.STEP_SOUND, 152);
                        e.setCancelled(true);
                        p.setHealth(20D);
                        p.setFoodLevel(20);
                    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 2, 2016
    Xerox262 likes this.
  4. Offline

    Xerox262

    Try casting your event to an EntityDamageByEntityEvent

    Like so

    EntityDamageByEntityEvent entityEvent = (EntityDamageByEntityEvent) e;
    Then use entityEvent.getDamager();
     
    FezzyDev and Thury like this.
  5. Offline

    flash110

    @Xerox262 Thanks, that seems like it would work, did not know you could do that! That's another small piece of info to remember.
    EDIT: That seemed to work, I did not know you could cast certain Events inside others. Thanks for the help!
     
    Last edited: Jun 3, 2016
    Xerox262 likes this.
Thread Status:
Not open for further replies.

Share This Page