/*
  farming for blockheads custom market additions
  authored by EnigmaQuip
  for use in ATM8

  List Gen
    /markethelper
    OP permission required
    only should need to be run on mod changes,
    generates a new marketitems.json file
*/

ServerEvents.commandRegistry(event => {
  const { commands: Commands, arguments: Arguments, builtinSuggestions: Suggestions } = event;
  event.register(
    Commands.literal("markethelper")
      .requires(source => source.getServer().isSingleplayer() || source.hasPermission(2))
      .executes((ctx) => Market(ctx.source))
  )
})

function Market(source) {
  let saplings = {}
  let seeds = {}
  let flowers = {}
  let taggedSeeds = Ingredient.of('#forge:seeds').stacks
  taggedSeeds.forEach(seed => {
    let mod = seed.idLocation.namespace
    if (seeds[mod] == null) {
      seeds[mod] = []
    }
    seeds[mod].push(seed.id)
  })
  let taggedSaplings = Ingredient.of('#minecraft:saplings').stacks
  taggedSaplings.forEach(sapling => {
    let mod = sapling.idLocation.namespace
    if (saplings[mod] == null) {
      saplings[mod] = []
    }
    saplings[mod].push(sapling.id)
  })
  let taggedFlowers = Ingredient.of('#minecraft:flowers').stacks
  taggedFlowers.forEach(flower => {
    let mod = flower.idLocation.namespace
    if (flowers[mod] == null) {
      flowers[mod] = []
    }
    flowers[mod].push(flower.id)
  })
  JsonIO.write('kubejs/server_scripts/mods/farmingforblockheads/marketitems.json', { saplings: saplings, seeds: seeds, flowers: flowers })
  return 1
}

const FFBAPI = Java.loadClass('net.blay09.mods.farmingforblockheads.api.FarmingForBlockheadsAPI')

// Datapack Gen
ServerEvents.highPriorityData(event => {
  event.addJson('kubejs:farmingforblockheads_compat/atm.json', {
    groupOverrides: {
      "Croptopia Seeds": {
        enabled: false
      }
    }
  })
})
