// Priority: 3
/**

  █▀▄▀█ █▀█ █▄░█ █░█ █▀▄▀█ █▀▀ █▄░█ ▀█▀ ▄▀█ █░░   █▀▀ ▀▄▀ █▀█ █▀▀ █▀█ █ █▀▀ █▄░█ █▀▀ █▀▀
  █░▀░█ █▄█ █░▀█ █▄█ █░▀░█ ██▄ █░▀█ ░█░ █▀█ █▄▄   ██▄ █░█ █▀▀ ██▄ █▀▄ █ ██▄ █░▀█ █▄▄ ██▄

  ☻/  * @file Starting Kit script for give Player starting items on first login
  /▌  * @copyright KnightDexx's Modded Minecraft World & ModernGamingWorld
  /▌  * Modified by @Cyn-SolveroftheAbsoluteGremlins on Discord
  /\
       * FIXED: stage is now only marked AFTER the kit is successfully given,
       *        player is re-fetched by UUID inside scheduled callbacks,
       *        and missing modded items are skipped instead of failing silently.
*/
PlayerEvents.loggedIn(event => {
  const server = event.server
  const uuid = event.player.uuid.toString()

  // Bail early if this player already received the kit
  if (event.player.stages.has('starting_items')) return

  // Wait 20 ticks (1 second) so the inventory is fully initialized/synced.
  // 1-2 ticks can be too short on a loaded server.
  server.scheduleInTicks(20, () => {
    // Re-fetch the player — never trust a captured reference across ticks.
    const player = server.getPlayer(uuid)
    if (!player) return // player disconnected before kit was given; stage NOT set, they'll get it next login

    // Double-check in case of a duplicate login event / relog race
    if (player.stages.has('starting_items')) return

    // Helper: give item only if it actually resolved (skips missing mods/IDs)
    const tryGive = (id, count) => {
      const item = Item.of(id, count || 1)
      if (item.isEmpty()) {
        console.warn(`[StarterKit] Skipped missing item: ${id}`)
        return
      }
      player.give(item)
    }

    // === Equip armor & shield ===
    player.setHeadArmorItem(Item.of('minecraft:leather_helmet'))
    player.setChestArmorItem(Item.of('minecraft:leather_chestplate'))
    player.setLegsArmorItem(Item.of('minecraft:leather_leggings'))
    player.setFeetArmorItem(Item.of('minecraft:leather_boots'))
    player.setOffHandItem(Item.of('minecraft:shield'))

    // === Default starter items ===
    tryGive('minecraft:oak_sapling', 4)
    tryGive('minecraft:iron_sword')
    tryGive('minecraft:iron_pickaxe')
    tryGive('minecraft:iron_shovel')
    tryGive('minecraft:iron_axe')
    tryGive('constructionwand:iron_wand')
    tryGive('naturescompass:naturescompass')
    tryGive('prefab:item_house_improved')
    tryGive('prefab:item_farm_improved')
    tryGive('minecraft:cookie', 4)
    tryGive('comforts:sleeping_bag_light_blue')
    tryGive('minecraft:crafting_table')
    tryGive('minecraft:smithing_table')

    // === Optional backpack ===
    if (Platform.isLoaded('sophisticatedbackpacks')) {
      tryGive('sophisticatedbackpacks:backpack')
    }

    // Mark the stage ONLY after the kit was actually delivered.
    // If anything above failed or the player left, this never runs,
    // so they get another attempt on next login instead of being locked out.
    player.stages.add('starting_items')
    console.info(`[StarterKit] Gave starter kit to ${player.username}`)
  })
})
