import {world} from '@minecraft/server';
world.afterEvents.itemUse.subscribe((ev)=>{
    const player = ev.source
    const item = ev.itemStack
    switch(item.typeId){
        case 'rex:adventurers_sword':
            player.runCommand(`effect @p speed 1 3 true`)
            break
       case 'rex:orc_king_necklace':
            player.runCommand(`effect @p strength 30 2 true`)
            break
    case 'rex:light_shot':
            player.runCommand(`effect @p fire_resistance 30 2 true`)
            break
     case 'rex:teleport_magic_wand':
            player.runCommand(`tp @p ^0.5 ^3 ^0.5`)
            break
      case 'rex:sword_of_justice':
            player.runCommand(`effect @p resistance 5 2 true`)
            break
      case 'rex:resistance_magic_wand':
            player.runCommand(`effect @p resistance 15 1 true`)
            break
      case 'rex:schedule':
            player.runCommand(`function diario`)
            break
       case 'rex:resistance_cookie':
            player.runCommand(`effect @p resistance 30 1 true`)
           player.runCommand(`effect @p fire_resistance 30 2 true`)
            break
       case 'rex:random_potions':
            player.runCommand(`summon rex:loot_potions ~~~`)
            break
      case 'rex:random_sword':
            player.runCommand(`summon rex:loot_efeitos ~~~`)
           player.runCommand(`event entity @e[family=efeitos,c=1] rex:interagir`)
           player.runCommand(`kill @e[family=efeitos]`)
            break
       case 'rex:earth_staff':
            player.runCommand(`camerashake add @p[r=6,c=6] 2 6 positional`)
            player.runCommand(`effect @e[r=8] fatal_poison 6 1 true`)
            player.runCommand(`effect @p clear`)
           
            break
       case 'rex:paladin_sword':
            player.runCommand(`effect @p resistance 2 2 true`)
            break
       case 'rex:magic_scroll_water':
            player.runCommand(`clear @p rex:magic_scroll_water 0 1`)
            player.runCommand(`fill ~~~~~~ water`)
            break
     case 'rex:mining_potion':
            player.runCommand(`effect @p night_vision 200 100`)
            player.runCommand(`effect @p fire_resistance 200 100`)
            break
     case 'rex:magic_scroll_wolf':
            player.runCommand(`clear @p rex:magic_scroll_wolf 0 1`)
            player.runCommand(`summon wolf ~~~`)
            player.runCommand(`give @p bone 6`)
            break
     case 'rex:magma_sword':
            player.runCommand(`effect @p fire_resistance 20 20 true`)
            break
      case 'rex:magic_scroll_tp_items':
            player.runCommand(`tp @e[type=item] ~~~`)
            player.runCommand(`clear @p rex:magic_scroll_tp_items 0 1`)
            
           
            break
case 'rex:magic_scroll_segueira':
            player.runCommand(`effect @e[r=8] blindness 35 1`)
            player.runCommand(`effect @p clear`)
            player.runCommand(`effect @p blindness 2 1`)
            player.runCommand(`clear @p rex:magic_scroll_segueira 0 1`)
           
            break
    
      case 'rex:miner_amulet':
            player.runCommand(`tell @p[tag=portugues] §eAmuleto do mineiro:§f dá o efeito de visão noturna toda vez que você quebra um bloco com sua picareta, só funciona em picaretas do addon Rex RPG Quest, basta deixar em seu inventário para ativar o efeito`)
            player.runCommand(`tell @p[tag=ingles] §eMiner's Amulet:§f gives the night vision effect every time you break a block with your pickaxe, it only works on pickaxes from the Rex RPG Quest addon, just leave it in your inventory to activate the effect`)
            player.runCommand(`tell @p[tag=espanhol] §eMiner's Amulet: §fotorga el efecto de visión nocturna cada vez que rompes un bloque con tu pico, solo funciona en picos del complemento Rex RPG Quest, solo déjalo en tu inventario para activar el efecto`)
            player.runCommand(`tell @p[tag=!portugues,tag=!espanhol,tag=!ingles] §cchoose your language on an NPC please... escolha seu idioma em um NPC por favor... elige tu idioma en un NPC`)
           
            break
        case 'rex:hero_medal':
            player.runCommand(`effect @p village_hero 100 3 true`)
            player.runCommand(`summon xp_orb ~~~`)
            player.runCommand(`particle minecraft:totem_particle ~ ~1 ~`)
            break
case 'rex:hero_medal':
            player.runCommand(`effect @p fire_resistance 20 20 true`)
            player.runCommand(`effect @p resistance 20 1`)
            break
        case 'rex:magic_scroll_time_power':
            player.runCommand(`time add 2000`)
            player.runCommand(`clear @p rex:magic_scroll_time_power 0 1`)
            break
        case 'rex:magic_scroll_rain_of_arrows':
            player.runCommand(`summon arrow ~1 ~5 ~4`)
            player.runCommand(`summon arrow ~-1 ~5 ~-4`)
            player.runCommand(`summon arrow ~2 ~5 ~2`)
            player.runCommand(`summon arrow ~-2 ~5 ~2`)
            player.runCommand(`summon arrow ~1 ~5 ~-3`)
            player.runCommand(`summon arrow ~ ~5 ~4`)
            player.runCommand(`summon arrow ~4 ~5 ~`)
            player.runCommand(`summon arrow ~1 ~5 ~-4`)
            player.runCommand(`summon arrow ~-4 ~5 ~`)
            player.runCommand(`summon arrow ~1 ~5 ~3`)
            player.runCommand(`summon arrow ~ ~5 ~-3`)
            player.runCommand(`summon arrow ~-3 ~5 ~2`)
            player.runCommand(`summon arrow ~2 ~5 ~-1`)
            player.runCommand(`summon arrow ~-1 ~5 ~`)
            player.runCommand(`summon arrow ~-4 ~5 ~`)
            player.runCommand(`clear @p rex:magic_scroll_rain_of_arrows 0 1`)
            break

        case 'rex:bright_berry_pie':
            player.runCommand(`effect @p night_vision 60 1 true`)
            break
       case 'rex:magic_scroll_hit_kill':
            player.runCommand(`clear @p rex:magic_scroll_hit_kill 0 1`)
            break

     case 'rex:magic_scroll_shulker_power':
            player.runCommand(`summon xp_orb ~~~`)
            player.runCommand(`effect @e[r=10] levitation 8 4`)
            player.runCommand(`effect @p clear`)
            player.runCommand(`clear @p rex:magic_scroll_shulker_power 0 1`)
            break
           case 'rex:shield_amulet':
            player.runCommand(`tell @p[tag=portugues] §eamuleto de resistência: §fdá o efeito de resistência toda vez que o jogador usar uma espada, só funciona em espadas do addon Rex RPG Quest (exceto a espada aleatória), basta deixar em seu inventário para ativar o efeito`)
            player.runCommand(`tell @p[tag=ingles] §eamulet of resistance:§f gives the effect of resistance every time the player uses a sword, it only works on swords from the Rex RPG Quest addon (except the random sword), just leave it in your inventory to activate the effect`)
            player.runCommand(`tell @p[tag=espanhol] §eamuleto de resistencia:§f da el efecto de resistencia cada vez que el jugador usa una espada, solo funciona en espadas del complemento Rex RPG Quest (excepto la espada aleatoria), solo déjalo en tu inventario para activar el efecto`)
            player.runCommand(`tell @p[tag=!portugues,tag=!espanhol,tag=!ingles] §cchoose your language on an NPC please... escolha seu idioma em um NPC por favor... elige tu idioma en un NPC`)
            break
       case 'rex:magic_scroll_spawn_golem':
            player.runCommand(`summon rex:magic_golen ~~~`)
            player.runCommand(`clear @p rex:magic_scroll_spawn_golem 0 1`)
            break
case 'rex:magic_scroll_tnt':
            player.runCommand(`summon minecraft:tnt ~~~`)
            player.runCommand(`clear @p rex:magic_scroll_tnt 0 1`)
            break
        case 'rex:necromancer_amulet':
            player.runCommand(`tell @p[tag=portugues] §eamuleto do necromante:§f aumenta sua vida toda vez que você acertar com sua adaga, mas... tem um preço... também dá o efeito de mau presságio, só funciona em adagas do addon Rex RPG Quest, basta colocá-lo em seu inventário para ativar o efeito`)
            player.runCommand(`tell @p[tag=ingles] §enecromancer amulet:§f increases your life every time you hit with your dagger, but... it has a price... it also gives the effect of bad omen, it only works on daggers from the Rex RPG Quest addon, just put it in your inventory to activate the effect`)
            player.runCommand(`tell @p[tag=espanhol] §eamuleto del nigromante: §faumenta tu vida cada vez que golpeas con tu daga, pero... tiene un precio... también da el efecto de mal augurio, solo funciona en dagas del complemento Rex RPG Quest, solo pon en tu inventario para activar o efeito`)
            player.runCommand(`tell @p[tag=!portugues,tag=!espanhol,tag=!ingles] §cchoose your language on an NPC please... escolha seu idioma em um NPC por favor... elige tu idioma en un NPC`)
            break
         case 'rex:magic_scroll_telepotation':
            player.runCommand(`tp @p ~ ~40 ~`)
            player.runCommand(`effect @p slow_falling 40 1`)
            player.runCommand(`clear @p rex:magic_scroll_telepotation 0 1`)
            break

        case 'rex:magic_scroll_snow_golem':
            player.runCommand(`clear @p rex:magic_scroll_snow_golem 0 1`)
           player.runCommand(`summon snow_golem ~~~`)
            
            break
case 'rex:magic_scroll_earthquake':
            player.runCommand(`camerashake add @p[r=6,c=6] 2 6 positional`)
            player.runCommand(`effect @e[r=8] fatal_poison 6 1 true`)
            player.runCommand(`effect @p clear`)
            player.runCommand(`clear @p rex:magic_scroll_earthquake 0 1`)
            break
     case 'rex:magic_scroll_fire':
            player.runCommand(`fill ~4 ~ ~4 ~-4 ~ ~-4 fire replace air`)
            player.runCommand(`fill ~4 ~ ~4 ~4 ~ ~4 fire replace air`)
            player.runCommand(`fill ~-4 ~ ~4 ~4 ~ ~-4 fire replace air`)
            player.runCommand(`fill ~4 ~ ~-4 ~-4 ~ ~4 fire replace air`)
            player.runCommand(`fill ~-4 ~ ~-4 ~4 ~ ~4 fire replace air`)
            player.runCommand(`fill ~-2 ~ ~2 ~-2 ~ ~2 fire replace air`)
            player.runCommand(`effect @p fire_resistance 20 1`)
            player.runCommand(`clear @p rex:magic_scroll_fire 0 1`)
            break
      case 'rex:orb_of_nature':
            player.runCommand(`fill ~3 ~-1 ~3 ~-3 ~ ~-3 tallgrass replace air`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=3..},r=1] regeneration 6 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=32..},r=1] strength 6 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=64..},r=1] speed 20 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=75..},r=1] regeneration 20 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=90..},r=1] haste 100 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=120..},r=1] night_vision 170 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=150..},r=1] fire_resistance 100 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=175..},r=1] resistance 30 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=200..},r=1] health_boost 100 3 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=230..},r=1] instant_health 5 1 true`)
            player.runCommand(`effect @p[hasitem={item=vine,quantity=250..},r=1] saturation 6 1 false`)
            player.runCommand(`clear @p[hasitem={item=vine,quantity=3..},r=1] vine 0 4`)
            player.runCommand(`clear @p[hasitem={item=vine,quantity=64..},r=1] vine 0 2`)
            player.runCommand(`clear @p[hasitem={item=vine,quantity=75..},r=1] vine 0 3`)
            player.runCommand(`clear @p[hasitem={item=vine,quantity=120..},r=1] vine 0 4`)
            player.runCommand(`clear @p[hasitem={item=vine,quantity=200..},r=1] vine 0 3`)
            player.runCommand(`clear @p[hasitem={item=vine,quantity=250..},r=1] vine 0 2`)
            player.runCommand(`particle minecraft:crop_growth_area_emitter ~ ~1 ~`)
            break
      case 'rex:magic_scroll_dragon_power':
            player.runCommand(`clear @p rex:magic_scroll_dragon_power 0 1`)
            break
    case 'rex:magic_scroll_dragon_power':
            player.runCommand(`tell @p[tag=portugues] §eamuleto misterioso: §frouba um pouco da vida do seu oponente toda vez que você acerta um hit com sua adaga, só funciona nas adagas do addon Rex RPG Quest, basta deixar em seu inventário para ativar o efeito`)
            player.runCommand(`tell @p[tag=ingles] §emysterious amulet: §fsteals a little of your opponent's life every time you hit with your dagger, only works on daggers from the Rex RPG Quest addon, just leave it in your inventory to activate the effect`)
            player.runCommand(`tell @p[tag=espanhol] §eamuleto misterioso: §froba un poco de la vida de tu oponente cada vez que golpeas con tu daga, solo funciona en dagas del complemento Rex RPG Quest, solo déjalo en tu inventario para activar el efecto`)
            player.runCommand(`tell @p[tag=!portugues,tag=!espanhol,tag=!ingles] §cchoose your language on an NPC please... escolha seu idioma em um NPC por favor... elige tu idioma en un NPC`)
            break

    case 'rex:magic_scroll_village_hero':
            player.runCommand(`clear @p rex:magic_scroll_village_hero 0 1`)
            player.runCommand(`effect @p village_hero 120 1`)
            break

