import { world, ItemStack, system, EquipmentSlot, GameMode } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe((eventData) => {
    eventData.itemComponentRegistry.registerCustomComponent("fm:on_use", {
        onUse(itemEvent,event) {
            

            
            const player = itemEvent.source;
            if (!player || !player.dimension) {
                return;
            }
            const mainHandItem = itemEvent.itemStack;
            
            const equippable = player?.getComponent("minecraft:equippable");
            const item = equippable.getEquipmentSlot(EquipmentSlot.Offhand);
            
                if (itemEvent.itemStack.typeId === 'fm:teleport_search') {
                        itemEvent.source.runCommand('summon fm:ender_p ^10^4^');
                }
                        if (itemEvent.itemStack.typeId === 'fm:wither_search') {
        let projectileEntity = player.dimension.spawnEntity("fm:wither_skull_dangerous", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
                                if (itemEvent.itemStack.typeId === 'fm:fire_ball_spell') {
        let projectileEntity = player.dimension.spawnEntity("fm:fireball", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:dragon_spell') {
        itemEvent.source.runCommand('summon fm:dragon_fball ^7^4^');
                }
        if (itemEvent.itemStack.typeId === 'fm:darkeye_spells') {
        let projectileEntity = player.dimension.spawnEntity("fm:eye", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:gamma_ray_spell') {
        let projectileEntity = player.dimension.spawnEntity("fm:gamma", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:green_tempest_spell') {
        let projectileEntity = player.dimension.spawnEntity("fm:gt", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:daggers_spell') {
        let projectileEntity = player.dimension.spawnEntity("fm:dagger_p", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:ice_spike') {
        let projectileEntity = player.dimension.spawnEntity("fm:spike", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:black_hole') {
        let projectileEntity = player.dimension.spawnEntity("fm:bh", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:expeliarmus') {
        let projectileEntity = player.dimension.spawnEntity("fm:expeliarmus_p", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        if (itemEvent.itemStack.typeId === 'fm:meteor_search') {
        let projectileEntity = player.dimension.spawnEntity("fm:mt", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
            if (itemEvent.itemStack.typeId === 'fm:fire_wave') {
                itemEvent.source.runCommand('function fire');
            }
            if (itemEvent.itemStack.typeId === 'fm:expecto_patronum') {
                itemEvent.source.runCommand('function patronum');
            }
            if (itemEvent.itemStack.typeId === 'fm:thunder_search') {
                itemEvent.source.runCommand('function light_ray');
                itemEvent.source.runCommand('execute at @e[family=!player,r=5] run summon lightning_bolt ~~~');
                itemEvent.source.runCommand('execute at @e[family=!player,r=5] run summon lightning_bolt ~~~');
                itemEvent.source.runCommand('execute at @e[family=!player,r=5] run summon lightning_bolt ~~~');
                itemEvent.source.runCommand('execute at @e[family=!player,r=5] run particle mob:c_3 ~ ~0.1 ~');

            }
            if (itemEvent.itemStack.typeId === 'fm:levitate_search') {
                itemEvent.source.runCommand('function levitate');
            }
            if (itemEvent.itemStack.typeId === 'fm:time_search') {
                itemEvent.source.runCommand('function skip');
            }
            if (itemEvent.itemStack.typeId === 'fm:guide_book') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page1');
                itemEvent.source.runCommand('playsound mob.guide @p');	   
            }
            if (itemEvent.itemStack.typeId === 'fm:page1') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page2');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page2') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page3');
                itemEvent.source.runCommand('playsound mob.guide @p');				
            }
            if (itemEvent.itemStack.typeId === 'fm:page3') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page4');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page4') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page5');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page5') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page6');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page6') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page7');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page7') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page8');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page8') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page9');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page9') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:page10');
                itemEvent.source.runCommand('playsound mob.guide @p');					
            }
            if (itemEvent.itemStack.typeId === 'fm:page10') {
                itemEvent.source.runCommand('replaceitem entity @s slot.weapon.mainhand 0 fm:guide_book');
                itemEvent.source.runCommand('playsound mob.guide @p');	
            }
            
            if (itemEvent.itemStack.typeId === 'fm:magic_wand') {
                
                itemEvent.source.runCommand('function extrair');
                itemEvent.source.runCommand('function extrair2');
                itemEvent.source.runCommand('kill @e[family=aura,r=5]');
            }
                    if (itemEvent.itemStack.typeId === `fm:magic_wand`) {
            if (item.typeId === `fm:avada_kedrava`) {
        let projectileEntity = player.dimension.spawnEntity("fm:avada_kedrava", {
          x: player.location.x + (player.getViewDirection().x),
          y: player.location.y + 1.7 + (player.getViewDirection().y),
          z: player.location.z + (player.getViewDirection().z)
        });
        let projectileComponent = projectileEntity.getComponent("minecraft:projectile");
        let velocity = { x: player.getViewDirection().x, y: player.getViewDirection().y, z: player.getViewDirection().z };
        projectileComponent.owner = player;
        projectileComponent.shoot( velocity );}
        }        
            
           if (!player.isSneaking) {
                if (itemEvent.itemStack.typeId === `fm:sword_of_souls`) {
                itemEvent.source.runCommand('function sword_atk1');
                itemEvent.source.runCommand('execute at @s run particle mob:corte_ani ^^1 ^4');
                player.addEffect("resistance", 3, {
                amplifier: 10,
                showParticles: false
                });}
           }
           if (player.isSneaking) {
                if (itemEvent.itemStack.typeId === `fm:sword_of_souls`) {
                itemEvent.source.runCommand('function sword_atk2');
                itemEvent.source.runCommand('execute at @s run particle mob:shadow ^^1 ^4');
                }
           }
           if (itemEvent.itemStack.typeId === `fm:time_ring`) {
                itemEvent.source.runCommand('tag @s add tempo');
                itemEvent.source.runCommand('enchant @s[tag=tempo] sharpness');
                itemEvent.source.runCommand('execute as @s[tag=tempo] at @e[tag=!tempo,r=15] run tp ~~~');
                itemEvent.source.runCommand('particle minecraft:electric_spark_particle ~~1 ~');
                itemEvent.source.runCommand('effect @e[tag=!tempo,r=15] slowness 2 230 true');
                itemEvent.source.runCommand('effect @e[tag=!tempo,r=15] slow_falling 2 255 true');
                itemEvent.source.runCommand('title @s actionbar Time Stop');
                }
            if (itemEvent.itemStack.typeId === `fm:telekinesis_ring`) {
                itemEvent.source.runCommand('enchant @e[tag=gravity] sharpness 3');
                itemEvent.source.runCommand('tag @s add gravity');
                itemEvent.source.runCommand('execute at @e[tag=gravity] run tp @e[r=7,tag=!gravity] ^^^6 facing @s');
        }
           if (itemEvent.itemStack.typeId === `fm:magicromicon`) {
                itemEvent.source.runCommand('function invocar');
            }
        }
    });
});