Search in sources :

Example 1 with BukkitPartiesPotionsFriendlyFireBlockedEvent

use of com.alessiodp.parties.api.events.bukkit.unique.BukkitPartiesPotionsFriendlyFireBlockedEvent in project Parties by AlessioDP.

the class BukkitFightListener method onPotionSplash.

@EventHandler(ignoreCancelled = true)
public void onPotionSplash(PotionSplashEvent event) {
    if (BukkitConfigParties.ADDITIONAL_FRIENDLYFIRE_ENABLE && event.getPotion().getShooter() instanceof Player) {
        Player attacker = (Player) event.getPotion().getShooter();
        PartyPlayerImpl ppAttacker = plugin.getPlayerManager().getPlayer(attacker.getUniqueId());
        BukkitPartyImpl party = (BukkitPartyImpl) plugin.getPartyManager().getParty(ppAttacker.getPartyId());
        if (party != null && party.isFriendlyFireProtected() && !attacker.hasPermission(PartiesPermission.ADMIN_PROTECTION_BYPASS.toString())) {
            boolean cancel = false;
            for (PotionEffect pe : event.getEntity().getEffects()) {
                switch(CommonUtils.toLowerCase(pe.getType().getName())) {
                    case "harm":
                    case "blindness":
                    case "confusion":
                    case "poison":
                    case "slow":
                    case "slow_digging":
                    case "weakness":
                    case "unluck":
                        cancel = true;
                        break;
                    default:
                        // Do not cancel
                        break;
                }
                if (cancel)
                    break;
            }
            if (cancel) {
                // Friendly fire not allowed here
                for (LivingEntity e : event.getAffectedEntities()) {
                    if (e instanceof Player) {
                        Player victim = (Player) e;
                        if (!attacker.equals(victim)) {
                            PartyPlayerImpl ppVictim = plugin.getPlayerManager().getPlayer(victim.getUniqueId());
                            if (ppAttacker.getPartyId() != null && ppAttacker.getPartyId().equals(ppVictim.getPartyId())) {
                                // Calling API event
                                BukkitPartiesPotionsFriendlyFireBlockedEvent partiesFriendlyFireEvent = ((BukkitEventManager) plugin.getEventManager()).preparePartiesPotionsFriendlyFireBlockedEvent(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.setIntensity(e, 0);
                                        plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_FRIENDLYFIRE_DENIED, "potion splash", attacker.getUniqueId(), victim.getUniqueId()), true);
                                    }
                                } else
                                    plugin.getLoggerManager().logDebug(String.format(PartiesConstants.DEBUG_API_FRIENDLYFIREEVENT_DENY, "potion splash", attacker.getUniqueId(), victim.getUniqueId()), true);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) User(com.alessiodp.core.common.user.User) PartyPlayerImpl(com.alessiodp.parties.common.players.objects.PartyPlayerImpl) BukkitPartyPlayerImpl(com.alessiodp.parties.bukkit.players.objects.BukkitPartyPlayerImpl) PotionEffect(org.bukkit.potion.PotionEffect) BukkitPartiesPotionsFriendlyFireBlockedEvent(com.alessiodp.parties.api.events.bukkit.unique.BukkitPartiesPotionsFriendlyFireBlockedEvent) BukkitPartyImpl(com.alessiodp.parties.bukkit.parties.objects.BukkitPartyImpl) BukkitEventManager(com.alessiodp.parties.bukkit.events.BukkitEventManager) EventHandler(org.bukkit.event.EventHandler)

Aggregations

User (com.alessiodp.core.common.user.User)1 BukkitPartiesPotionsFriendlyFireBlockedEvent (com.alessiodp.parties.api.events.bukkit.unique.BukkitPartiesPotionsFriendlyFireBlockedEvent)1 BukkitEventManager (com.alessiodp.parties.bukkit.events.BukkitEventManager)1 BukkitPartyImpl (com.alessiodp.parties.bukkit.parties.objects.BukkitPartyImpl)1 BukkitPartyPlayerImpl (com.alessiodp.parties.bukkit.players.objects.BukkitPartyPlayerImpl)1 PartyPlayerImpl (com.alessiodp.parties.common.players.objects.PartyPlayerImpl)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 PotionEffect (org.bukkit.potion.PotionEffect)1