case 'rex:magic_scroll_iron_golrm':
            player.runCommand(`clear @p rex:magic_scroll_iron_golrm 0 1`)
            player.runCommand(`summon iron_golem ~~~`)
            break
case 'rex:magic_scroll_regeneration':
            player.runCommand(`clear @p rex:magic_scroll_regeneration 0 1`)
            player.runCommand(`effect @p regeneration 60 2`)
            break
       case 'rex:magic_scroll_item_raffle':
            player.runCommand(`clear @p rex:magic_scroll_item_raffle 0 1`)
            player.runCommand(`summon rex:loot_itemraffe ~~~`)
            break

case 'rex:magic_scroll_lightning':
            player.runCommand(`summon lightning_bolt ~~~`)
            player.runCommand(`effect @p fire_resistance 30 1`)
            player.runCommand(`effect @p resistance 30 1`)
            player.runCommand(`clear @p rex:magic_scroll_lightning 0 1`)
            break



      case 'rex:magic_scroll_defense_evoker':
            player.runCommand(`summon evocation_fang ~1 ~ ~1`)
           player.runCommand(`summon evocation_fang ~2 ~ ~2`)
           player.runCommand(`summon evocation_fang ~3 ~ ~3`)
           player.runCommand(`summon evocation_fang ~4 ~ ~4`)
           player.runCommand(`summon evocation_fang ~-1 ~ ~1`)
           player.runCommand(`summon evocation_fang ~-2 ~ ~2`)
          player.runCommand(`summon evocation_fang ~-3 ~ ~3`)
           player.runCommand(`summon evocation_fang ~-4 ~ ~4`)
           player.runCommand(`summon evocation_fang ~1 ~ ~-1`)
           player.runCommand(`summon evocation_fang ~2 ~ ~-2`)
          player.runCommand(`summon evocation_fang ~3 ~ ~-3`)
           player.runCommand(`summon evocation_fang ~4 ~ ~-4`)
           player.runCommand(`summon evocation_fang ~-1 ~ ~-1`)
           player.runCommand(`summon evocation_fang ~-2 ~ ~-2`)
          player.runCommand(`summon evocation_fang ~-3 ~ ~-3`)
           player.runCommand(`summon evocation_fang ~-4 ~ ~-4`)
           player.runCommand(`summon evocation_fang ~1 ~ ~2`)
           player.runCommand(`summon evocation_fang ~2 ~ ~1`)
          player.runCommand(`summon evocation_fang ~-1 ~ ~2`)
           player.runCommand(`summon evocation_fang ~-2 ~ ~1`)
           player.runCommand(`effect @p resistance 20 1`)
           player.runCommand(`clear @p rex:magic_scroll_defense_evoker 0 1`)
            break
      case 'rex:king_sword':
            player.runCommand(`effect @p resistance 6 1 true`)
           player.runCommand(`effect @p regeneration 6 1 true`)
            break
    case 'rex:magic_scroll_clear_weather':
            player.runCommand(`weather clear`)
           player.runCommand(`clear @p rex:magic_scroll_clear_weather 0 1`)
            break
