Search in sources :

Example 16 with ProjectileSource

use of org.bukkit.projectiles.ProjectileSource in project InfernalMobs by NyaaCat.

the class EventListener method onEntityDeath.

@EventHandler(priority = EventPriority.HIGH)
public void onEntityDeath(final EntityDeathEvent event) {
    if (event.getEntity() instanceof Bat) {
        try {
            LivingEntity entity = event.getEntity();
            List<MetadataValue> metadata = entity.getMetadata("im-drop");
            if (!metadata.isEmpty()) {
                MetadataValue metadataValue = metadata.get(0);
                List<ItemStack> value = (List<ItemStack>) metadataValue.value();
                event.getDrops().clear();
                event.getDrops().addAll(value);
            }
        } catch (Exception e) {
        }
    }
    UUID id = event.getEntity().getUniqueId();
    Mob mob = plugin.mobManager.mobMap.get(id);
    if (mob == null)
        return;
    LivingEntity mobEntity = event.getEntity();
    // item drop decision
    ItemStack selectedDropItem = null;
    Player killer = mobEntity.getKiller();
    Bat bat = null;
    if (killer == null) {
        EntityDamageByEntityEvent lastDamageCause = mob.lastDamageCause;
        if (lastDamageCause != null) {
            Entity damager = lastDamageCause.getDamager();
            if (damager instanceof Projectile) {
                ProjectileSource shooter = ((Projectile) damager).getShooter();
                if (shooter instanceof Player) {
                    killer = (Player) shooter;
                }
            } else if (damager instanceof Player) {
                killer = ((Player) damager);
            }
            event.getEntity().setLastDamageCause(lastDamageCause);
            bat = ArmorStandUtil.asVictim(mobEntity);
            bat.setCustomName(mobEntity.getCustomName());
        // Bukkit.getServer().getPluginManager().callEvent(new EntityDeathEvent(armorStand, dropClone));
        }
    }
    for (EnumAbilities ab : mob.abilityList) {
        ab.onDeath(mobEntity, mob, killer, event);
    }
    if (determineShouldDrop(killer != null, (killer != null) && (Helper.validGamemode(killer)))) {
        ItemStack drop;
        if (!mob.isCustomMob) {
            drop = this.plugin.lootManager.getRandomLoot(killer, mob.getMobLevel());
        } else {
            drop = this.plugin.lootManager.getLootByName(killer, mob.customLoot);
        }
        if (drop != null && drop.getType() != Material.AIR) {
            final int percentage = ConfigReader.getDropChance();
            final int randomNum = new Random().nextInt(100);
            if (randomNum < percentage) {
                event.getDrops().add(drop);
                selectedDropItem = drop;
            }
        }
    }
    // set xp drop
    if (ConfigReader.isEnhanceEnabled()) {
        int xp = ConfigReader.getLevelConfig().getExp(event.getDroppedExp(), mob.getMobLevel());
        event.setDroppedExp(xp);
    } else {
        final int xpm = ConfigReader.getXpMultiplier();
        final int xp = event.getDroppedExp() * xpm;
        event.setDroppedExp(xp);
    }
    // broadcast death message TODO use ConfigReader
    if (ConfigReader.isMobDeathMessageEnabled() && killer != null) {
        Player player = killer;
        String playerName = player.getName();
        String mobName;
        if (event.getEntity().getCustomName() != null) {
            mobName = event.getEntity().getCustomName();
        } else {
            mobName = event.getEntity().getType().name();
        }
        boolean broadcastToAllWorld = ConfigReader.isDeathMessageBroadcastAllWorld();
        Message message = new Message("");
        if (this.plugin.getConfig().getList("deathMessages") != null) {
            String deathMessage = Helper.randomItem(plugin.getConfig().getStringList("deathMessages"));
            deathMessage = ChatColor.translateAlternateColorCodes('&', deathMessage);
            deathMessage = deathMessage.replace("{player}", playerName);
            deathMessage = deathMessage.replace("{mob}", mobName);
            ItemStack item = player.getInventory().getItemInMainHand();
            if (item != null && !item.getType().equals(Material.AIR)) {
                message.append(deathMessage, item);
            } else {
                message.append(deathMessage.replace("{itemName}", "fist").replace("{itemName:0}", "fist"));
            }
        } else {
            System.out.println("No valid death messages found!");
        }
        message.append("\n");
        String msg = Helper.randomItem(plugin.getConfig().getStringList("nodropMessages"));
        if (plugin.getConfig().isList("dropMessages") && selectedDropItem != null) {
            msg = Helper.randomItem(plugin.getConfig().getStringList("dropMessages"));
        // message.append(msg);
        // if (broadcastToAllWorld) {
        // new Message("")
        // .append(msg, selectedDropItem)
        // .broadcast();
        // } else {
        // new Message("")
        // .append(msg, selectedDropItem)
        // .broadcast(player.getLocation().getWorld());
        // }
        }
        if (plugin.getConfig().isList("nodropMessages") && selectedDropItem == null) {
            msg = Helper.randomItem(plugin.getConfig().getStringList("nodropMessages"));
        // if (broadcastToAllWorld) {
        // new Message(msg)
        // .broadcast();
        // } else {
        // new Message(msg)
        // .broadcast(player.getLocation().getWorld());
        // }
        }
        msg = ChatColor.translateAlternateColorCodes('&', msg);
        msg = msg.replace("{player}", playerName);
        msg = msg.replace("{mob}", mobName);
        if (selectedDropItem == null) {
            message.append(msg);
        } else {
            message.append(msg, selectedDropItem);
        }
        if (broadcastToAllWorld) {
            Broadcaster.broadcastToAllWorld(message, player);
        } else {
            Broadcaster.broadcast(player.getLocation().getWorld(), message, player);
        }
    }
    if (bat != null) {
        List<ItemStack> drops = event.getDrops();
        List<ItemStack> dropClone = new ArrayList<>(drops);
        drops.clear();
        bat.setMetadata("im-drop", new FixedMetadataValue(InfernalMobs.instance, dropClone));
        bat.damage(bat.getHealth() * 10, killer);
    }
    plugin.mobManager.mobMap.remove(id);
    BossBarManager.removeMob(mob, mobEntity);
// TODO event
}
Also used : FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) MetadataValue(org.bukkit.metadata.MetadataValue) Mob(com.jacob_vejvoda.infernal_mobs.persist.Mob) Message(cat.nyaa.nyaacore.Message) ArrayList(java.util.ArrayList) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) ProjectileSource(org.bukkit.projectiles.ProjectileSource) ItemStack(org.bukkit.inventory.ItemStack) UUID(java.util.UUID) EnumAbilities(com.jacob_vejvoda.infernal_mobs.ability.EnumAbilities) EventHandler(org.bukkit.event.EventHandler)

