// priority: 0

settings.logAddedRecipes = true
settings.logRemovedRecipes = true
settings.logSkippedRecipes = false
settings.logErroringRecipes = true

console.info('Hello, World! (You will see this line every time server resources reload)')

// events.listen('server.load', function(event) {
//     event.server.schedule(55 * MINUTE, event.server, function(callback) {
//         callback.data.tell('Server backup in 5 minutes!')
//     })

//     event.server.schedule(60 * MINUTE, event.server, function(callback) {
//         callback.data.runCommand('/backup')
//     })
// })
events.listen('player.chat', function(event) {
        // Check if message equals creeper, ignoring case
        if (event.message.startsWith('!goaway')) {
            event.player.tell('Nanu? Wo ist die Armee hin?')
            event.server.scheduleInTicks(1, event.server, function(callback) {

                //callback.server.runCommandSilent(`execute as ${event.player.name} run summon minecraft:zombie ${event.player.x} ${event.player.y} ${event.player.z}`);
                callback.server.runCommandSilent('execute as ' + event.player.name + ' undead_army stop')
            })

            event.cancel()
        }
    })
    //event.player.tell(event.player + " left clicked on" + event.block + " using" + event.item);
    // event.player.tell(event.block.id)


onEvent("block.right_click", event => {
    if (event.block.id == "eidolon:soul_enchanter") {
        if (event.hasGameStage('soul_enchanter')) {
            //just let him use it i guess
        } else {
            if (event.item.id == 'create:shadow_steel') {
                event.addGameStage("soul_enchanter");
                if (!event.player.isCreativeMode()) {
                    event.item.setCount(event.item.getCount() - 1)
                }
                event.server.scheduleInTicks(1, event.server, function(callback) {
                    callback.server.runCommandSilent('particle minecraft:firework ' + event.block.x + " " + event.block.y + " " + event.block.z + " 1 1 2 1 1000")
                    callback.server.runCommandSilent('playsound minecraft:ui.toast.challenge_complete ambient ' + event.player.name)
                })
            } else {


                event.player.tell("the Enchanter seems to be missing a single piece of §eShadow Steel.")
                event.server.scheduleInTicks(1, event.server, function(callback) {
                    callback.server.runCommandSilent('particle minecraft:witch ' + event.block.x + " " + event.block.y + " " + event.block.z + " 1 1 2 1 1000")
                    callback.server.runCommandSilent('playsound minecraft:item.totem.use block @a ' + event.block.x + " " + event.block.y + " " + event.block.z)
                    callback.server.runCommandSilent('execute as ' + event.player.name + ' run effect give @p minecraft:instant_damage 1 1')
                })
                event.cancel()
            }
        }
    }
})

// onForgeEvent('net.minecraftforge.event.entity.player.PlayerOpenContainerEvent', event => {
//     //event.setCanceled(true)
//     event.player.tell("open up")
// })