case 'rex:magic_scroll_haste':
            player.runCommand(`effect @p haste 80 1`)
           player.runCommand(`clear @p rex:magic_scroll_haste 0 1`)
            break
      case 'rex:cleric_staff':
            player.runCommand(`effect @e[family=villager,c=3] regeneration 10 5`)
            player.runCommand(`execute as @e[family=villager,c=2] positioned ~ ~2 ~ run particle minecraft:villager_happy ~ ~ ~`)
            player.runCommand(`effect @e[family=guard,c=1] regeneration 10 5`)
            player.runCommand(`effect @e[family=archer,c=1] regeneration 10 5`)
            player.runCommand(`execute at @e[family=archer,c=1,family=!clerigo] positioned ~ ~2 ~ run particle minecraft:villager_happy ~ ~ ~`)
            player.runCommand(`effect @p[c=2,r=10] regeneration 15 5`)
            player.runCommand(`particle minecraft:totem_particle ~ ~1 ~`)
            break

       case 'rex:golem_staff':
            player.runCommand(`summon rex:magic_golen ~~~`)
            break
       case 'rex:haste_potion':
            player.runCommand(`effect @p haste 200 1`)
            break

        case 'rex:golden_apple_juice':
       case 'rex:golden_apple_pie':
            player.runCommand(`effect @p regeneration 25 1 true`)
            player.runCommand(`effect @p absorption 25 3 true`)
            break
        case 'rex:fire_shield':
            player.runCommand(`effect @p fire_resistance 60 1 true`)
            break
       case 'rex:healing_staff':
            player.runCommand(`tell @p[tag=ingles] §cOnly Marcio the cleric can use this staff`)
            player.runCommand(`tell @p[tag=espanhol] §cSólo Marcio el clérigo puede utilizar este bastón`)
            player.runCommand(`tell @p[tag=portugues] §cSomente Márcio, o clérigo, pode usar este cajado`)
            break
       case 'rex:cure_for_the_whiter':
      case 'rex:magic_cookie':
            player.runCommand(`effect @p clear`)
            break
      case 'rex:drum_eye':
            player.runCommand(`effect @p[scores={reputacao=..-1},r=1] wither 10 1 true`)
            player.runCommand(`effect @p[scores={reputacao=0..},r=1] night_vision 20 1 true`)
            player.runCommand(`effect @p[scores={reputacao=30..},r=1] slow_falling 10 1 true`)
            player.runCommand(`effect @p[scores={reputacao=100..},r=1] regeneration 20 1 true`)
            player.runCommand(`effect @p[scores={reputacao=120..},r=1] strength 20 1 true`)
            player.runCommand(`effect @p[scores={reputacao=140..},r=1] speed 40 1 true`)
            player.runCommand(`effect @p[scores={reputacao=170..},r=1] absorption 40 1 true`)
            player.runCommand(`effect @p[scores={reputacao=190..},r=1] conduit_power 60 1 true`)
            player.runCommand(`effect @p[scores={reputacao=230..},r=1] strength 40 2 true`) 
           player.runCommand(`effect @p[scores={reputacao=250..},r=1] jump_boost 40 1 true`)
            player.runCommand(`effect @p[scores={reputacao=280..},r=1] invisibility 40 1 true`)
            player.runCommand(`effect @p[scores={reputacao=300..},r=1] haste 60 1 true`)
           player.runCommand(`kill @p[scores={reputacao=..-10},r=1]`)
            break
       case 'rex:death_to_the_dead':
            player.runCommand(`tag @p add imune3`)
            player.runCommand(`damage @e[r=5,tag=!imune3] 5 fall`)
            player.runCommand(`damage @e[r=4,tag=!imune3,family=undead] 20 fall`)
            player.runCommand(`particle minecraft:knockback_roar_particle ~ ~1 ~`)
            break
        case 'rex:end_staff':
            player.runCommand(`function cajado_vento`)
            break
       case 'rex:blacksmith_amulet':
            player.runCommand(`function blacksmith`)
      case 'rex:cleric_amulet':
            player.runCommand(`tell @p[tag=!portugues,tag=!espanhol,tag=!ingles] §cchoose your language on an NPC please... escolha seu idioma em um NPC por favor... elige tu idioma en un NPC`)
           player.runCommand(`tell @p[tag=espanhol] §eamuleto de clérigo: §felimina cualquier efecto de poción del jugador cuando usa su espada o herramienta, funciona en todas las espadas y herramientas en el complemento Rex RPG Quest, simplemente déjelo en su inventario para activar el efecto`)
           player.runCommand(`tell @p[tag=ingles] §ecleric's amulet:§f removes any potion effect from the player when he uses his sword or tool, works on all swords and tools in the Rex RPG Quest addon, just leave it in your inventory to activate the effect`)
           player.runCommand(`tell @p[tag=portugues] §eamuleto do clérigo: §fretira qualquer efeito de poção do jogador quando ele usa sua espada ou ferramenta, funciona em todas as espadas e ferramentas do addon Rex RPG Quest, basta deixar em seu inventário para ativar o efeito`)
            break
      
            case 'rex:cursed_vase':
            player.runCommand(`tag @p add usou_vaso`)
            player.runCommand(`summon rex:nightmares ~~~`)
            player.runCommand(`summon rex:nightmares ~~~`)
            player.runCommand(`summon rex:nightmares ~~~`)
            player.runCommand(`summon rex:nightmares ~~~`)
            player.runCommand(`clear @p rex:cursed_vase 0 1`)
            player.runCommand(`dialogue open @e[type=rex:oliver_the_traveler,c=1,r=20] @p[tag=misoli_and,c=1] oliver_pt_socorro`)
           break
            
       case 'rex:blessed_sword':
            player.runCommand(`effect @p regeneration 5 5`)
           break
    }
})

