onEvent('server.datapack.high_priority', (event) => {

    const pools = [
        {
            rolls: 1,
            entries: [
                {item: 'eidolon:pewter_ingot', weight: 20, count: [2,4]},
                {item: 'emendatusenigmatica:aluminum_ingot', weight: 20, count: [2,4]},
                {item: 'emendatusenigmatica:cast_iron_ingot', weight: 20, count: [2,4]},
                {item: 'emendatusenigmatica:invar_ingot', weight: 20, count: [2,4]}
            ]
        },
        {
            rolls: 1,
            entries: [
                {item: 'bloodmagic:weak_tau', count: [1,2]}
            ]
        },
        {
            rolls: [0,1],
            entries: [
                {item: 'bloodmagic:blankslate', weight: 40, count: [2,4]},
                {item: 'bloodmagic:reinforcedslate', weight: 20, count: [1,2]},
                {item: 'bloodmagic:reagentwater', weight: 5},
                {item: 'bloodmagic:reagentlava', weight: 5},
                {item: 'bloodmagic:reagentgrowth', weight: 5},
                {item: 'bloodmagic:reagentair', weight: 5},
                {item: 'bloodmagic:reagentbinding', weight: 5},
                {item: 'bloodmagic:melee_anointment', weight: 5},
                {item: 'bloodmagic:silk_touch_anointment', weight: 5},
                {item: 'bloodmagic:fortune_anointment', weight: 5},
                {item: 'bloodmagic:holy_water_anointment', weight: 5},
                {item: 'bloodmagic:hidden_knowledge_anointment', weight: 5},
                {item: 'bloodmagic:quick_draw_anointment', weight: 5},
                {item: 'bloodmagic:looting_anointment', weight: 5},
                {item: 'bloodmagic:bow_power_anointment', weight: 5},
                {item: 'bloodmagic:smelting_anointment', weight: 5},
                {item: 'bloodmagic:bow_velocity_anointment', weight: 5},
                {item: 'bloodmagic:plantoil', weight: 5},
                {item: 'bloodmagic:tauoil', weight: 5},
                {item: 'bloodmagic:throwing_dagger_syringe', weight: 5},
                {item: 'bloodmagic:throwing_dagger', weight: 5},
                {item: 'bloodmagic:itemroutingnode', weight: 5},
                {item: 'bloodmagic:shaped_charge', weight: 5},
                {item: 'bloodmagic:deforester_charge', weight: 5},
                {item: 'bloodmagic:veinmine_charge', weight: 5},
                {item: 'bloodmagic:fungal_charge', weight: 5},
            ]
        },
        {
            rolls: [0,1],
            entries: [
                {item: 'bloodmagic:basemonstersoul', nbt: '{souls:4.8d}', weight: 10},
                {item: 'bloodmagic:basemonstersoul', nbt: '{souls:9.4d}', weight: 10},
                {type: 'minecraft:empty', weight: 20}
            ]
        },
        {
            rolls: [0,1],
            entries: [
                {item: 'eidolon:soul_shard', weight: 20, count: [2,4]},
                {item: 'eidolon:gold_inlay', weight: 20, count: [1,2]},
                {item: 'eidolon:ender_calx', weight: 20, count: [1,2]},
                {item: 'naturesaura:aura_bottle', nbt: '{stored_type:"naturesaura:nether"}', weight: 20, count: [1,2]},
                {item: 'integrateddynamics:crystalized_menril_chunk', weight: 10, count: [2,4]},
                {type: 'minecraft:empty', weight: 10}
            ]
        }
    ];

    let loot_table = {
        type: 'minecraft:chest',
        pools: []
    };

    pools.forEach(pool => {
        let poolJson = {};
        poolJson.rolls = typeof pool.rolls === 'number'? pool.rolls : {min:pool.rolls[0],max:pool.rolls[1],type:'minecraft:uniform'};
        let entriesJson = [];
        pool.entries.forEach(entry => {
            let entryJson = {};
            entryJson.type = entry.type? entry.type : 'minecraft:item';
            entryJson.weight = entry.weight;
            entryJson.functions = [];
            if(entry.count){
                entryJson.functions.push({function:'minecraft:set_count',count:{min:entry.count[0],max:entry.count[1],type:'minecraft:uniform'}});
            }
            if(entry.enchant){
                entryJson.functions.push({function:'minecraft:enchant_with_levels',levels:{min:entry.enchant[0],max:entry.enchant[1]}});
            }
            if(entry.nbt){
                entryJson.functions.push({function:'minecraft:set_nbt',tag:entry.nbt});
            }
            if(entry.map){
                entryJson.functions.push({function:'minecraft:exploration_map',destination:entry.map,decoration:'red_x',zoom:1,skip_existing_chunks:false});
            }
            entryJson.name = entry.item;
            entriesJson.push(entryJson);
        });
        poolJson.entries = entriesJson;
        loot_table.pools.push(poolJson);
    });

    event.addJson('kubejs:loot_tables/chests/bloodmagic/dungeon_common.json', loot_table);
});