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

    const pools = [
        {
            rolls: [0,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: [2,4]}
            ]
        },
        {
            rolls: [1,2],
            entries: [
                {item: 'bloodmagic:blankslate', weight: 40, count: [3,6]},
                {item: 'bloodmagic:reinforcedslate', weight: 20, count: [2,4]},
                {item: 'bloodmagic:reagentwater', weight: 5, count: [1,3]},
                {item: 'bloodmagic:reagentlava', weight: 5, count: [1,3]},
                {item: 'bloodmagic:reagentgrowth', weight: 5, count: [1,3]},
                {item: 'bloodmagic:reagentair', weight: 5, count: [1,3]},
                {item: 'bloodmagic:reagentbinding', weight: 5, count: [1,3]},
                {item: 'bloodmagic:melee_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:silk_touch_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:fortune_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:holy_water_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:hidden_knowledge_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:quick_draw_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:looting_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:bow_power_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:smelting_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:bow_velocity_anointment', weight: 5, count: [1,3]},
                {item: 'bloodmagic:plantoil', weight: 5, count: [1,3]},
                {item: 'bloodmagic:tauoil', weight: 5, count: [1,3]},
                {item: 'bloodmagic:throwing_dagger_syringe', weight: 5, count: [1,3]},
                {item: 'bloodmagic:throwing_dagger', weight: 5, count: [1,3]},
                {item: 'bloodmagic:itemroutingnode', weight: 5, count: [1,2]},
                {item: 'bloodmagic:shaped_charge', weight: 5, count: [1,3]},
                {item: 'bloodmagic:deforester_charge', weight: 5, count: [1,3]},
                {item: 'bloodmagic:veinmine_charge', weight: 5, count: [1,3]},
                {item: 'bloodmagic:fungal_charge', weight: 5, count: [1,3]},
            ]
        },
        {
            rolls: [0,1],
            entries: [
                {item: 'bloodmagic:basemonstersoul', nbt: '{souls:4.8d}', weight: 10},
                {item: 'bloodmagic:basemonstersoul', nbt: '{souls:9.4d}', weight: 10},
                {item: 'bloodmagic:ingot_hellforged', weight: 5, count: [1,3]}
            ]
        },
        {
            rolls: [0,1],
            entries: [
                {item: 'eidolon:soul_shard', weight: 20, count: [3,8]},
                {item: 'eidolon:gold_inlay', weight: 20, count: [2,4]},
                {item: 'eidolon:ender_calx', weight: 20, count: [2,4]},
                {item: 'naturesaura:aura_bottle', nbt: '{stored_type:"naturesaura:nether"}', weight: 20, count: [2,4]},
                {item: 'create:rose_quartz', weight: 10, count: [2,4]},
                {item: 'integrateddynamics:crystalized_menril_chunk', weight: 10, count: [3,8]},
                {item: 'jaept:scarlet_rope', weight: 10, count: [2,4]},
                {item: 'jaept:dark_dough', weight: 10, count: [2,4]},
            ]
        }
    ];

    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);
});