world.afterEvents.itemUse.subscribe((ev) => { durability() })
function durability() {
    for (let player of world.getPlayers()) {
        const item = player.getComponent('equippable').getEquipment("Mainhand")
        if (!item.hasComponent("durability")) return
        const db = item.getComponent("durability")
        let dbs = db.maxDurability - db.damage
        if (dbs == 0) return
        db.damage++
        player.getComponent('equippable').setEquipment("Mainhand", item)
    }
}

world.afterEvents.entityHurt.subscribe((ev)=>{
    if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
        if(item === 'rex:adventurers_sword'){
            victim.runCommand(`function espada_aventureiro`)
           
          }
        };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:assassins_dagger'){
            victim.runCommand(`tag @p add not_effect`)
           victim.runCommand(`effect @e[c=1,tag=!not_effect,r=5] poison 6 3
`)
          }  
        };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:iron_long_sword'){
            victim.runCommand(`effect @p weakness 3 100 true`)
           victim.runCommand(`effect @p slowness 4 3 true`)
          }  
        };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:netherite_long_sword'){
            victim.runCommand(`effect @p weakness 3 100 true`)
           victim.runCommand(`effect @p slowness 4 3 true`)
          }  
        };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:diamond_long_sword'){
            victim.runCommand(`effect @p weakness 3 100 true`)
           victim.runCommand(`effect @p slowness 4 3 true`)
          }  
        };
 if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:gold_long_sword'){
            victim.runCommand(`effect @p weakness 3 100 true`)
           victim.runCommand(`effect @p slowness 4 3 true`)
          }  
        };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:blessed_sword'){
          victim.runCommand(`damage @e[r=5,family=!player] 4 fall`)
            
        }
    };
    if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:enchanted_sword'){
          victim.runCommand(`summon xp_orb ~~~`)
            
        }
    };
    if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:forge_hammer'){
          victim.runCommand(`effect @p[hasitem={item=rex:cleric_amulet,quantity=!0},r=1] clear`)
         victim.runCommand(`tag @p add forge_imune`)
        victim.runCommand(`effect @e[c=1,r=5,tag=!forge_imune] weakness 5 5`)
       victim.runCommand(`tag @p remove forge_imune`)
            
        }
    };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:holy_mega_sword'){
          victim.runCommand(`particle minecraft:evoker_spell ~ ~1 ~`)
         victim.runCommand(`effect @p speed 25 1 true`)
        victim.runCommand(`effect @p haste 25 1 true`)
            
        }
    };
      if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:random_sword'){
          
        victim.runCommand(`summon rex:loot_efeitos ~~~`)
            
        }
    };
