Skip to content

Instantly share code, notes, and snippets.

@stephan-gh
Last active December 19, 2015 17:49
Show Gist options
  • Save stephan-gh/5994098 to your computer and use it in GitHub Desktop.
Save stephan-gh/5994098 to your computer and use it in GitHub Desktop.
Get the last entity damaged another entity.
public static Entity getLastEntityDamager(Entity entity) {
EntityDamageEvent event = entity.getLastDamageCause();
if (event != null && !event.isCancelled() && (event instanceof EntityDamageByEntityEvent)) {
Entity damager = ((EntityDamageByEntityEvent) event).getDamager();
if (damager instanceof Projectile) {
Object shooter = ((Projectile) damager).getShooter();
if (shooter != null && (shooter instanceof Entity)) return (Entity) shooter;
}
// Add other special cases if necessary
return damager;
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment