}
@EventHandler(priority=EventPriority.HIGHEST)
public void damageDealt(EntityDamageEvent event)
{
AutoRefMatch match = plugin.getMatch(event.getEntity().getWorld());
if (match == null || !match.getCurrentState().inProgress()) return;
if (event.getEntityType() == EntityType.PLAYER)
{
AutoRefPlayer apl = match.getPlayer((Player) event.getEntity());
if (apl != null && apl.isGodMode()) { event.setDamage(0); return; }
}
if (match.getCurrentState().inProgress() &&
event instanceof EntityDamageByEntityEvent)
{
EntityDamageByEntityEvent ed = (EntityDamageByEntityEvent) event;
Player damaged = entityToPlayer(ed.getEntity());
// enderpearls are a special case!
if (ed.getDamager().getType() == EntityType.ENDER_PEARL) return;
Player damager = entityToPlayer(ed.getDamager());
if (null != damager && ed.getDamager() instanceof Arrow)
{
AutoRefPlayer apl = match.getPlayer(damager);
if (apl != null) apl.incrementShotsHit();
Arrow arrow = (Arrow) ed.getDamager();
if (arrow.getShooter().getType() == EntityType.PLAYER)
{
AutoRefPlayer shooter = match.getPlayer((Player) arrow.getShooter());
Location shotFrom = shotArrows.get(arrow);
if (shooter != null && shotFrom != null)
shooter.setFurthestShot(arrow.getLocation().distance(shotFrom));
}
}
// spectators cannot cause damage to any entity
if (match.getCurrentState().inProgress() &&
null != damager && match.isSpectator(damager))
{ event.setCancelled(true); return; }
if (null != damager && ed.getEntityType() == EntityType.PIG_ZOMBIE)
{
AutoRefPlayer apl = match.getPlayer(damager);
Long lastAggro = lastPigmenAggro.get(apl);
long currentTime = ManagementFactory.getRuntimeMXBean().getUptime();
if (lastAggro == null || currentTime > PIGMEN_COOLDOWN_MS + lastAggro)
{
for (Player ref : match.getReferees(false))
ref.sendMessage(apl.getDisplayName() + ChatColor.GRAY + " has angered the Zombie Pigmen");
lastPigmenAggro.put(apl, currentTime);
}
}
// if either of these aren't players, nothing to do here
if (null == damager || null == damaged) return;
AutoRefPlayer aapl = match.getPlayer(damager);
AutoRefPlayer vapl = match.getPlayer(damaged);
// FIXME - define behavior for when attacker or victim is not actually in the match
// (and therefore aapl/vapl is null)
if (!aapl.isInsideLane())
{ event.setCancelled(true); return; }
// if the match is in progress and player is in start region
// cancel any damage dealt to the player
if (match.getCurrentState().inProgress() && vapl != null && !vapl.isActive())
{ event.setCancelled(true); return; }
// if both players are not on a team, quit
if (aapl.getTeam() == null && vapl.getTeam() == null) return;
// if the attacked isn't on a team, or same team (w/ no FF), cancel
if (vapl.getTeam() == null || (aapl.getTeam() == vapl.getTeam() && !match.allowFriendlyFire()))
{ event.setCancelled(true); return; }
}
// only allow damage before a match if it is a direct attack
if (match.getCurrentState().isBeforeMatch() &&
event.getCause() != DamageCause.ENTITY_ATTACK)
{ event.setCancelled(true); return; }
}