//Agradecimientos especiales a Gabriel Cape, este wey me ayudó musho :D
import { world, system, Player, ItemDurabilityComponent, EquipmentSlot, EntityEquippableComponent, EntityInventoryComponent } from "@minecraft/server";


//Spider Matriarch Basic Hit Entity -Poison Effect-
world.afterEvents.entityHitEntity.subscribe((data) => {
    const entity = data.hitEntity;
    const creature = data.damagingEntity;
    if (creature.typeId == 'fc:spider_matriarch_simple_attack') {
        entity.addEffect('poison', 20, { amplifier: 3, showParticles: true })
    } return;
});

//Classic Wyvern Basic Hit Entity -Poison Effect-
//experimentando
world.afterEvents.entityHurt.subscribe(ev => {
    const hurtEntity = ev.hurtEntity;
    const damageSource = ev.damageSource;
    const cause = damageSource.cause
    const damagingEntity = damageSource.damagingEntity;
    if (damagingEntity?.typeId === "fc:classic_wyvern") {
        hurtEntity.addEffect("poison", 400, { amplifier: 1, showParticles: true });
    }
});

//Item Abilities
world.afterEvents.itemUse.subscribe((data) => {
    const item = data.itemStack;
    const player = data.source;

    if (!item.typeId.startsWith(`fc:`)) return;
    //Orc Warlord Sword
    if (item.typeId === `fc:orc_warlord_sword`){
        const orcwarlordswordCooldown = world.scoreboard.getObjective(`orcwarlordsword_cooldown`);
        //Charge attack
        if (player.isSneaking && player.isOnGround && !player.isInWater && !player.hasTag(`orc_warlord_sword_attacking`) && !player.hasTag(`orc_warlord_sword_charge_attack`) && orcwarlordswordCooldown?.getScore(player) < 1) {
            player.addEffect(`slowness`, 34, { amplifier: 5, showParticles: false });
            player.playAnimation(`animation.npc.fcplayer.orc_warlord_new_special_attack`, { nextState: `none`, stopExpression: `query.any_animation_finished`, blendOutTime: 0, controller: `controller_name` });
            player.addTag(`orc_warlord_sword_charge_attack`);
            bardicheCooldown.setScore(player, 250);

            system.runTimeout(() => {
                const interval = system.runInterval(() => {
                    const playerVel = Math.sqrt(Math.pow(player.getVelocity().x, 2) + Math.pow(player.getVelocity().z, 2));
                    const playerDir = player.getViewDirection();
                    const equipment = player.getComponent("minecraft:equippable");
                    const mainHand = equipment?.getEquipmentSlot(`Mainhand`)?.getItem();
                    const sharpness = item.getComponent("enchantable").getEnchantment(`sharpness`)?.level ?? 0
                }, 1)
                system.runTimeout(() => {
                    system.clearRun(interval);
                    system.runTimeout(() => {
                        const equipment = player.getComponent("minecraft:equippable");
                        const mainHand = equipment?.getEquipmentSlot(`Mainhand`)?.getItem();
                        if (mainHand?.typeId === item?.typeId) {
                            player.runCommandAsync(`summon fc:orc_warlord_knockback_attack ^ ^0.5 ^2`);
                        }
                        system.runTimeout(() => {
                            world.playSound(`mob.captain_orc.break`, { x: player.location.x, y: player.location.y, z: player.location.z });
                            player.removeTag(`orc_warlord_sword_attack`);
                            return;
                        }, 28.466)
                    }, 4.8)
                }, 27.4)
            }, 35)
        };
    }
}
)






