ServerEvents.recipes(event => {

  let essences = [
    { tier: 'inferium' },
    { tier: 'prudentium' },
    { tier: 'tertium' },
    { tier: 'imperium' },
    { tier: 'supremium' },
    { tier: 'insanium', mod: 'mysticalagradditions' }
  ]

  let farmlandTiers = ['inferium', 'prudentium', 'tertium', 'imperium', 'supremium']

  // Remove old shaped recipes
  farmlandTiers.forEach(tier => {
    let mod = tier === 'insanium' ? 'mysticalagradditions' : 'mysticalagriculture'
    event.remove({ id: `${mod}:${tier}_farmland_till` })
    event.remove({ id: 'mysticalagradditions:insanium_farmland_till' })
    event.remove({ id: 'mysticalagradditions:insanium_farmland' })
    event.remove({ id: `${mod}:${tier}_farmland` })
    event.remove({ id: `matc:${tier}_essence` })
    event.remove({ id: 'matc:insanium_essence' })
  })

  // Essence Upgrades
  for (let i = 0; i < essences.length - 1; i++) {
    let inputEssence  = `mysticalagriculture:${essences[i].tier}_essence`
    let outputEssence = `${essences[i + 1].mod || 'mysticalagriculture'}:${essences[i + 1].tier}_essence`
    let inputCrystal  = `matc:${essences[i].tier}_crystal`

    // PORTED: createMechanicalCrafting → create.mechanical_crafting
    // PORTED: Y: ['item1', 'item2'] → Ingredient.of(...).or(...)
    event.recipes.create.mechanical_crafting(outputEssence, [
      'XZX',
      'ZYZ',
      'XZX'
    ], {
      Z: inputEssence,
      X: 'mysticalagriculture:prosperity_shard',
      Y: Ingredient.of('mysticalagriculture:master_infusion_crystal').or(inputCrystal)
    })
    .damageIngredient(4)
    .keepIngredient('mysticalagriculture:master_infusion_crystal')
    .keepIngredient(inputCrystal)
    .id(`kubejs:mysticalagriculture/${essences[i + 1].tier}_essence`)

    console.log(`Creating recipe for: kubejs:mysticalagriculture/${essences[i + 1].tier}_essence`)
  }

  // Farmland Crafting
  for (let i = 0; i < farmlandTiers.length; i++) {
    let tier          = farmlandTiers[i]
    let mod           = tier === 'insanium' ? 'mysticalagradditions' : 'mysticalagriculture'
    let inputEssence  = `${mod}:${tier}_essence`
    let outputFarmland = `${mod}:${tier}_farmland`
    let inputFarmland  = i === 0 ? 'minecraft:farmland' : `${mod}:${farmlandTiers[i - 1]}_farmland`

    // PORTED: createMechanicalCrafting → create.mechanical_crafting
    event.recipes.create.mechanical_crafting(outputFarmland, [
      'YXY',
      'ZWZ',
      'YXY'
    ], {
      X: inputEssence,
      Z: inputFarmland,
      Y: 'mysticalagriculture:prosperity_shard',
      W: 'mysticalagriculture:prosperity_block'
    }).id(`kubejs:mysticalagradditions/${tier}_farmland`)

    console.log(`Creating farmland recipe for: kubejs:mysticalagradditions/${tier}_farmland`)
  }

  // Insanium Farmland (explicit, prevent duplication)
  event.remove({ id: 'kubejs:mysticalagradditions/insanium_farmland' })

  // PORTED: createMechanicalCrafting → create.mechanical_crafting
  event.recipes.create.mechanical_crafting('mysticalagradditions:insanium_farmland', [
    'YXY',
    'ZWZ',
    'YXY'
  ], {
    X: 'mysticalagradditions:insanium_essence',
    Z: 'mysticalagriculture:supremium_farmland',
    Y: 'mysticalagriculture:prosperity_shard',
    W: 'mysticalagriculture:prosperity_block'
  }).id('kubejs:mysticalagradditions/insanium_farmland')

  console.log('Creating Insanium farmland recipe for: kubejs:mysticalagradditions/insanium_farmland')

})
