ServerEvents.recipes(event => {

  // Empowerer: Empty Blaze Burner (center) + 4 Blaze Rods (stands) -> Blaze Burner
  event.recipes.actuallyadditions
    .empowering(
      Item.of("create:blaze_burner"),
      "create:empty_blaze_burner",
      [
        "minecraft:blaze_rod",
        "minecraft:blaze_rod",
        "minecraft:blaze_rod",
        "minecraft:blaze_rod"
      ]
    )
    .energy(2000)
    .time(100)
    .color(0xFFA500) // orange

  // Empowerer: Quartz (center) + 4 Blazing Crystals (stands) -> Blazing Teleporter
  event.recipes.actuallyadditions
    .empowering(
      Item.of("kubejs:blazing_teleporter"),
      "minecraft:quartz",
      [
        "powah:crystal_blazing",
        "powah:crystal_blazing",
        "powah:crystal_blazing",
        "powah:crystal_blazing"
      ]
    )
    .energy(2000)
    .time(100)
    .color(0xFFA500) // orange

})

/*
  Blazing Teleporter behavior:
  - Right click saves your current position + dimension
  - Teleports you to the Nether
  - Gives Slow Falling (10s) + Fire Resistance (60s)
  - After 20 seconds, teleports you back to where you used it
  - 10 seconds before return, shows an actionbar warning
*/

const TP_KEY = "blazingTp"
const RETURN_TICKS = 20 * 20      // 20 seconds
const WARNING_TICKS = 20 * 10     // 10 seconds

function dimId(level) {
  return level.dimension.location().toString() // e.g. "minecraft:overworld"
}

function sendActionbar(player, text) {
  // Title actionbar is "above your hotbar"
  player.server.runCommandSilent(
    `execute as ${player.username} run title @s actionbar {"text":"${text}","italic":false}`
  )
}

function returnPlayer(player) {
  const pd = player.persistentData
  const dim = pd.getString(`${TP_KEY}:returnDim`)
  if (!dim) return

  const x = pd.getDouble(`${TP_KEY}:returnX`)
  const y = pd.getDouble(`${TP_KEY}:returnY`)
  const z = pd.getDouble(`${TP_KEY}:returnZ`)

  // Clear stored return info
  pd.remove(`${TP_KEY}:returnDim`)
  pd.remove(`${TP_KEY}:returnX`)
  pd.remove(`${TP_KEY}:returnY`)
  pd.remove(`${TP_KEY}:returnZ`)

  // Teleport back as the player
  player.server.runCommandSilent(
    `execute as ${player.username} in ${dim} run tp @s ${x} ${y} ${z}`
  )
}

ItemEvents.rightClicked("kubejs:blazing_teleporter", event => {
  if (event.level.isClientSide()) return

  const player = event.player
  const pd = player.persistentData

  // Prevent re-use while a return is pending
  if (pd.getString(`${TP_KEY}:returnDim`)) return

  // Save return location
  pd.putString(`${TP_KEY}:returnDim`, dimId(event.level))
  pd.putDouble(`${TP_KEY}:returnX`, player.x)
  pd.putDouble(`${TP_KEY}:returnY`, player.y)
  pd.putDouble(`${TP_KEY}:returnZ`, player.z)

  // Effects (20 ticks = 1 second)
  player.potionEffects.add("minecraft:slow_falling", 20 * 10, 0, false, true)     // 10s
  player.potionEffects.add("minecraft:fire_resistance", 20 * 60, 0, false, true) // 60s

  // Teleport to Nether
  player.server.runCommandSilent(
    `execute as ${player.username} in minecraft:the_nether run tp @s 0 80 0`
  )

  // Actionbar warning 10 seconds before return
  event.server.scheduleInTicks(RETURN_TICKS - WARNING_TICKS, () => {
    const p = event.server.getPlayer(player.uuid)
    if (p) sendActionbar(p, "<rainb><wave><neon><glitch>GET READY FOR LIFTOFF!!")
  })

  // Return after 20 seconds
  event.server.scheduleInTicks(RETURN_TICKS, () => {
    const p = event.server.getPlayer(player.uuid)
    if (p) returnPlayer(p)
  })
})