if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:king_sword'){
          victim.runCommand(`particle minecraft:evoker_spell ~ ~1 ~`)
         victim.runCommand(`tag @p add imuneking`)
        victim.runCommand(`summon evocation_fang ~~~`)
       victim.runCommand(`tag @p remove imuneking`)
            
        }
    };
   if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:sword_of_justice'){
          
       victim.runCommand(`effect @p strength 4 2 true`)
            
        }
    };
   if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurttEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:ghost_sword'){
          victim.runCommand(`damage @e[c=3,family=fantasma] 4 fall`)           
        }
    };
     if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
if(item === 'rex:diamond_long_sword'){
          victim.runCommand(`effect @p weakness 3 100 true`)
           victim.runCommand(`effect @p slowness 4 3 true`)
          }  
        }               
    
})

function shootProjetil(player, projetil, forca, altura) {

    const velocity = {

        x: player.getViewDirection().x * forca,
        y: player.getViewDirection().y * forca,
        z: player.getViewDirection().z * forca
    };
    const position = {
        x: player.location.x,
        y: player.location.y + altura,
        z: player.location.z 
    };
    let power = player.dimension.spawnEntity(projetil, position);
    power.clearVelocity();
    power.getComponent('projectile').owner = player;
    power.getComponent('projectile').shoot(velocity);
}

