Search in sources :

Example 1 with PartiesPotionsFriendlyFireBlockedEvent

use of com.alessiodp.partiesapi.events.PartiesPotionsFriendlyFireBlockedEvent in project Parties by AlessioDP.

the class FightListener method onPotionSplash.

@EventHandler
public void onPotionSplash(PotionSplashEvent event) {
    if (ConfigParties.FRIENDLYFIRE_ENABLE && event.getEntity() instanceof Player && event.getPotion().getShooter() instanceof Player) {
        Player attacker = (Player) event.getPotion().getShooter();
        PartyPlayerEntity ppAttacker = plugin.getPlayerManager().getPlayer(attacker.getUniqueId());
        if (!ppAttacker.getPartyName().isEmpty() && (ConfigParties.FRIENDLYFIRE_LISTWORLDS.contains("*") || ConfigParties.FRIENDLYFIRE_LISTWORLDS.contains(attacker.getWorld().getName()))) {
            boolean cancel = false;
            for (PotionEffect pe : event.getEntity().getEffects()) {
                switch(pe.getType().getName().toLowerCase()) {
                    case "harm":
                    case "blindness":
                    case "confusion":
                    case "poison":
                    case "slow":
                    case "slow_digging":
                    case "weakness":
                    case "unluck":
                        cancel = true;
                }
                if (cancel)
                    break;
            }
            if (cancel) {
                // Friendly fire not allowed here
                PartyEntity party = plugin.getPartyManager().getParty(ppAttacker.getPartyName());
                for (LivingEntity e : event.getAffectedEntities()) {
                    if (e instanceof Player) {
                        Player victim = (Player) e;
                        if (!attacker.equals(victim)) {
                            PartyPlayerEntity ppVictim = plugin.getPlayerManager().getPlayer(victim.getUniqueId());
                            if (ppVictim.getPartyName().equalsIgnoreCase(ppAttacker.getPartyName())) {
                                // Calling API event
                                PartiesPotionsFriendlyFireBlockedEvent partiesFriendlyFireEvent = new PartiesPotionsFriendlyFireBlockedEvent(ppVictim, ppAttacker, event);
                                Bukkit.getServer().getPluginManager().callEvent(partiesFriendlyFireEvent);
                                if (!partiesFriendlyFireEvent.isCancelled()) {
                                    // Friendly fire confirmed
                                    ppAttacker.sendMessage(Messages.OTHER_FRIENDLYFIRE_CANTHIT);
                                    party.sendFriendlyFireWarn(ppVictim, ppAttacker);
                                    event.setIntensity(e, 0);
                                    LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_FRIENDLYFIRE_DENIED.replace("{type}", "potion splash").replace("{player}", attacker.getName()).replace("{victim}", victim.getName()), true);
                                } else
                                    LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_API_FRIENDLYFIREEVENT_DENY.replace("{type}", "potion splash").replace("{player}", attacker.getName()).replace("{victim}", victim.getName()), true);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) PartiesPotionsFriendlyFireBlockedEvent(com.alessiodp.partiesapi.events.PartiesPotionsFriendlyFireBlockedEvent) Player(org.bukkit.entity.Player) PartyPlayerEntity(com.alessiodp.parties.players.objects.PartyPlayerEntity) PotionEffect(org.bukkit.potion.PotionEffect) PartyEntity(com.alessiodp.parties.parties.objects.PartyEntity) EventHandler(org.bukkit.event.EventHandler)

Aggregations

PartyEntity (com.alessiodp.parties.parties.objects.PartyEntity)1 PartyPlayerEntity (com.alessiodp.parties.players.objects.PartyPlayerEntity)1 PartiesPotionsFriendlyFireBlockedEvent (com.alessiodp.partiesapi.events.PartiesPotionsFriendlyFireBlockedEvent)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