Search in sources :

Example 1 with EnderPearl

use of org.bukkit.entity.EnderPearl in project Parties by AlessioDP.

the class FightListener method onPlayerHit.

@EventHandler
public void onPlayerHit(EntityDamageByEntityEvent event) {
    if (ConfigParties.FRIENDLYFIRE_ENABLE && event.getEntity() instanceof Player) {
        Player victim = (Player) event.getEntity();
        Player attacker = null;
        // 0=Basic, 1=Arrow, 2=EnderPearl, 3=Snowball
        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;
        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 UNSUPPORTED:
            }
            if (attacker != null) {
                // Found right attacker
                if (!victim.getUniqueId().equals(attacker.getUniqueId()) && (ConfigParties.FRIENDLYFIRE_LISTWORLDS.contains("*") || ConfigParties.FRIENDLYFIRE_LISTWORLDS.contains(victim.getWorld().getName()))) {
                    // Friendly fire not allowed here
                    PartyPlayerEntity ppVictim = plugin.getPlayerManager().getPlayer(victim.getUniqueId());
                    PartyPlayerEntity ppAttacker = plugin.getPlayerManager().getPlayer(attacker.getUniqueId());
                    if (!ppVictim.getPartyName().isEmpty() && ppVictim.getPartyName().equalsIgnoreCase(ppAttacker.getPartyName())) {
                        // Calling API event
                        PartiesFriendlyFireBlockedEvent partiesFriendlyFireEvent = new PartiesFriendlyFireBlockedEvent(ppVictim, ppAttacker, event);
                        Bukkit.getServer().getPluginManager().callEvent(partiesFriendlyFireEvent);
                        if (!partiesFriendlyFireEvent.isCancelled()) {
                            // Friendly fire confirmed
                            PartyEntity party = plugin.getPartyManager().getParty(ppVictim.getPartyName());
                            ppAttacker.sendMessage(Messages.OTHER_FRIENDLYFIRE_CANTHIT);
                            party.sendFriendlyFireWarn(ppVictim, ppAttacker);
                            event.setCancelled(true);
                            LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_FRIENDLYFIRE_DENIED.replace("{type}", type.name()).replace("{player}", attacker.getName()).replace("{victim}", victim.getName()), true);
                        } else
                            LoggerManager.log(LogLevel.DEBUG, Constants.DEBUG_API_FRIENDLYFIREEVENT_DENY.replace("{type}", type.name()).replace("{player}", attacker.getName()).replace("{victim}", victim.getName()), true);
                    }
                }
            }
        }
    }
}
Also used : Arrow(org.bukkit.entity.Arrow) PartiesFriendlyFireBlockedEvent(com.alessiodp.partiesapi.events.PartiesFriendlyFireBlockedEvent) Snowball(org.bukkit.entity.Snowball) Player(org.bukkit.entity.Player) PartyPlayerEntity(com.alessiodp.parties.players.objects.PartyPlayerEntity) EnderPearl(org.bukkit.entity.EnderPearl) ProjectileSource(org.bukkit.projectiles.ProjectileSource) 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 PartiesFriendlyFireBlockedEvent (com.alessiodp.partiesapi.events.PartiesFriendlyFireBlockedEvent)1 Arrow (org.bukkit.entity.Arrow)1 EnderPearl (org.bukkit.entity.EnderPearl)1 Player (org.bukkit.entity.Player)1 Snowball (org.bukkit.entity.Snowball)1 EventHandler (org.bukkit.event.EventHandler)1 ProjectileSource (org.bukkit.projectiles.ProjectileSource)1