Example 17 with ProjectileSource

use of org.bukkit.projectiles.ProjectileSource in project acidisland by tastybento.

the class IslandGuard1_9 method EndCrystalDamage.

/**
 * Handle end crystal damage by visitors
 * @param e - event
 */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void EndCrystalDamage(final EntityDamageByEntityEvent e) {
    if (e.getEntity() == null || !IslandGuard.inWorld(e.getEntity())) {
        return;
    }
    if (!(e.getEntity() instanceof EnderCrystal)) {
        return;
    }
    Player p = null;
    if (e.getDamager() instanceof Player) {
        p = (Player) e.getDamager();
    } else if (e.getDamager() instanceof Projectile) {
        // Get the shooter
        Projectile projectile = (Projectile) e.getDamager();
        ProjectileSource shooter = projectile.getShooter();
        if (shooter instanceof Player) {
            p = (Player) shooter;
        }
    }
    if (p != null) {
        if (p.isOp() || VaultHelper.checkPerm(p, Settings.PERMPREFIX + "mod.bypassprotect")) {
            return;
        }
        // Check if on island
        if (plugin.getGrid().playerIsOnIsland(p)) {
            return;
        }
        // Check island
        Island island = plugin.getGrid().getIslandAt(e.getEntity().getLocation());
        if (island == null && Settings.defaultWorldSettings.get(SettingsFlag.BREAK_BLOCKS)) {
            return;
        }
        if (island != null && island.getIgsFlag(SettingsFlag.BREAK_BLOCKS)) {
            return;
        }
        Util.sendMessage(p, ChatColor.RED + plugin.myLocale(p.getUniqueId()).islandProtected);
        e.setCancelled(true);
    }
}
Also used : Player(org.bukkit.entity.Player) EnderCrystal(org.bukkit.entity.EnderCrystal) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Island(com.wasteofplastic.acidisland.Island) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 18 with ProjectileSource

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

the class SentinelEventHandler method whenAttacksHappened.

/**
 * Called when combat has occurred in the world (and has been processed by all other plugins), to handle things like cancelling invalid damage to/from a Sentinel NPC,
 * adding targets (if combat occurs near an NPC), and if relevant handling config options that require overriding damage events.
 */
