BaleineBleue
BaleineBleue

Reputation: 387

Bukkit 1.11.2 Get player who did damage to another player

I know that you can get the damage cause with

event.getCause() == DamageCause.ENTITY_ATTACK

in the entity damage event, but I have not found a method to return the entity that did the damage. I need to find this out so I can check if the player has a certain item in their inventory.

Upvotes: 1

Views: 3689

Answers (2)

LeoColman
LeoColman

Reputation: 7143

To accomplish what you want, you should use an EntityDamageByEntityEvent.

Basically it's an event that fires everytime an Entity gets damaged by another Entity, and players are Entities.


Now, the Event Handling can be done this way:

@EventHandler
public void onPlayerDamage(EntityDamageByEntityEvent e) {
Entity damager = e.getDamager();
Entity damageTaker = e.getEntity();

if (damageTaker instanceof Player) {
    //DamageTaker is a Player
    Player taker = (Player) damageTaker;
    if (damager instanceof Player) {
        //Damage Causer is also a player
        Player damagerPlayer = (Player) damager;
    }
}

Upvotes: 1

stelar7
stelar7

Reputation: 335

Use EntityDamageByEntityEvent and cast the damager to a Player

Upvotes: 0

Related Questions