world.afterEvents.itemUse.subscribe((event) => {
    let player = event.source
    let item = event.itemStack
    if (item.typeId == 'rex:fire_staff') {
        shootProjetil(player, "rex:fire_shot", 2, 2)
    };
   if (item.typeId == 'rex:air_staff') {
        shootProjetil(player, "minecraft:wind_charge_projectile", 2, 2)
    };
   if (item.typeId == 'rex:infinite_enderpearl') {
        shootProjetil(player, "rex:ender_shot", 10, 2)
    };
   if (item.typeId == 'rex:fire_magic_wand') {
        shootProjetil(player, "rex:fire_shot2", 2, 2)
    };
    if (item.typeId == 'rex:thunder_staff') {
        shootProjetil(player, "rex:light_shot", 2, 2)
    };
   if (item.typeId == 'rex:magma_sword') {
        shootProjetil(player, "rex:fire_shot2", 2, 2)
    };
    if (item.typeId == 'rex:paladin_sword') {
        shootProjetil(player, "rex:light_shot", 2, 2)
    };
   if (item.typeId == 'rex:ice_staff') {
        shootProjetil(player, "rex:ice_shot", 2, 2)
    };
  if (item.typeId == 'rex:magic_scroll_dragon_power') {
        shootProjetil(player, "rex:evocation_shot", 2, 2)
    };
   if (item.typeId == 'rex:redstone_power') {
        shootProjetil(player, "rex:redstone_shot", 2, 2)
    };
   if (item.typeId == 'rex:tnt_staff') {
        shootProjetil(player, "rex:redstone_shot", 2, 2)
    };
   if (item.typeId == 'rex:magic_crossbow') {
        shootProjetil(player, "minecraft:arrow", 7, 2)
    };
   if (item.typeId == 'rex:training_magic_wand') {
        shootProjetil(player, "minecraft:snowball", 7, 2)
    };
   if (item.typeId == 'rex:magic_scroll_hit_kill') {
        shootProjetil(player, "minecraft:arrow", 999, 2)
    };
   if (item.typeId == 'rex:evoker_staff') {
        shootProjetil(player, "rex:evocation_shot", 2, 2)
   }
})

