Reputation: 387
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
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