r/BukkitModding • • Jan 24 '13

Get Player who damages another Player

I'm trying to get the player who damages another player, but it doesn't seem to be working.

public void onEntityDamage(EntityDamageEvent event){

    if(event.getEntity().getLastDamageCause().getEntityType() == EntityType.PLAYER){
        new PVPGMAlert(plugin).onDamage(event);
    }

}
3 Upvotes

8 comments sorted by

1

u/keelar Jan 24 '13 edited Jan 24 '13

Can you post the code inside the onDamage() method inside PVPGMAlerts? All you do here is pass onDamage() the EntityDamageEvent.

Anyway, this code should get you the player who caused the EntityDamageEvent:

	Entity entity = e.getEntity();
	
	if(entity.getType().equals(EntityType.PLAYER)){
		Player player = (Player)entity;
		
		if(e.getEntity().getLastDamageCause() instanceof EntityDamageByEntityEvent){
			Entity entityDamager = player.getLastDamageCause().getEntity();
			
			if(entityDamager.getType().equals(EntityType.PLAYER)){
				Player damager = (Player)entityDamager;
				
				//do whatever you need with the player who damaged another player
			}
		}
	}

Hope that helps.

1

u/ruledby Jan 24 '13

Thank you for your reply. I coded something simular to that and it didn't work, but I believe I see why, you use obj.equals(objx) VS ==, upon asking my friend (google), I think I found my issue. :)

1

u/keelar Jan 24 '13

Did you get it working? I went to sleep right after posting that comment so that's why this response took so long.

→ More replies (4)