// 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()){
    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()){
    if (event.item === 'minecraft:stone_pickaxe' && event.getBlock() === "minecraft:stone" && event.getPlayer().isCrouching() ) {
      event.block.set('cobblestone')
      event.server.runCommandSilent(`playsound minecraft:block.stone.break block @a ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()}`)
    }
  }
})


BlockEvents.rightClicked(event => {

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

    if (event.item === Item.of('minecraft:potion', '{Potion:"minecraft:water"}').strongNBT() && event.getBlock() === "opolisutilities:drying_table" && event.getPlayer().isCrouching()) {
      event.server.runCommandSilent(`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")
    }
    
  }
})


BlockEvents.rightClicked(event => {

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

    if (event.item === 'stoneopolis:cluster_grower' && event.getBlock() === 'geode_plus:budding_lapis' && event.getBlock().up === 'minecraft:air') {
      event.server.runCommandSilent(`setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:lapis_cluster`)
      event.server.runCommandSilent(`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(`setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:redstone_crystal`)
      event.server.runCommandSilent(`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(`setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:nether_quartz_crystal`)
      event.server.runCommandSilent(`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(`setblock ${event.getBlock().getX()} ${event.getBlock().getY() + 1} ${event.getBlock().getZ()} geode_plus:wrappist_cluster`)
      event.server.runCommandSilent(`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") {
    event.server.runCommandSilent(`setblock ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()} minecraft:grass_block`)
    event.server.runCommandSilent(`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") {
    event.server.runCommandSilent(`summon minecraft:item ${event.getBlock().getX()} ${event.getBlock().getY()+0.75} ${event.getBlock().getZ()} {Item:{id:"minecraft:dragon_egg",Count:1}}`)
    event.server.runCommandSilent(`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") {
    event.server.runCommandSilent(`summon minecraft:item ${event.getBlock().getX()} ${event.getBlock().getY()+0.75} ${event.getBlock().getZ()} {Item:{id:"minecraft:dragon_egg",Count:1}}`)
    event.server.runCommandSilent(`setblock ${event.getBlock().getX()} ${event.getBlock().getY()} ${event.getBlock().getZ()} minecraft:air`)
  }
  event.cancel()

})

