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

system.runInterval(() => {
    world.getAllPlayers().forEach((player) => {
        const equipmentInventory = player.getComponent( "minecraft:equippable" );
        if (
            equipmentInventory.getEquipment( EquipmentSlot.Mainhand )?.typeId == "better_on_bedrock:void_totem"
            || equipmentInventory.getEquipment( EquipmentSlot.Offhand )?.typeId == "better_on_bedrock:void_totem"
        ) {
        if(player.location.y <= 4){
            player.applyKnockback(0, 0, 0, 7.3)
            player.addEffect("slow_falling", 1000)
            player.addEffect("nausea", 180)
            player.addEffect("blindness", 80)
            player.runCommandAsync('playsound random.totem @s ~ ~ ~ 0.1')
            player.runCommandAsync('particle minecraft:totem_particle')
            player.dimension.runCommand("clear @p better_on_bedrock:void_totem 0 1")
            if(player.getComponent('health').currentValue >=4) {
                player.applyDamage(19)
            }
         }

} 
const test = player.dimension.getBlockFromRay(player.location, new Vector(0, -1, 0))
const test1 = player.dimension.getBlockFromRay(new Vector(player.location.x-2, player.location.y, player.location.z-2), new Vector(0, -1, 0))
const test2 = player.dimension.getBlockFromRay(new Vector(player.location.x+2, player.location.y, player.location.z+2), new Vector(0, -1, 0))


if( (test == undefined || test1 == undefined || test2 == undefined) && equipmentInventory.getEquipment( EquipmentSlot.Feet )?.typeId === "better_on_bedrock:voiding_boots") {
    player.runCommandAsync('effect @p slowness 3 1 true')
    player.runCommand(`fill ${player.location.x-1} ${player.location.y-1} ${player.location.z-1} ${player.location.x+1} ${player.location.y-1} ${player.location.z+1} better_on_bedrock:void_block [] replace air`)
 }
})}, 1)

system.runInterval(() => {
    world.getAllPlayers().forEach((player) => {

        const equipmentInventory = player.getComponent( "minecraft:equippable" );
        const item = equipmentInventory.getEquipment(EquipmentSlot.Feet );
		if (item === undefined) return;
		
        const durability = item.getComponent("durability");
        const test = player.dimension.getBlockFromRay(new Vector(player.location.x, player.location.y-2, player.location.z), new Vector(0, -1, 0))
        const test2 = player.dimension.getBlockFromRay(new Vector(player.location.x, player.location.y, player.location.z), new Vector(0, -1, 0))
        durability.damage += 1;

        if(test == undefined && equipmentInventory.getEquipment( EquipmentSlot.Feet )?.typeId == "better_on_bedrock:voiding_boots"){
            equipmentInventory.setEquipment(EquipmentSlot.Feet, item)
        } if(durability.damage >= durability.maxDurability) {
            equipmentInventory.setEquipment(EquipmentSlot.Feet, undefined)  
        }

        if(player.hasTag('endFog')){
        if(test2.block.typeId == 'better_on_bedrock:shrub_nylium' && !player.hasTag('vacant')){
            player.runCommand("fog @p remove test")
            player.runCommand("fog @p push pog:vacant_dusk test")
            player.addTag('vacant')
            player.removeTag('void')
            player.removeTag('chorus')
            player.removeTag('abyssal')
            player.removeTag('shroom')
        } else
        if(test2.block.typeId == 'better_on_bedrock:void_grass' && !player.hasTag('void')){
            player.runCommand("fog @p remove test")
            player.runCommand("fog @p push pog:voiding_plains test")
            player.addTag('void')
            player.removeTag('chorus')
            player.removeTag('abyssal')
            player.removeTag('vacant')
            player.removeTag('shroom')
        } else
        if(test2.block.typeId == 'better_on_bedrock:shroom_nylium' && !player.hasTag('shroom')){
            player.runCommand("fog @p remove test")
            player.runCommand("fog @p push pog:fungal_grove test")
            player.addTag('shroom')
            player.removeTag('vacant')
            player.removeTag('chorus')
            player.removeTag('abyssal')
            player.removeTag('void')
        } else
        if(test2.block.typeId == 'better_on_bedrock:fall_nylium' && !player.hasTag('abyssal')){
            player.runCommand("fog @p remove test")
            player.runCommand("fog @p push pog:voided_abyssal test")
            player.addTag('abyssal')
            player.removeTag('vacant')
            player.removeTag('shroom')
            player.removeTag('chorus')
            player.removeTag('void')
        } else
        if(test2.block.typeId == 'better_on_bedrock:chorus_grass' && !player.hasTag('chorus')){
            player.runCommand("fog @p remove test")
            player.runCommand("fog @p push pog:chorus_forest test")
            player.addTag('chorus')
            player.removeTag('vacant')

            player.removeTag('shroom')
            player.removeTag('void')
        } else

        if(test2.block.typeId == 'minecraft:end_stone' || test2.block.typeId == undefined){
            player.runCommand("fog @p remove test")
            player.removeTag('abyssal')
            player.removeTag('vacant')
            player.removeTag('shroom')
            player.removeTag('void')
            player.removeTag('chorus')
        }}    })}, 30)

