// priority: 50

onEvent('block.break', event => {
    if (!event.player.isCreativeMode()){
        if (event.block.hasTag('caveopolis:level_1_blocks')){
            if (!event.player.getTags().contains('level_1_blocks')){
                event.cancel()
                event.player.tell(`§cThis block cannot be broken at this level follow the quests to level up!`)
            }
        }
    }
})

onEvent('block.break', event => {
    if (!event.player.isCreativeMode()){
        if (event.block.hasTag('caveopolis:level_2_blocks')){
            if (!event.player.getTags().contains('level_2_blocks')){
                event.cancel()
                event.player.tell(`§cThis block cannot be broken at this level follow the quests to level up!`)
            }
        }
    }
})

onEvent('block.break', event => {
    if (!event.player.isCreativeMode()){
        if (event.block.hasTag('caveopolis:level_3_blocks')){
            if (!event.player.getTags().contains('level_3_blocks')){
                event.cancel()
                event.player.tell(`§cThis block cannot be broken at this level follow the quests to level up!`)
            }
        }
    }
})

onEvent('block.break', event => {
    if (!event.player.isCreativeMode()){
        if (event.block.hasTag('caveopolis:level_4_blocks')){
            if (!event.player.getTags().contains('level_4_blocks')){
                event.cancel()
                event.player.tell(`§cThis block cannot be broken at this level follow the quests to level up!`)
            }
        }
    }
})

onEvent('block.break', event => {
    if (!event.player.isCreativeMode()){
        if (event.block.hasTag('caveopolis:level_5_blocks')){
            if (!event.player.getTags().contains('level_5_blocks')){
                event.cancel()
                event.player.tell(`§cThis block cannot be broken at this level follow the quests to level up!`)
            }
        }
    }
})


onEvent('block.break', event => {
    if (!event.player.isCreativeMode()){
        if (event.block.hasTag('caveopolis:level_6_blocks')){
            if (!event.player.getTags().contains('level_6_blocks')){
                event.cancel()
                event.player.tell(`§cThis block cannot be broken at this level follow the quests to level up!`)
            }
        }
    }
})

//Predictions 

function predictions(type, xp){

onEvent('item.right_click', event => {
    if (event.item.id == type) 
    if (event.player.isCrouching()) {
            event.server.schedule(5, event.server, function (callback) {
                let command = `experience add ${event.player} ${xp}`
                callback.server.runCommandSilent(command);})
                if (!event.player.isCreativeMode()) {event.item.setCount(event.item.getCount() - 1)}
            }
        })
    }

  //  let command = `execute as ${event.player} experience add ${event.player} ${xp}`
    
    //if (!event.player.isCreativeMode()) {event.item.setCount(event.item.getCount() - 1)}
 

predictions('hostilenetworks:overworld_prediction', 15)
predictions('hostilenetworks:nether_prediction', 60)
predictions('hostilenetworks:end_prediction', 240)


//End Portal

onEvent('block.right_click', event => {
    if (event.hand == MAIN_HAND)
    if (event.item.hasTag('forge:ingots/enderium'))
    if (event.block.id == 'minecraft:end_portal_frame')
    if (event.player.isCrouching()){
      event.server.schedule(5, event.server, function (callback) {
      event.player.addItemCooldown(event.player.getHeldItem(MAIN_HAND), 10)   
      callback.server.runCommandSilent(`execute as ${event.player} run summon item ${event.block.x} ${event.block.y+0.5} ${event.block.z} {Item:{id:"minecraft:end_portal_frame",Count:1b}}`);                                       
      callback.server.runCommandSilent(`execute as ${event.player} run setblock ${event.block.x} ${event.block.y} ${event.block.z} minecraft:air`);;
  })}})

//Dragon Egg

onEvent('block.right_click', event => {

    if (event.block.id == 'minecraft:dragon_egg' && event.hand == MAIN_HAND && event.item.hasTag('forge:ingots/enderium') && event.player.isCrouching()){
      event.server.schedule(5, event.server, function (callback) {
      event.player.addItemCooldown(event.player.getHeldItem(MAIN_HAND), 10)   
      callback.server.runCommandSilent(`execute as ${event.player} in ${event.level.dimension} run summon item ${event.block.x} ${event.block.y+0.5} ${event.block.z} {Item:{id:"minecraft:dragon_egg",Count:1b}}`);                                       
      callback.server.runCommandSilent(`execute as ${event.player} in ${event.level.dimension} run setblock ${event.block.x} ${event.block.y} ${event.block.z} minecraft:air`);
    })}
    
    else if (event.block.id == 'minecraft:dragon_egg' && !event.item.hasTag('forge:ingots/enderium' && !event.player.isCrouching())) {
        event.cancel();
    }
})

//Caveopolis Menu

onEvent('item.right_click', event => {
    if (event.item.id == 'caveopolis:menu') {event.server.schedule(5, event.server, function (callback) {
    let command = `execute as ${event.player} run skyblock gui`
    callback.server.runCommandSilent(command);})
    //if (!event.player.isCreativeMode()) {event.item.setCount(event.item.getCount() - 1)}
    }  
})



//Blocks LootTable Changes (simple, others override vanilla drops)

onEvent('block.loot_tables', event => {
    event.addSimpleBlock('minecraft:calcite', 'minecraft:bone_meal')
})

//Enable Caveopolis Mob Spawning

onEvent('player.logged_in', event =>{
    event.server.schedule(5, event.server, function (callback) {     
    callback.server.runCommandSilent(`execute as ${event.player} run gamerule doTraderSpawning false`);
    callback.server.runCommandSilent(`execute as ${event.player} run gamerule doMobSpawning true`);
    console.log('Applying caveopolis additional spawning')
})})


