;(() => {

  // ==============================
  // 任务配置表
  // 只需要在这里添加任务即可
  // ==============================
  const TASKS = [
{ taskId: '415250E31460DFEC', biomes: ['regions_unexplored:flower_fields'] },
{ taskId: '5C51F296AF51ECA5', biomes: ['biomeswevegone:crag_gardens'] },
{ taskId: '6EF57AF884093747', biomes: ['biomeswevegone:enchanted_tangle'] },
{ taskId: '2A4E9237E9FE05C6', biomes: ['biomeswevegone:atacama_outback'] },
{ taskId: '6CA7FC392F499BCB', biomes: ['regions_unexplored:rainforest'] },
{ taskId: '7C2AA2744522B3D2', biomes: ['regions_unexplored:joshua_desert'] },
{ taskId: '3FED87449FE80391', biomes: ['biomeswevegone:fragment_jungle'] },
{ taskId: '4FE0909C95E881B7', biomes: ['regions_unexplored:highland_fields'] },
{ taskId: '52501F56437CBE28', biomes: ['regions_unexplored:magnolia_woodland'] },
{ taskId: '2635E91D62B14808', biomes: ['regions_unexplored:autumnal_maple_forest'] },
{ taskId: '7FFDD337187566DB', biomes: ['regions_unexplored:silver_birch_forest'] },
{ taskId: '10DB62EA02F2951A', biomes: ['biomeswevegone:canadian_shield'] },
{ taskId: '107F9E5539E82001', biomes: ['biomeswevegone:howling_peaks'] },
{ taskId: '666F59694BCABEA0', biomes: ['biomeswevegone:skyris_vale'] },
{ taskId: '5A0EB90C2267EBD1', biomes: ['biomeswevegone:frosted_coniferous_forest'] },
{ taskId: '63D411070E964ABF', biomes: ['biomeswevegone:frosted_taiga'] },
{ taskId: '73F2E1C61E0B8882', biomes: ['biomeswevegone:dacite_ridges'] },
{ taskId: '6790317C6D306544', biomes: ['biomeswevegone:cika_woods'] },
{ taskId: '3D678983E6731722', biomes: ['biomeswevegone:black_forest'] },
{ taskId: '7A92ED8330BE64E5', biomes: ['biomeswevegone:weeping_witch_forest'] },
{ taskId: '69D0267E3EF299E2', biomes: ['biomeswevegone:zelkova_forest'] },
{ taskId: '4966D2DF3430CC39', biomes: ['biomeswevegone:maple_taiga'] },
{ taskId: '7AA2A9032FB9BDF9', biomes: ['biomeswevegone:lush_stacks'] },
{ taskId: '7E2A59DD4FAB1FFF', biomes: ['biomeswevegone:dead_sea'] },
{ taskId: '3CC728CFDC9ADD8C', biomes: ['biomeswevegone:basalt_barrera'] },
{ taskId: '5ADFB06A89B7AEA7', biomes: ['biomeswevegone:firecracker_chaparral'] },
{ taskId: '00195B7FBF9DB31B', biomes: ['biomeswevegone:coconino_meadow'] },
{ taskId: '275E6DCF96F1C8A5', biomes: ['regions_unexplored:redwoods'] },
{ taskId: '35AE3E1DD83097F5', biomes: ['biomeswevegone:redwood_thicket'] },
{ taskId: '7F497B4579DB6E0A', biomes: ['biomeswevegone:white_mangrove_marshes'] },
{ taskId: '14D26714C6FB07B8', biomes: ['biomeswevegone:red_rock_valley'] },
{ taskId: '383D13E439A49A19', biomes: ['biomeswevegone:forgotten_forest'] },
{ taskId: '51DCA4B10A0D7419', biomes: ['biomeswevegone:temperate_grove'] },
{ taskId: '3DE512A1EACF79AF', biomes: ['biomeswevegone:ebony_woods'] },
{ taskId: '0A255E057DEA716E', biomes: ['biomeswevegone:rose_fields'] },
{ taskId: '787BA0E3D710A116', biomes: ['biomeswevegone:allium_shrubland'] },
{ taskId: '1E15F0C6607646F3', biomes: ['biomeswevegone:orchard'] },
{ taskId: '25B2404ED817E906', biomes: ['biomeswevegone:aspen_boreal'] },
{ taskId: '4C92F2B0C5F34834', biomes: ['biomeswevegone:amaranth_grassland'] },
{ taskId: '3BE3BFAB9A01174E', biomes: ['biomeswevegone:jacaranda_jungle'] },
{ taskId: '2F9CDB5E958F999A', biomes: ['biomeswevegone:bayou'] },
{ taskId: '1322A0B18BC3A7C0', biomes: ['biomeswevegone:prairie'] },
{ taskId: '06C15E0CF79D8B48', biomes: ['biomeswevegone:tropical_rainforest'] },
{ taskId: '5721E1EF754F2C00', biomes: ['biomeswevegone:pumpkin_valley'] },
{ taskId: '335E480358DA4459', biomes: ['biomeswevegone:cypress_swamplands'] },
{ taskId: '04A12E0C99834344', biomes: ['biomeswevegone:baobab_savanna'] },
{ taskId: '181E2D215B1169BD', biomes: ['biomeswevegone:windswept_desert'] },
{ taskId: '5B5C71BEBCE9CB87', biomes: ['biomeswevegone:shattered_glacier'] },
{ taskId: '2C7EDFC617623D17', biomes: ['regions_unexplored:alpha_grove'] },
{ taskId: '7948BEF2E77B5539', biomes: ['regions_unexplored:pine_slopes'] },
{ taskId: '3C6119448867C5AF', biomes: ['regions_unexplored:barley_fields'] },
{ taskId: '2B340DED5A84F4C1', biomes: ['regions_unexplored:orchard'] },
{ taskId: '1F843C499FB9B2C8', biomes: ['regions_unexplored:poppy_fields'] },
{ taskId: '183FD17D5B0A02D8', biomes: ['regions_unexplored:mauve_hills'] },
{ taskId: '647177128945E059', biomes: ['regions_unexplored:baobab_savanna'] },
{ taskId: '71B08806154E6622', biomes: ['regions_unexplored:scorching_caves'] },
{ taskId: '60932B7C6140B023', biomes: ['regions_unexplored:towering_cliffs'] },
{ taskId: '495C4A387233B3BD', biomes: ['regions_unexplored:fungal_fen'] },
{ taskId: '0FCACDF2871AC3A9', biomes: ['regions_unexplored:rocky_meadow'] },
{ taskId: '540FEBADBDA48086', biomes: ['regions_unexplored:prismachasm'] },
{ taskId: '5BAC7957916967EC', biomes: ['regions_unexplored:boreal_taiga'] },
{ taskId: '7E0AAF14086ED2F6', biomes: ['regions_unexplored:redstone_caves'] },
{ taskId: '4BD45711DD4AE5BC', biomes: ['regions_unexplored:old_growth_bayou'] },
{ taskId: '00F5D88355297C05', biomes: ['regions_unexplored:blackwood_taiga'] },
{ taskId: '4F6A3C5B74EF80B2', biomes: ['regions_unexplored:clover_plains'] },
{ taskId: '3ECD7E16D006B903', biomes: ['biomeswevegone:crimson_tundra'] },
{ taskId: '0C40E8B5E63D3F8B', biomes: ['regions_unexplored:hyacinth_deeps'] },
{ taskId: '512E07D31A4FB488', biomes: ['regions_unexplored:rocky_reef'] },
{ taskId: '35B1C9CBDB8F8F59', biomes: ['regions_unexplored:bamboo_forest'] },
{ taskId: '2793A398BE7B1D94', biomes: ['regions_unexplored:frozen_tundra'] },
{ taskId: '53CD12B2AE682441', biomes: ['regions_unexplored:fen'] },
{ taskId: '71A1FDDEB35E2FCB', biomes: ['biomeswevegone:ironwood_gour'] },
{ taskId: '7FE5303149B2647F', biomes: ['biomeswevegone:sakura_grove'] },
{ taskId: '13D87032FFA77CD0', biomes: ['regions_unexplored:golden_boreal_taiga'] },
{ taskId: '52A3D18E7DED2365', biomes: ['regions_unexplored:grassy_beach'] },
{ taskId: '52894C2D2433C22D', biomes: ['biomeswevegone:rainbow_beach'] },
{ taskId: '45EB9731A5AB0CD2', biomes: ['biomeswevegone:mojave_desert'] },
{ taskId: '75835B4D2DAA6D63', biomes: ['regions_unexplored:ashen_woodland'] },
{ taskId: '2FC6DE6EC4CF8A04', biomes: ['regions_unexplored:chalk_cliffs'] },
{ taskId: '7551DBE40AE8D273', biomes: ['biomeswevegone:overgrowth_woodlands'] }
  ]

  // 预编译任务字段，避免在高频路径重复 map/string 拼接
  const COMPILED_TASKS = TASKS.map(cfg => {
    const tid = String(cfg.taskId)
    return {
      tid: tid,
      allowed: cfg.biomes.map(String),
      key: 'biome_state_' + tid,
    }
  })
  // =====================================================
  // 1. 注册 FTB 自定义任务
  // 逻辑：在指定群系内 = 完成；否则 = 未完成
  // 注意：这里只做“纯判定”，不做任何 reset 或副作用操作
  // =====================================================
  COMPILED_TASKS.forEach(cfg => {
    const tid = cfg.tid
    const allowed = cfg.allowed

    FTBQuestsEvents.customTask(tid, ev => {
      ev.maxProgress = 1 // 最大进度为 1（完成/未完成）

      ev.setCheck((task, player) => {
        // 每次检测时根据当前群系判断是否完成
        task.progress = isBiomeMatch(player, allowed) ? 1 : 0
      })
    })
  })

  // =====================================================
  // 2. 服务端 Tick 监听
  // 逻辑：如果玩家“从目标群系离开”，立即重置任务
  // 采用“边沿触发”：
  // 上一刻在群系 && 当前不在群系 → reset
  // =====================================================
  PlayerEvents.tick(event => {
    const player = event.player

    // 仅在服务端执行（避免客户端重复触发）
    if (player.level.isClientSide()) return

    COMPILED_TASKS.forEach(cfg => {
      const tid = cfg.tid
      const allowed = cfg.allowed

      // 每个任务独立存储自己的状态
      const key = cfg.key

      // 当前是否在匹配群系
      const nowIn = isBiomeMatch(player, allowed)

      // 上一 Tick 是否在匹配群系
      const lastIn = player.persistentData.getBoolean(key)

      // ===== 边沿检测 =====
      // 仅当“从在群系 → 不在群系”时才触发 reset
      if (lastIn && !nowIn) {
        player.runCommandSilent('ftbquests change_progress @s reset ' + tid)
      }

      // 更新当前状态，供下一 Tick 使用
      player.persistentData.putBoolean(key, nowIn)
    })
  })

  // =====================================================
  // 群系匹配判断函数
  // 返回 true = 当前所在群系在允许列表中
  // =====================================================
  function isBiomeMatch(player, allowedList) {
    const lv = player.level

    // 获取当前玩家所在群系的完整 ID（namespace:path）
    const id = lv.getBiome(player.blockPosition())
                 .unwrapKey()
                 .get()
                 .location()
                 .toString()

    return allowedList.includes(id)
  }

})()