world.beforeEvents.playerBreakBlock.subscribe((event) => {
    const block = event.block;
    const item = event.itemStack;
    if (block.typeId.includes('ore') && item.typeId == 'rex:impulse_pickaxe') {
        block.dimension.runCommandAsync('effect @p haste 45 3 true')
   };
 
    if (block.typeId == 'minecraft:stone' && item.typeId == 'rex:magnetic_pickaxe') {
        block.dimension.runCommandAsync('execute at @p run function pic')
    };

        if (block.typeId == 'minecraft:stone' && item.typeId == 'rex:torch_pickaxe') {
        block.dimension.runCommandAsync('execute at @p positioned ~~~ run fill ~~~~~~ torch');
    }
})


world.afterEvents.playerPlaceBlock.subscribe((event) => {
    const block = event.block;
    const player = event.player;
    if (block.typeId == "rex:ancient_temple_key") {
block.dimension.runCommand('function ancient_key')

       };
    if (block.typeId == "rex:challenge_key_n013") {
block.dimension.runCommand('function key013')

       };
    if (block.typeId == "rex:challenge_key184") {
block.dimension.runCommand('function key184')

       };
   if (block.typeId == "rex:el_lorenzo_hideout_key") {
block.dimension.runCommand('function hideout_key')

       };
    if (block.typeId == "rex:drum_tomb_key") {
block.dimension.runCommand('function tumba')

       };
     if (block.typeId == "rex:challenge_key_n254") {
block.dimension.runCommand('function key254')

       };
   if (block.typeId == "rex:nameless_structure_key") {
block.dimension.runCommand('function estruturabu')
     
       };
     if (block.typeId == "rex:snow_mine_key") {
block.dimension.runCommand('function mina_nevada')

       };
    if (block.typeId == "rex:king_orc_key") {
block.dimension.runCommand('function sala')

       };
      if (block.typeId == "rex:challenge_key_graduation") {
block.dimension.runCommand('function formatura');

       }  
})

