Search in sources :

Example 41 with ProjectileSource

use of org.bukkit.projectiles.ProjectileSource in project Denizen-For-Bukkit by DenizenScript.

the class NPCTagBase method onDeath.

// <--[action]
// @Actions
// death
// death by entity
// death by <entity>
// death by block
// death by <cause>
// 
// @Triggers when the NPC dies.
// 
// @Context
// <context.killer> returns the entity that killed the NPC (if any)
// <context.shooter> returns the shooter of the killing projectile (if any)
// <context.damage> returns the last amount of damage applied (if any)
// <context.death_cause> returns the last damage cause (if any)
// 
// -->
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(EntityDeathEvent deathEvent) {
    NPC citizen = CitizensAPI.getNPCRegistry().getNPC(deathEvent.getEntity());
    if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(citizen);
    EntityDamageEvent event = deathEvent.getEntity().getLastDamageCause();
    String deathCause = event == null ? "unknown" : CoreUtilities.toLowerCase(event.getCause().toString()).replace('_', ' ');
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event == null ? 0 : event.getDamage()));
    context.put("death_cause", new ElementTag(deathCause));
    PlayerTag player = null;
    if (event instanceof EntityDamageByEntityEvent) {
        Entity killerEntity = ((EntityDamageByEntityEvent) event).getDamager();
        context.put("killer", new EntityTag(killerEntity).getDenizenObject());
        if (killerEntity instanceof Player) {
            player = PlayerTag.mirrorBukkitPlayer((Player) killerEntity);
        } else if (killerEntity instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) killerEntity).getShooter();
            if (shooter instanceof LivingEntity) {
                context.put("shooter", new EntityTag((LivingEntity) shooter).getDenizenObject());
                if (shooter instanceof Player) {
                    player = PlayerTag.mirrorBukkitPlayer((Player) shooter);
                }
                npc.action("death by " + ((LivingEntity) shooter).getType().toString(), player, context);
            }
        }
        npc.action("death by entity", player, context);
        npc.action("death by " + killerEntity.getType().toString(), player, context);
    } else if (event instanceof EntityDamageByBlockEvent) {
        npc.action("death by block", null, context);
    }
    npc.action("death", player, context);
    npc.action("death by " + deathCause, player, context);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 42 with ProjectileSource

use of org.bukkit.projectiles.ProjectileSource in project FunnyGuilds by FunnyGuilds.

the class EntityDamage method onDamage.

@EventHandler(priority = EventPriority.HIGHEST)
public void onDamage(EntityDamageByEntityEvent event) {
    Entity entity = event.getEntity();
    Entity damager = event.getDamager();
    if (!(entity instanceof Player)) {
        return;
    }
    Player attacker = null;
    if (damager instanceof Player) {
        attacker = (Player) damager;
    } else if (damager instanceof Projectile) {
        ProjectileSource shooter = ((Projectile) damager).getShooter();
        if (shooter instanceof Player) {
            attacker = (Player) shooter;
        }
    }
    if (attacker == null) {
        return;
    }
    PluginConfig config = Settings.getConfig();
    User victimUser = User.get((Player) event.getEntity());
    User attackerUser = User.get(attacker);
    if (victimUser.hasGuild() && attackerUser.hasGuild()) {
        if (victimUser.getUUID().equals(attackerUser.getUUID())) {
            return;
        }
        if (victimUser.getGuild().equals(attackerUser.getGuild())) {
            if (!victimUser.getGuild().getPvP()) {
                event.setCancelled(true);
                return;
            }
        }
        if (victimUser.getGuild().getAllies().contains(attackerUser.getGuild())) {
            if (!config.damageAlly) {
                event.setCancelled(true);
                return;
            }
        }
    }
    if (config.assistEnable && !event.isCancelled()) {
        victimUser.addDamage(attackerUser, event.getDamage());
    }
}
Also used : PluginConfig(net.dzikoysk.funnyguilds.data.configs.PluginConfig) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) User(net.dzikoysk.funnyguilds.basic.User) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 43 with ProjectileSource

