use of com.alessiodp.parties.bukkit.events.BukkitEventManager in project Parties by AlessioDP.
the class BukkitFightListener method onPlayerHit.
@EventHandler(ignoreCancelled = true)
public void onPlayerHit(EntityDamageByEntityEvent event) {
if (BukkitConfigParties.ADDITIONAL_FRIENDLYFIRE_ENABLE && event.getEntity() instanceof Player) {
Player victim = (Player) event.getEntity();
Player attacker = null;
DamageType type = DamageType.UNSUPPORTED;
if (event.getDamager() instanceof Player)
type = DamageType.PLAYER;
else if (event.getDamager() instanceof Arrow)
type = DamageType.ARROW;
else if (event.getDamager() instanceof EnderPearl)
type = DamageType.ENDERPEARL;
else if (event.getDamager() instanceof Snowball)
type = DamageType.SNOWBALL;
try {
if (event.getDamager() instanceof Trident)
type = DamageType.TRIDENT;
} catch (NoClassDefFoundError ignored) {
}
if (!type.equals(DamageType.UNSUPPORTED)) {
ProjectileSource shooterSource;
switch(type) {
case PLAYER:
attacker = (Player) event.getDamager();
break;
case ARROW:
shooterSource = ((Arrow) event.getDamager()).getShooter();
if (shooterSource instanceof Player)
attacker = (Player) shooterSource;
break;
case ENDERPEARL:
shooterSource = ((EnderPearl) event.getDamager()).getShooter();
if (shooterSource instanceof Player)
attacker = (Player) shooterSource;
break;
case SNOWBALL:
shooterSource = ((Snowball) event.getDamager()).getShooter();
if (shooterSource instanceof Player)
attacker = (Player) shooterSource;
break;
case TRIDENT:
shooterSource = ((Trident) event.getDamager()).getShooter();
if (shooterSource instanceof Player)
attacker = (Player) shooterSource;
break;
default:
// Nothing to do
break;
}
if (attacker != null) {
// Found right attacker
if (!victim.getUniqueId().equals(attacker.getUniqueId())) {
// Friendly fire not allowed here
PartyPlayerImpl ppVictim = plugin.getPlayerManager().getPlayer(victim.getUniqueId());
PartyPlayerImpl ppAttacker = plugin.getPlayerManager().getPlayer(attacker.getUniqueId());
BukkitPartyImpl party = (BukkitPartyImpl) plugin.getPartyManager().getParty(ppAttacker.getPartyId());
if (party != null && party.getId().equals(ppVictim.getPartyId()) && party.isFriendlyFireProtected() && !attacker.hasPermission(PartiesPermission.ADMIN_PROTECTION_BYPASS.toString())) {
// Calling API event
BukkitPartiesFriendlyFireBlockedEvent partiesFriendlyFireEvent = ((BukkitEventManager) plugin.getEventManager()).preparePartiesFriendlyFireBlockedEvent(ppVictim, ppAttacker, event);
plugin.getEventManager().callEvent(partiesFriendlyFireEvent);
if (!partiesFriendlyFireEvent.isCancelled()) {
// Friendly fire confirmed
User userAttacker = plugin.getPlayer(attacker.getUniqueId());
if (userAttacker != null) {
userAttacker.sendMessage(plugin.getMessageUtils().convertPlaceholders(BukkitMessages.ADDCMD_PROTECTION_PROTECTED, ppAttacker, party), true);
party.warnFriendlyFire(ppVictim, ppAttacker);
event.setCancelled(true);
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_FRIENDLYFIRE_DENIED, type.name(), attacker.getUniqueId(), victim.getUniqueId()), true);
}
} else
plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_API_FRIENDLYFIREEVENT_DENY, type.name(), attacker.getUniqueId(), victim.getUniqueId()), true);
}
}
}
}
}
}
Aggregations