// priority: 0

// Visit the wiki for more info - https://kubejs.com/
ServerEvents.loaded(event => {

  if (event.server.persistentData.gameRules) return

  event.server.gameRules.set("doTraderSpawning", false)

  event.server.persistentData.gameRules = true
})


ItemEvents.rightClicked(event => {
  if(!event.getLevel().isClientSide()){

    let dimension = event.getLevel().getDimension().toString()

    if (event.item === 'minecraft:glass_bottle' && event.level.dimension === "javd:void") {
      event.player.give('stoneopolis:bottled_void')
      event.item.setCount(event.item.getCount() - 1)
    }
  }
})

BlockEvents.rightClicked(event => {

  if(!event.getLevel().isClientSide()){

    let dimension = event.getLevel().getDimension().toString()

    if (event.item === 'minecraft:stone_pickaxe' && event.getBlock() === "minecraft:stone" && event.getPlayer().isCrouching() ) {
      event.block.set('cobblestone')
      event.server.runCommandSilent(`execute in ${dimension} run playsound minecraft:block.stone.break block @a ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()}`)
    }
  }
})


BlockEvents.rightClicked(event => {

  if(!event.getLevel().isClientSide()){

    let dimension = event.getLevel().getDimension().toString()

    if (event.item === Item.of('minecraft:potion', '{Potion:"minecraft:water"}').strongNBT() && event.getBlock() === "opolisutilities:drying_table" && event.getPlayer().isCrouching()) {
      event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()} opolisutilities:drying_table[waterlogged=true]`)
      event.getItem().setCount(event.item.getCount() - 1)
      event.getPlayer().addItem("minecraft:glass_bottle")
      event.cancel()
    }
    
  }
})


BlockEvents.rightClicked(event => {

  if(!event.getLevel().isClientSide()){

    let dimension = event.getLevel().getDimension().toString()

    if (event.item === 'stoneopolis:cluster_grower' && event.getBlock() === 'geode_plus:budding_lapis' && event.getBlock().up === 'minecraft:air') {
      event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:lapis_cluster`)
      event.server.runCommandSilent(`execute in ${dimension} run particle dust 0.19 0 0.94 1 ${event.getBlock().getX()} ${event.getBlock().getY() + 1.2} ${event.getBlock().getZ()} 0.25 0.3 0.25 1 100 force`)
    }
    if (event.item === 'stoneopolis:cluster_grower' && event.getBlock() === 'geode_plus:budding_redstone' && event.getBlock().up === 'minecraft:air') {
      event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:redstone_crystal`)
      event.server.runCommandSilent(`execute in ${dimension} run particle dust 1 0 0 1 ${event.getBlock().getX()} ${event.getBlock().getY() + 1.2} ${event.getBlock().getZ()} 0.25 0.2 0.25 1 100 force`)
    }
    if (event.item === 'stoneopolis:cluster_grower' && event.getBlock() === 'geode_plus:budding_nether_quartz' && event.getBlock().up === 'minecraft:air') {
      event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:nether_quartz_crystal`)
      event.server.runCommandSilent(`execute in ${dimension} run particle dust 1 0.93 1 1 ${event.getBlock().getX()} ${event.getBlock().getY() + 1.2} ${event.getBlock().getZ()} 0.25 0.2 0.25 1 100 force`)
    }
    if (event.item === 'stoneopolis:cluster_grower' && event.getBlock() === 'geode_plus:budding_wrappist' && event.getBlock().up === 'minecraft:air') {
      event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:wrappist_cluster`)
      event.server.runCommandSilent(`execute in ${dimension} run particle dust 0.47 0.9 1 1 ${event.getBlock().getX()} ${event.getBlock().getY() + 1.2} ${event.getBlock().getZ()} 0.25 0.2 0.25 1 100 force`)
    }
    
  }
})


//Death Events

PlayerEvents.respawned(event => {
  const seconds = 30
  event.player.potionEffects.add("bloodmagic:soulfray", 20 * seconds)
})

//Banned Items After Death

ItemEvents.rightClicked(event => {

  if(!event.getLevel().isClientSide()){
    if (event.item.hasTag('stoneopolis:banned_after_death') && event.player.potionEffects.isActive("bloodmagic:soulfray")) {
      const remainingTime = event.player.potionEffects.getDuration("bloodmagic:soulfray") / 20
      event.player.tell(`No, No, No, we don't do this here. Soulfray is in effect! Please wait ${remainingTime} seconds`)
      event.cancel()
    }
  }
})