@EventHandler(priority = EventPriority.HIGHEST)
public void whenAttacksHappened(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) {
        return;
    }
    Entity damager = event.getDamager();
    if (event.getDamager() instanceof Projectile) {
        ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
        if (source instanceof Entity) {
            damager = (Entity) source;
        }
    }
    SentinelTrait victim = SentinelUtilities.tryGetSentinel(event.getEntity());
    SentinelTrait attacker = SentinelUtilities.tryGetSentinel(damager);
    if (attacker != null) {
        if (!(event.getEntity() instanceof LivingEntity)) {
            event.setCancelled(true);
            return;
        }
        if (victim != null && victim.getNPC().getId() == attacker.getNPC().getId()) {
            event.setCancelled(true);
            return;
        }
        attacker.whenAttacksHappened(event);
    }
    if (victim != null) {
        victim.whenAttacksHappened(event);
    }
    for (SentinelTrait sentinel : cleanCurrentList()) {
        UUID guarding = sentinel.getGuarding();
        if (guarding != null && event.getEntity().getUniqueId().equals(guarding)) {
            sentinel.whenAttacksHappened(event);
        }
    }
    if (damager instanceof LivingEntity) {
        LivingEntity damagerLiving = (LivingEntity) damager;
        for (SentinelTrait sentinel : cleanCurrentList()) {
            if (sentinel.allTargets.isEventTarget(event) && sentinel.targetingHelper.canSee(damagerLiving) && !sentinel.targetingHelper.isIgnored(damagerLiving)) {
                sentinel.targetingHelper.addTarget(damager.getUniqueId());
            }
            if (sentinel.allAvoids.isEventTarget(event) && sentinel.targetingHelper.canSee(damagerLiving) && !sentinel.targetingHelper.isIgnored(damagerLiving)) {
                sentinel.targetingHelper.addAvoid(damager.getUniqueId());
            }
        }
    }
    if (event.getEntity() instanceof LivingEntity) {
        LivingEntity entity = (LivingEntity) event.getEntity();
        for (SentinelTrait sentinel : cleanCurrentList()) {
            if (sentinel.allTargets.isReverseEventTarget(sentinel, event) && sentinel.targetingHelper.canSee(entity) && !sentinel.targetingHelper.isIgnored(entity)) {
                sentinel.targetingHelper.addTarget(entity.getUniqueId());
            }
            if (sentinel.allAvoids.isReverseEventTarget(sentinel, event) && sentinel.targetingHelper.canSee(entity) && !sentinel.targetingHelper.isIgnored(entity)) {
                sentinel.targetingHelper.addAvoid(entity.getUniqueId());
            }
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) ProjectileSource(org.bukkit.projectiles.ProjectileSource) UUID(java.util.UUID) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 19 with ProjectileSource

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

the class SentinelEventHandler method whenAttacksAreHappening.

/**
 * Called when combat occurs in the world (and has not yet been processed by other plugins),
 * to handle things like cancelling invalid damage to/from a Sentinel NPC,
 * changing damage values given to or received from an NPC,
 * and if relevant handling config options that require overriding damage events.
 */
@EventHandler(priority = EventPriority.LOW)
public void whenAttacksAreHappening(EntityDamageByEntityEvent event) {
    if (event.isCancelled()) {
        return;
    }
    UUID victimUuid = event.getEntity().getUniqueId();
    for (SentinelTrait sentinel : cleanCurrentList()) {
        sentinel.whenSomethingMightDie(victimUuid);
    }
    SentinelTrait victim = SentinelUtilities.tryGetSentinel(event.getEntity());
    SentinelTrait attacker = SentinelUtilities.tryGetSentinel(event.getDamager());
    if (victim != null) {
        if (attacker != null && victim.getNPC().getId() == attacker.getNPC().getId()) {
            event.setCancelled(true);
            return;
        }
        victim.whenAttacksAreHappeningToMe(event);
    }
    if (attacker != null) {
        attacker.whenAttacksAreHappeningFromMe(event);
    }
    if (event.getDamager() instanceof Projectile) {
        ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
        if (source instanceof Entity) {
            SentinelTrait shooter = SentinelUtilities.tryGetSentinel((Entity) source);
            if (shooter != null) {
                shooter.whenAttacksAreHappeningFromMyArrow(event);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) ProjectileSource(org.bukkit.projectiles.ProjectileSource) UUID(java.util.UUID) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 20 with ProjectileSource

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

the class SentinelEventHandler method onProjectileHitsBlock.

/**
 * Called when a projectile hits a block, to auto-remove Sentinel-fired arrows quickly.
 */
@EventHandler
public void onProjectileHitsBlock(ProjectileHitEvent event) {
    if (SentinelPlugin.instance.arrowCleanupTime <= 0) {
        return;
    }
    final Projectile projectile = event.getEntity();
    ProjectileSource source = projectile.getShooter();
    if (!(source instanceof Entity)) {
        return;
    }
    SentinelTrait sentinel = SentinelUtilities.tryGetSentinel((Entity) source);
    if (sentinel == null) {
        return;
    }
    Bukkit.getScheduler().scheduleSyncDelayedTask(SentinelPlugin.instance, new Runnable() {

        @Override
        public void run() {
            if (projectile.isValid()) {
                projectile.remove();
            }
        }
    }, SentinelPlugin.instance.arrowCleanupTime);
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

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