world.afterEvents.entityHurt.subscribe((ev)=>{
    if(ev.damageSource.damagingEntity.typeId === 'minecraft:player'){
        const victim = ev.hurtEntity
        const attacker = ev.damageSource.damagingEntity
        const item = attacker.getComponent('inventory').container.getItem(attacker.selectedSlotIndex).typeId
        
        if(item === 'minecraft:redstone'){
            victim.runCommand(`say @p §cI Love REDSTONE`)
           
          }
        }
})

world.afterEvents.entityHitEntity.subscribe((data) => {
    const { damagingEntity, hitEntity } = data;
    if (damagingEntity.typeId != 'minecraft:player') return;

    const equipment = damagingEntity.getComponent('equippable').getEquipment('Mainhand');
    if (equipment == undefined || equipment.typeId != 'rex:gold_long_sword') return;

    const inventory = damagingEntity.getComponent('inventory').container;
    let hasRequiredItem = false;

    for (let i = 0; i < inventory.size; i++) {
        const item = inventory.getItem(i);
        if (item && item.typeId === 'rex:blacksmith_amulet') {
            hasRequiredItem = true;
            break;
        }
    }

    
    if (!hasRequiredItem) return;

    hitEntity.runCommand('effect @p clear');
})

//escrever esse script QUASE me fez desistir de ser um criador de addons! isso ainda precisa de muitos ajustes... mas funciona! e por hora isso basta!
