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 (including all essences)
    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`;

        // Creating the mechanical crafting recipe for essence upgrade
        event.recipes.createMechanicalCrafting(outputEssence, [
            'XZX',
            'ZYZ',
            'XZX'
        ], {
            Z: inputEssence,
            X: 'mysticalagriculture:prosperity_shard',
            Y: ['mysticalagriculture:master_infusion_crystal', inputCrystal]
        })
        .damageIngredient(4)
        .keepIngredient('mysticalagriculture:master_infusion_crystal')  // Keep master crystal
        .keepIngredient(inputCrystal)  // Keep the tier-specific crystal
        .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`;

        // Creating the mechanical crafting recipe for farmland
        event.recipes.createMechanicalCrafting(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 Recipe (Explicitly handled to avoid duplication)
    event.remove({id: 'kubejs:mysticalagradditions/insanium_farmland'}); // Prevent duplicate from being generated

    // Ensure the Insanium Farmland recipe is created only once
    event.recipes.createMechanicalCrafting('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`);

});
