Search in sources :

Example 1 with MyPetOnHitSkillEvent

use of de.Keyle.MyPet.api.event.MyPetOnHitSkillEvent in project MyPet by xXKeyleXx.

the class MyPetEntityListener method onMyPetDamageMonitor.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMyPetDamageMonitor(final EntityDamageByEntityEvent event) {
    if (event.getEntity() == null) {
        // catch invalid events (i.e. EnchantmentAPI)
        return;
    }
    Entity target = event.getEntity();
    if (WorldGroup.getGroupByWorld(target.getWorld()).isDisabled()) {
        return;
    }
    if (target instanceof LivingEntity) {
        Entity source = event.getDamager();
        if (Configuration.LevelSystem.Experience.DAMAGE_WEIGHTED_EXPERIENCE_DISTRIBUTION && !(target instanceof Player) && !(target instanceof MyPetBukkitEntity)) {
            LivingEntity livingSource = null;
            if (source instanceof Projectile) {
                Projectile projectile = (Projectile) source;
                if (projectile.getShooter() instanceof LivingEntity) {
                    livingSource = (LivingEntity) projectile.getShooter();
                }
            } else if (source instanceof LivingEntity) {
                livingSource = (LivingEntity) source;
            }
            if (livingSource != null) {
                MyPetExperience.addDamageToEntity(livingSource, (LivingEntity) target, event.getDamage());
            }
        }
        if (source instanceof Projectile) {
            ProjectileSource projectileSource = ((Projectile) source).getShooter();
            if (projectileSource instanceof Entity) {
                source = (Entity) projectileSource;
            }
        }
        if (source instanceof MyPetBukkitEntity) {
            MyPet myPet = ((MyPetBukkitEntity) source).getMyPet();
            if (myPet.getStatus() != PetState.Here) {
                return;
            }
            // fix influence of other plugins for this event and throw damage event
            MyPetDamageEvent petDamageEvent = new MyPetDamageEvent(myPet, target, event.getOriginalDamage(EntityDamageEvent.DamageModifier.BASE));
            Bukkit.getPluginManager().callEvent(petDamageEvent);
            if (petDamageEvent.isCancelled()) {
                event.setCancelled(true);
                return;
            } else {
                event.setDamage(petDamageEvent.getDamage());
            }
            if (!isSkillActive) {
                for (Skill skill : myPet.getSkills().all()) {
                    if (skill instanceof OnHitSkill) {
                        OnHitSkill onHitSkill = (OnHitSkill) skill;
                        if (onHitSkill.trigger()) {
                            MyPetOnHitSkillEvent skillEvent = new MyPetOnHitSkillEvent(myPet, onHitSkill, (LivingEntity) target);
                            Bukkit.getPluginManager().callEvent(skillEvent);
                            if (!skillEvent.isCancelled()) {
                                isSkillActive = true;
                                onHitSkill.apply((LivingEntity) target);
                                isSkillActive = false;
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) OnDamageByEntitySkill(de.Keyle.MyPet.api.skill.OnDamageByEntitySkill) Skill(de.Keyle.MyPet.api.skill.skilltree.Skill) OnHitSkill(de.Keyle.MyPet.api.skill.OnHitSkill) MyPetOnHitSkillEvent(de.Keyle.MyPet.api.event.MyPetOnHitSkillEvent) MyPetDamageEvent(de.Keyle.MyPet.api.event.MyPetDamageEvent) OnHitSkill(de.Keyle.MyPet.api.skill.OnHitSkill) ProjectileSource(org.bukkit.projectiles.ProjectileSource) EntityMyPetProjectile(de.Keyle.MyPet.api.entity.skill.ranged.EntityMyPetProjectile) CraftMyPetProjectile(de.Keyle.MyPet.api.entity.skill.ranged.CraftMyPetProjectile) EventHandler(org.bukkit.event.EventHandler)

Aggregations

CraftMyPetProjectile (de.Keyle.MyPet.api.entity.skill.ranged.CraftMyPetProjectile)1 EntityMyPetProjectile (de.Keyle.MyPet.api.entity.skill.ranged.EntityMyPetProjectile)1 MyPetDamageEvent (de.Keyle.MyPet.api.event.MyPetDamageEvent)1 MyPetOnHitSkillEvent (de.Keyle.MyPet.api.event.MyPetOnHitSkillEvent)1 MyPetPlayer (de.Keyle.MyPet.api.player.MyPetPlayer)1 OnDamageByEntitySkill (de.Keyle.MyPet.api.skill.OnDamageByEntitySkill)1 OnHitSkill (de.Keyle.MyPet.api.skill.OnHitSkill)1 Skill (de.Keyle.MyPet.api.skill.skilltree.Skill)1 EventHandler (org.bukkit.event.EventHandler)1 ProjectileSource (org.bukkit.projectiles.ProjectileSource)1