//Grass Seed

BlockEvents.rightClicked("minecraft:dirt", event => {
  if(event.item === "stoneopolis:grass_seed") {
    
    let dimension = event.getLevel().getDimension().toString()

    event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()} minecraft:grass_block`)
    event.server.runCommandSilent(`execute in ${dimension} run particle dust 0 0.65 0 1 ${event.getBlock().getX()} ${event.getBlock().getY() + 1.2} ${event.getBlock().getZ()} 0.25 0.2 0.25 1 100 force`)
    event.getItem().setCount(event.item.getCount() - 1)
  }
})


//Dragon Egg 

BlockEvents.leftClicked("minecraft:dragon_egg", event => {
  if(event.item === "stoneopolis:dragon_egg_collector") {

    let dimension = event.getLevel().getDimension().toString()

    event.server.runCommandSilent(`execute in ${dimension} run summon minecraft:item ${event.getBlock().getX()} ${event.getBlock().getY()+0.75} ${event.getBlock().getZ()} {Item:{id:"minecraft:dragon_egg",Count:1}}`)
    event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()} minecraft:air`)
  }
  event.cancel()

})

BlockEvents.rightClicked("minecraft:dragon_egg", event => {
  if(event.item === "stoneopolis:dragon_egg_collector") {

    let dimension = event.getLevel().getDimension().toString()

    event.server.runCommandSilent(`execute in ${dimension} run summon minecraft:item ${event.getBlock().getX()} ${event.getBlock().getY()+0.75} ${event.getBlock().getZ()} {Item:{id:"minecraft:dragon_egg",Count:1}}`)
    event.server.runCommandSilent(`execute in ${dimension} run setblock ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()} minecraft:air`)
    
  }
  event.cancel()

})


//Strainers Uncraft
ItemEvents.rightClicked(event => {
  if (event.item === 'strainers:improved_everything_upgrade' && event.player.crouching) {
    event.player.give('strainers:improved_mesh_upgrade')
    event.player.give('strainers:improved_input_upgrade')
    event.player.give('strainers:improved_output_upgrade')
    event.player.give('strainers:improved_duration_upgrade')
    event.item.setCount(event.item.getCount() - 1)
  }

  if (event.item === 'strainers:sturdy_everything_upgrade' && event.player.crouching) {
    event.player.give('strainers:sturdy_mesh_upgrade')
    event.player.give('strainers:sturdy_input_upgrade')
    event.player.give('strainers:sturdy_output_upgrade')
    event.player.give('strainers:sturdy_duration_upgrade')
    event.item.setCount(event.item.getCount() - 1)
  }

  if (event.item === 'strainers:everlasting_everything_upgrade' && event.player.crouching) {
    event.player.give('strainers:everlasting_mesh_upgrade')
    event.player.give('strainers:everlasting_input_upgrade')
    event.player.give('strainers:everlasting_output_upgrade')
    event.player.give('strainers:everlasting_duration_upgrade')
    event.item.setCount(event.item.getCount() - 1)
  }

  if (event.item === 'strainers:reinforced_everything_upgrade' && event.player.crouching) {
    event.player.give('strainers:reinforced_mesh_upgrade')
    event.player.give('strainers:reinforced_input_upgrade')
    event.player.give('strainers:reinforced_output_upgrade')
    event.player.give('strainers:reinforced_duration_upgrade')
    event.item.setCount(event.item.getCount() - 1)
  }
})