onEvent('recipes', event => {
    // Change recipes here
    // removing the OP Items from Simple Networks
    event.remove({ id: 'storagenetwork:collector_remote' })
    event.remove({ id: 'storagenetwork:builder_remote' })
    event.remove({ id: 'storagenetwork:inventory_remote' })
    event.remove({ id: 'storagenetwork:crafting_remote' })
    event.remove({ id: 'storagenetwork:picker_remote' })


    event.shapeless("endrem:guardian_pearl", 'upgrade_aquatic:elder_eye')
    event.shapeless('upgrade_aquatic:elder_eye', "endrem:guardian_pearl")
        //eidolon nerf: 
        //event.remove({ output: 'eidolon:soul_enchanter' }) //doesnt work ffs
        // event.custom({
        //     "type": "eidolon:worktable",
        //     "pattern": [
        //         " b ",
        //         "gog",
        //         "ooo"
        //     ],
        //     "reagents": "didi",
        //     "key": {
        //         "b": {
        //             "item": "minecraft:book"
        //         },
        //         "g": {
        //             "tag": "forge:ingots/arcane_gold"
        //         },
        //         "o": {
        //             "item": "minecraft:obsidian"
        //         },
        //         "d": {
        //             "tag": "forge:gems/diamond"
        //         },
        //         "i": {
        //             "item": "eidolon:gold_inlay"
        //         }
        //     },
        //     "result": {
        //         "item": "eidolon:soul_shard"
        //     }
        // })

    //fixing the Planes
    event.remove({ mod: "simpleplanes" })

    //remove the weird quark smelting recipe
    event.remove({ id: "quark:tweaks/smelting/bone_meal_utility" })

    //large plane:
    // Item.of('simpleplanes:large_plane', {EntityTag:{max_health:10,material:"minecraft:white_terracotta",Used:1,health:10,max_speed:1.0,upgrades:{"simpleplanes:chest":{Size:27,Items:[]},"simpleplanes:shooter":{},"simpleplanes:floaty_bedding":{},"simpleplanes:booster":{fuel:-1},"simpleplanes:furnace_engine":{item:{Size:1,Items:[]},burnTimeTotal:0,burnTime:0}}}})

    //small plane:
    // Item.of('simpleplanes:plane', {EntityTag:{max_health:10,material:"minecraft:red_terracotta",Used:1,health:10,max_speed:1.0,upgrades:{"simpleplanes:shooter":{},"simpleplanes:booster":{fuel:-1},"simpleplanes:furnace_engine":{item:{Size:1,Items:[]},burnTimeTotal:0,burnTime:0}}}})

    //helicopter
    // Item.of('simpleplanes:helicopter', {EntityTag:{max_health:10,material:"minecraft:green_terracotta",Used:1,health:10,max_speed:1.0,upgrades:{"simpleplanes:chest":{Size:27,Items:[]},"simpleplanes:furnace_engine":{item:{Size:1,Items:[]},burnTimeTotal:0,burnTime:0}}}})


    event.recipes.create.mechanical_crafting(Item.of('simpleplanes:plane', { EntityTag: { max_health: 10, material: "minecraft:red_terracotta", Used: 1, health: 10, max_speed: 1.0, upgrades: { "simpleplanes:shooter": {}, "simpleplanes:booster": { fuel: -1 }, "simpleplanes:furnace_engine": { item: { Size: 1, Items: [] }, burnTimeTotal: 0, burnTime: 0 } } } }), [
        '  M   ',
        ' DM  M',
        'PYCEMM',
        ' DM   ',
        '  M   '
    ], {
        M: '#forge:sheetmetals/constantan',
        P: 'create:propeller',
        Y: 'immersiveengineering:dynamo',
        C: '#forge:sheetmetals/steel',
        E: 'create:furnace_engine',
        D: 'minecraft:dispenser'
    })

    event.recipes.create.mechanical_crafting(Item.of('simpleplanes:large_plane', { EntityTag: { max_health: 10, material: "minecraft:white_terracotta", Used: 1, health: 10, max_speed: 1.0, upgrades: { "simpleplanes:chest": { Size: 27, Items: [] }, "simpleplanes:shooter": {}, "simpleplanes:floaty_bedding": {}, "simpleplanes:booster": { fuel: -1 }, "simpleplanes:furnace_engine": { item: { Size: 1, Items: [] }, burnTimeTotal: 0, burnTime: 0 } } } }), [
        '   M   ',
        ' DFM  M',
        'PYCEMMM',
        ' DFMWWW',
        '  M    '
    ], {
        M: '#forge:sheetmetals/aluminum',
        P: 'create:propeller',
        Y: 'immersiveengineering:dynamo',
        C: '#forge:sheetmetals/steel',
        E: 'create:furnace_engine',
        D: 'minecraft:dispenser',
        F: 'create:encased_fan',
        W: '#forge:wool'
    })

    event.recipes.create.mechanical_crafting(Item.of('simpleplanes:helicopter', { EntityTag: { max_health: 10, material: "minecraft:green_terracotta", Used: 1, health: 10, max_speed: 1.0, upgrades: { "simpleplanes:chest": { Size: 27, Items: [] }, "simpleplanes:furnace_engine": { item: { Size: 1, Items: [] }, burnTimeTotal: 0, burnTime: 0 } } } }), [
        ' P   ',
        ' MM P',
        'MSEMM',
        'MMM  '
    ], {
        M: '#forge:sheetmetals/steel',
        P: 'create:propeller',
        E: 'create:furnace_engine',
        S: '#forge:chests'
    })

    //better Eidolon Pewter blend
    event.remove({ id: "eidolon:pewter_blend" })
    event.shapeless("2x eidolon:pewter_blend", ['create:crushed_iron_ore', 'create:crushed_lead_ore'])


    //fixing the copper duplicates
    //unify the ingots
    //so much easier now that i read the docs
    //lmao there are no docs


    event.replaceInput({}, '#forge:ingots/copper', 'create:copper_ingot')
    event.replaceOutput({}, '#forge:ingots/copper', 'create:copper_ingot')

    event.replaceInput({}, '#forge:ingots/lead', 'eidolon:lead_ingot')
    event.replaceOutput({}, '#forge:ingots/lead', 'eidolon:lead_ingot')

    //all dusts are now crushed ore and vice versa

    for (i = 0; i < ores.length; i++) {

        event.replaceInput({}, '#forge:dusts/' + ores[i], 'create:crushed_' + ores[i] + '_ore')
        event.replaceOutput({}, '#forge:dusts/' + ores[i], 'create:crushed_' + ores[i] + '_ore')

    }
    //brass needs special treatment
    event.replaceInput({}, '#forge:dusts/brass', 'create:crushed_brass')
    event.replaceOutput({}, '#forge:dusts/brass', 'create:crushed_brass')

    event.replaceInput({}, 'immersiveengineering:plate_copper', 'create:copper_sheet')
    event.replaceOutput({}, 'immersiveengineering:plate_copper', 'create:copper_sheet')

    event.replaceInput({}, 'immersiveengineering:plate_iron', 'create:iron_sheet')
    event.replaceOutput({}, 'immersiveengineering:plate_iron', 'create:iron_sheet')

    event.replaceInput({}, 'immersiveengineering:plate_gold', 'create:golden_sheet')
    event.replaceOutput({}, 'immersiveengineering:plate_gold', 'create:golden_sheet')

    event.remove({ output: "theabyss:auto_smelt_pick_axe" })

    //createAdditions 
    const removeInout = (rem) => {
        event.remove({ input: rem })
        event.remove({ output: rem })
    }

    event.replaceInput({}, 'createaddition:copper_spool', 'immersiveengineering:wirecoil_copper')
    event.replaceInput({}, 'createaddition:gold_spool', 'immersiveengineering:wirecoil_steel')

    removeInout('createaddition:spool')
    removeInout('createaddition:connector')
    removeInout('createaddition:heater')
    removeInout('createaddition:copper_spool')
    removeInout('createaddition:iron_spool')
    removeInout('createaddition:multimeter')
    removeInout('createaddition:gold_spool')
        // removeInout('createaddition:copper_wire')
    removeInout('createaddition:iron_wire')
    removeInout('createaddition:gold_wire')

    event.remove({ id: "createaddition:rolling/copper_plate" })
    event.custom({
        "type": "createaddition:rolling",
        "input": {
            "tag": "forge:plates/copper"
        },
        "result": {
            "item": "immersiveengineering:wire_copper"
        }
    })

    event.replaceInput({}, '#forge:wires/copper', 'immersiveengineering:wire_copper')
    event.replaceOutput({}, '#forge:wires/copper', 'immersiveengineering:wire_copper')
        //event.replaceOutput({}, 'createaddition:copper_wire', 'immersiveengineering:wire_copper')


    event.replaceInput({}, 'createaddition:iron_rod', 'immersiveengineering:stick_iron')
    event.replaceOutput({}, 'createaddition:iron_rod', 'immersiveengineering:stick_iron')

    //pulling machine:
    event.shaped('createaddition:rolling_mill', [
        'ISI',
        'ASA',
        'ACA'
    ], {
        A: 'create:andesite_alloy',
        I: "minecraft:iron_ingot",
        C: 'create:andesite_casing',
        S: "create:shaft"
    })


    //fixing copper Blocks    
    event.remove({ output: "create:copper_block" })
    event.shaped("create:copper_block", [
        'IPI',
        'PPP',
        'IPI'
    ], {
        P: 'create:copper_sheet',
        I: "create:copper_ingot"
    })

    //reaadd furnace heater since it got removed by createadditions (thank you for that)
    event.shaped('immersiveengineering:furnace_heater', [
        'IPI',
        'PCP',
        'IRI'
    ], {
        C: 'immersiveengineering:coil_lv',
        P: 'create:copper_ingot',
        I: "create:iron_sheet",
        R: "minecraft:redstone"
    })


    //remove crafting for all the copper items in Ice and Fire
    event.remove({ output: "iceandfire:copper_block" })
    event.remove({ output: "iceandfire:copper_ingot" })
    event.remove({ output: "iceandfire:copper_ore" })
    event.remove({ output: "iceandfire:copper_nugget" })


    // event.custom({
    //     //custom elven Trade
    //     "type": "botania:elven_trade",
    //     "ingredients": [{
    //             "tag": "forge:ingots/manasteel"
    //         },
    //         {
    //             "tag": "forge:ingots/manasteel"
    //         }
    //     ],
    //     "output": [{
    //         "item": "minecraft:diamond"
    //     }]
    // })

    //custom runic altar
    //forbidden stella
    //console.log()



    event.remove({ output: 'forbidden_arcanus:eternal_stella' })
    event.custom({

        "type": "botania:runic_altar",
        "output": {
            "item": 'forbidden_arcanus:eternal_stella',
            "count": 1
        },
        "mana": 80000,
        "ingredients": [{
                "item": "druidcraft:black_soulfire"
            },
            {
                "item": 'forbidden_arcanus:xpetrified_orb'
            },
            {
                "item": "druidcraft:white_soulfire"
            },
            {
                "item": 'forbidden_arcanus:xpetrified_orb'
            },
            {
                "item": 'forbidden_arcanus:stellarite_piece'
            },
            {
                "item": 'forbidden_arcanus:xpetrified_orb'
            },
            {
                "item": "druidcraft:lime_soulfire"
            },
            {
                "item": 'forbidden_arcanus:xpetrified_orb'
            }

        ]
    })

    //soulfires in all colors:
    for (var i = 0; i < colors.length; i++) {
        event.custom({

            "type": "botania:runic_altar",
            "output": {
                "item": "druidcraft:" + colors[i] + "_soulfire",
                "count": 1
            },
            "mana": 100,
            "ingredients": [{
                    "item": 'eidolon:lesser_soul_gem'
                },
                {
                    "item": "minecraft:glowstone_dust"
                },
                {
                    "item": "minecraft:" + colors[i] + "_dye"
                },
                {
                    "item": "minecraft:glowstone_dust"
                }
            ]
        })

    }


    //custom pure Daisy
    // event.custom({
    //     "type": "botania:pure_daisy",
    //     "input": {
    //         "type": "tag",
    //         "tag": "forge:stone"
    //     },
    //     "output": {
    //         "name": "botania:livingrock"
    //     }
    // })


    // 'mutantbeasts:chemical_x'


    //remove crafting for waystones (for now)
    event.remove({ mod: "waystones" })
})