use of org.bukkit.projectiles.ProjectileSource in project Sentinel by mcmonkey4eva.

the class SentinelTrait method whenAttacksHappened.

public void whenAttacksHappened(EntityDamageByEntityEvent event) {
    if (!npc.isSpawned()) {
        return;
    }
    if (event.isCancelled()) {
        return;
    }
    if (event == damageDeDup) {
        return;
    }
    damageDeDup = event;
    Entity damager = event.getDamager();
    LivingEntity projectileSource = null;
    if (damager instanceof Projectile) {
        ProjectileSource source = ((Projectile) damager).getShooter();
        if (source instanceof LivingEntity) {
            projectileSource = (LivingEntity) source;
            damager = projectileSource;
        }
    }
    boolean isMe = event.getEntity().getUniqueId().equals(getLivingEntity().getUniqueId());
    if (isMe && (SentinelPlugin.instance.protectFromIgnores || protectFromIgnores)) {
        if (damager instanceof LivingEntity && targetingHelper.isIgnored((LivingEntity) damager)) {
            event.setCancelled(true);
            return;
        } else if (projectileSource != null && targetingHelper.isIgnored(projectileSource)) {
            event.setCancelled(true);
            return;
        }
    }
    boolean isKilling = event.getEntity() instanceof LivingEntity && event.getFinalDamage() >= ((LivingEntity) event.getEntity()).getHealth();
    boolean isFriend = getGuarding() != null && event.getEntity().getUniqueId().equals(getGuarding());
    boolean attackerIsMe = damager.getUniqueId().equals(getLivingEntity().getUniqueId());
    if (projectileSource != null && projectileSource.getUniqueId().equals(getLivingEntity().getUniqueId())) {
        attackerIsMe = true;
    }
    if (isMe || isFriend) {
        if (attackerIsMe) {
            if (SentinelPlugin.debugMe) {
                debug("Ignoring damage I did to " + (isMe ? "myself." : "my friend."));
            }
            event.setCancelled(true);
            return;
        }
        if (getGuarding() != null && damager.getUniqueId().equals(getGuarding())) {
            if (isMe && SentinelPlugin.instance.noGuardDamage) {
                debug("Ignoring damage from the player we're guarding.");
                event.setCancelled(true);
            }
            return;
        }
        if (isMe) {
            stats_damageTaken += event.getFinalDamage();
            if (runaway) {
                debug("Ow! They hit me! Run!");
                targetingHelper.addAvoid(damager.getUniqueId());
            }
        }
        if (fightback && (damager instanceof LivingEntity) && !targetingHelper.isIgnored((LivingEntity) damager)) {
            if (SentinelPlugin.debugMe) {
                debug("Fighting back against attacker: " + damager.getUniqueId() + "! They hurt " + (isMe ? "me!" : "my friend!"));
            }
            targetingHelper.addTarget(damager.getUniqueId());
            if (chasing == null) {
                if (itemHelper.isRanged() ? rangedChase : closeChase) {
                    attackHelper.chase((LivingEntity) damager);
                }
            }
        }
        if (SentinelPlugin.debugMe && isMe) {
            debug("Took damage of " + event.getFinalDamage() + " with currently remaining health " + getLivingEntity().getHealth() + (isKilling ? ". This will kill me." : "."));
        }
        if (isKilling && isMe && SentinelPlugin.instance.blockEvents) {
            // We're going to die but we've been requested to avoid letting a death event fire.
            // The solution at this point is to fake our death: remove the NPC entity from existence and trigger our internal death handling sequence.
            // We also mark the damage event cancelled to reduce trouble (ensure event doesn't try to apply damage to our now-gone entity).
            debug("Died! Applying death workaround (due to config setting)");
            generalDeathHandler(getLivingEntity());
            npc.despawn(DespawnReason.PLUGIN);
            event.setCancelled(true);
            return;
        }
        return;
    }
    if (attackerIsMe) {
        if (safeShot && !targetingHelper.shouldTarget((LivingEntity) event.getEntity())) {
            event.setCancelled(true);
            return;
        }
        stats_damageGiven += event.getFinalDamage();
        if (!enemyDrops && event.getEntity().getType() != EntityType.PLAYER) {
            needsDropsClear.add(event.getEntity().getUniqueId());
            if (SentinelPlugin.debugMe) {
                debug("This " + event.getEntity().getType() + " with id " + event.getEntity().getUniqueId() + " is being tracked for potential drops removal.");
            }
        }
        return;
    }
}
Also used : ProjectileSource(org.bukkit.projectiles.ProjectileSource)