world.afterEvents.playerInteractWithBlock.subscribe(ev => {
    const block = ev.block
    const blockLoc = ev.block.location
    const item = ev.itemStack
    const permutations = ev.block.permutation;
    const states = permutations.getState("pog:ring_added");
    const aboves = ev.block.above();
    if(block.typeId == 'better_on_bedrock:end_podium' && item.typeId == 'better_on_bedrock:the_ardent_crystal') {
        block.setPermutation(permutations.withState("pog:ring_added", true));
        ev.player.runCommand(`particle test:dragon_death2 ${blockLoc.x}  ${blockLoc.y+5}  ${blockLoc.z}`)
        ev.player.runCommand(`clear @p better_on_bedrock:the_ardent_crystal 0 1`)
        system.runTimeout(() => {
            ev.player.runCommand(`summon better_on_bedrock:poggy ${blockLoc.x} ${blockLoc.y+5} ${blockLoc.z} ~ ~ minecraft:entity_spawned`)
        }, 80)
    }
    if(block.typeId == 'better_on_bedrock:end_podium_basic' && item.typeId == 'better_on_bedrock:ring_of_care' && block.permutation.getState('pog:ring_added') == 0) {
        block.setPermutation(permutations.withState("pog:ring_added", 1));
        ev.player.runCommand(`clear @p better_on_bedrock:ring_of_care 0 1`)
        block.dimension.runCommandAsync(`loot spawn ${block.location.x} ${block.location.y+1} ${block.location.z} loot "chests/end_city_treasure_once"`)
    }
    if(block.typeId == 'better_on_bedrock:end_podium_basic' && item.typeId == 'better_on_bedrock:ring_of_hope' && block.permutation.getState('pog:ring_added') == 0) {
        block.setPermutation(permutations.withState("pog:ring_added", 2));
        ev.player.runCommand(`clear @p better_on_bedrock:ring_of_hope 0 1`)
        block.dimension.runCommandAsync(`loot spawn ${block.location.x} ${block.location.y+1} ${block.location.z} loot "chest/lava_shrine_once"`)
    }
    if(block.typeId == 'better_on_bedrock:end_podium_basic' && item.typeId == 'better_on_bedrock:ring_of_hate' && block.permutation.getState('pog:ring_added') == 0) {
        block.setPermutation(permutations.withState("pog:ring_added", 3));
        ev.player.runCommand(`clear @p better_on_bedrock:ring_of_hate 0 1`)
        block.dimension.runCommandAsync(`loot spawn ${block.location.x} ${block.location.y+1} ${block.location.z} loot "chests/waystone_tower_once"`)
    }
})

world.afterEvents.itemCompleteUse.subscribe(ev => {
    const item = ev.itemStack
    const player = ev.source
    if(item.typeId =='better_on_bedrock:rage_potion'){
        console.warn('...')
        player.runCommandAsync("effect @p speed 30 1 false")
        player.runCommandAsync("effect @p strength 30 2 false")
        player.runCommandAsync("effect @p haste 30 3 false")
        player.runCommandAsync("effect @p absorption 30 3 false")
        player.runCommandAsync("effect @p regeneration 30 1 false")
        player.runCommandAsync("replaceitem entity @p slot.weapon.mainhand 0 glass_bottle")
    }
})