onEvent('item.tags', event => {
    // Get the #forge:cobblestone tag collection and add Diamond Ore to it
    // event.get('forge:cobblestone').add('minecraft:diamond_ore')
    for (i = 0; i < ores.length; i++) {
        event.get("forge:dusts").add('create:crushed_' + ores[i] + '_ore')
        event.get("forge:dusts/" + ores[i]).add('create:crushed_' + ores[i] + '_ore')
    }

    event.get("forge:dusts").add('create:crushed_brass')
    event.get("forge:dusts/brass").add('create:crushed_brass')

})

events.listen('player.logged_in', function(event) {
    // Check if player doesn't have "starting_items" gamestage yet
    if (!event.hasGameStage('starting_items')) {
        // Add the gamestage
        event.addGameStage('starting_items')
            // Give some items to player
        event.player.give(Item.of('antiqueatlas:antique_atlas', { atlasID: 1, RepairCost: 1, display: { Name: { "text": "Antique Atlas" } }, Enchantments: [{ lvl: 1, id: "ensorcellation:soulbound" }] }))
            //event.player.give(Item.of('xercapaint:item_palette', { r: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], b: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], g: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], basic: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], m: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], n: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }))
            //event.player.give('xercapaint:item_canvas_large')
            //event.player.give({ item: 'minecraft:apple', count: 30 })
    }
})

const ores = [
    "iron",
    "gold",
    "copper",
    "zinc",
    // "brass",
    "silver",
    "lead",
    "aluminum",
    "uranium",
    "nickel"
]

//to itterate through the soulfires from Druidcraft
const colors = [
    "white",
    "orange",
    "magenta",
    "yellow",
    "lime",
    "pink",
    "gray",
    "light_gray",
    "cyan",
    "purple",
    "blue",
    "brown",
    "green",
    "red",
    "black"
]