Example 44 with ProjectileSource

use of org.bukkit.projectiles.ProjectileSource in project Sentinel by mcmonkey4eva.

the class SentinelTargetList method isEventTarget.

/**
 * Returns whether the damager in a damage event is targeted by this list.
 */
public boolean isEventTarget(EntityDamageByEntityEvent event) {
    Entity damager = event.getDamager();
    if (event.getDamager() instanceof Projectile) {
        ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
        if (source instanceof Entity) {
            damager = (Entity) source;
        }
    }
    if (CitizensAPI.getNPCRegistry().isNPC(damager)) {
        return false;
    }
    if (damager.equals(event.getEntity())) {
        // Players can accidentally hurt themselves - that's not PvP
        return false;
    }
    for (String evt : byEvent) {
        if (evt.equals("pvp") && event.getEntity() instanceof Player && damager instanceof Player && !CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
            return true;
        } else if (evt.equals("pve") && !(event.getEntity() instanceof Player) && damager instanceof Player && event.getEntity() instanceof LivingEntity) {
            return true;
        } else if (evt.equals("eve") && !(damager instanceof Player) && !(event.getEntity() instanceof Player) && event.getEntity() instanceof LivingEntity) {
            return true;
        } else if (evt.equals("pvnpc") && event.getEntity() instanceof LivingEntity && damager instanceof Player && CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
            return true;
        } else if (evt.equals("pvsentinel") && event.getEntity() instanceof LivingEntity && damager instanceof Player && CitizensAPI.getNPCRegistry().isNPC(event.getEntity()) && CitizensAPI.getNPCRegistry().getNPC(event.getEntity()).hasTrait(SentinelTrait.class)) {
            return true;
        }
        if (evt.contains(":")) {
            int colon = evt.indexOf(':');
            String prefix = evt.substring(0, colon);
            String value = evt.substring(colon + 1);
            if (prefix.equals("pv")) {
                SentinelTarget target = SentinelTarget.forName(value);
                if (target != null) {
                    if (damager instanceof Player && event.getEntity() instanceof LivingEntity && target.isTarget((LivingEntity) event.getEntity())) {
                        return true;
                    }
                }
            } else if (prefix.equals("ev")) {
                SentinelTarget target = SentinelTarget.forName(value);
                if (target != null) {
                    if (!(damager instanceof Player) && event.getEntity() instanceof LivingEntity && target.isTarget((LivingEntity) event.getEntity())) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ProjectileSource(org.bukkit.projectiles.ProjectileSource) SentinelTrait(org.mcmonkey.sentinel.SentinelTrait)

Aggregations

ProjectileSource (org.bukkit.projectiles.ProjectileSource)44 EventHandler (org.bukkit.event.EventHandler)24 LivingEntity (org.bukkit.entity.LivingEntity)21 Projectile (org.bukkit.entity.Projectile)21 Entity (org.bukkit.entity.Entity)20 Player (org.bukkit.entity.Player)17 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)5 UUID (java.util.UUID)4 Entity (org.spongepowered.api.entity.Entity)4 Player (org.spongepowered.api.entity.player.Player)4 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)3 EntityTag (com.denizenscript.denizen.objects.EntityTag)2 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)2 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 EnumAbilities (com.jacob_vejvoda.infernal_mobs.ability.EnumAbilities)2 Mob (com.jacob_vejvoda.infernal_mobs.persist.Mob)2 List (java.util.List)2 Random (java.util.Random)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 Location (org.bukkit.Location)2