*
* @param event
* fired event
*/
private void handlerAttackDefend(EntityDamageByEntityEvent event) {
Entity attacker = event.getDamager();
Entity defender = event.getEntity();
// Maybe an arrow was thrown
if (attacker != null && event.getDamager() instanceof Projectile && ((Projectile)event.getDamager()).getShooter() instanceof Player){
attacker = ((Player)((Projectile)event.getDamager()).getShooter());
}
if (attacker != null && defender != null && attacker instanceof Player && defender instanceof Player) {
// only let adversaries (same warzone, different team) attack each other
Player a = (Player) attacker;
Player d = (Player) defender;
Warzone attackerWarzone = Warzone.getZoneByPlayerName(a.getName());
Team attackerTeam = Team.getTeamByPlayerName(a.getName());
Warzone defenderWarzone = Warzone.getZoneByPlayerName(d.getName());
Team defenderTeam = Team.getTeamByPlayerName(d.getName());
if ((attackerTeam != null && defenderTeam != null && attackerTeam != defenderTeam && attackerWarzone == defenderWarzone)
|| (attackerTeam != null && defenderTeam != null && attacker.getEntityId() == defender.getEntityId())) {
LoadoutSelection defenderLoadoutState = defenderWarzone.getLoadoutSelections().get(d.getName());
if (defenderLoadoutState != null && defenderLoadoutState.isStillInSpawn()) {
War.war.badMsg(a, "pvp.target.spawn");
event.setCancelled(true);
return;
}
LoadoutSelection attackerLoadoutState = attackerWarzone.getLoadoutSelections().get(a.getName());
if (attackerLoadoutState != null && attackerLoadoutState.isStillInSpawn()) {
War.war.badMsg(a, "pvp.self.spawn");
event.setCancelled(true);
return;
}
// Make sure none of them are locked in by respawn timer
if (defenderWarzone.isRespawning(d)) {
War.war.badMsg(a, "pvp.target.respawn");
event.setCancelled(true);
return;
} else if (attackerWarzone.isRespawning(a)) {
War.war.badMsg(a, "pvp.self.respawn");
event.setCancelled(true);
return;
}
if (!attackerWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.PVPINZONE)) {
// spleef-like, non-pvp, zone
event.setCancelled(true);
return;
}
// Detect death, prevent it and respawn the player
if (event.getDamage() >= d.getHealth()) {
if (defenderWarzone.getReallyDeadFighters().contains(d.getName())) {
// don't re-kill a dead person
return;
}
WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(defenderWarzone, d, a, event.getCause());
War.war.getServer().getPluginManager().callEvent(event1);
if (!defenderWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.REALDEATHS)) {
// fast respawn, don't really die
event.setCancelled(true);
}
if (d == a) {
defenderWarzone.handleSuicide(d);
} else {
defenderWarzone.handleKill(a, d, event.getDamager());
}
} else if (defenderWarzone.isBombThief(d.getName()) && d.getLocation().distance(a.getLocation()) < 2) {
// Close combat, close enough to detonate
Bomb bomb = defenderWarzone.getBombForThief(d.getName());
// Kill the bomber
WarPlayerDeathEvent event1 = new WarPlayerDeathEvent(defenderWarzone, d, null, event.getCause());
War.war.getServer().getPluginManager().callEvent(event1);
defenderWarzone.handleDeath(d);
if (defenderWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.REALDEATHS)) {
// and respawn him and remove from deadmen (cause realdeath + handleDeath means no respawn and getting queued up for onPlayerRespawn)
defenderWarzone.getReallyDeadFighters().remove(d.getName());
defenderWarzone.respawnPlayer(defenderTeam, d);
}
// Blow up bomb
if (!defenderWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.UNBREAKABLE)) {
defenderWarzone.getWorld().createExplosion(a.getLocation(), 2F);
}
// bring back tnt
bomb.getVolume().resetBlocks();
bomb.addBombBlocks();
// Notify everyone
for (Team t : defenderWarzone.getTeams()) {
t.sendAchievement(attackerTeam.getKind().getColor() + a.getName() + ChatColor.YELLOW + " made ",
defenderTeam.getKind().getColor() + d.getName() + ChatColor.YELLOW + " blow up!", new ItemStack(Material.TNT), 10000);
t.teamcast("pvp.kill.bomb", attackerTeam.getKind().getColor() + a.getName() + ChatColor.WHITE,
defenderTeam.getKind().getColor() + d.getName() + ChatColor.WHITE);
}
}
} else if (attackerTeam != null && defenderTeam != null && attackerTeam == defenderTeam && attackerWarzone == defenderWarzone && attacker.getEntityId() != defender.getEntityId()) {
// same team, but not same person
if (attackerWarzone.getWarzoneConfig().getBoolean(WarzoneConfig.FRIENDLYFIRE)) {
War.war.badMsg(a, "pvp.ff.enabled"); // if ff is on, let the attack go through
} else {
War.war.badMsg(a, "pvp.ff.disabled");
event.setCancelled(true); // ff is off
}
} else if (attackerTeam == null && defenderTeam == null && War.war.canPvpOutsideZones(a)) {
// let normal PVP through is its not turned off or if you have perms
} else if (attackerTeam == null && defenderTeam == null && !War.war.canPvpOutsideZones(a)) {
if (!War.war.getWarConfig().getBoolean(WarConfig.DISABLEPVPMESSAGE)) {
War.war.badMsg(a, "pvp.outside.permission");
}
event.setCancelled(true); // global pvp is off
} else {
if (attackerTeam == null) {
War.war.badMsg(a, "pvp.self.notplaying");
} else if (defenderTeam == null) {
War.war.badMsg(a, "pvp.target.notplaying");
} else if (attacker != null && defender != null && attacker.getEntityId() == defender.getEntityId()) {
// You just hit yourself, probably with a bouncing arrow
} else if (attackerTeam == defenderTeam) {
War.war.badMsg(a, "pvp.ff.disabled");
} else if (attackerWarzone != defenderWarzone) {
War.war.badMsg(a, "pvp.target.otherzone");