var velos = ['arrzenhanced:velocity_injector', 'arrzenhanced:velocity_serum']
ItemEvents.rightClicked(velos, (event) => {
    let player = event.player
    if (event.getItem().getNbt() == undefined) {
        let pow = Math.floor(Math.random() * 10)
        event.getItem().setNbt({level:pow})
    }
})

var velLevels = [1, 2, 3, 4, 5, 6, 7, 8, 9]

ServerEvents.recipes(event => {
    event.shaped(
        Item.of(`arrzenhanced:velocity_injector`, 1, {level: 0}),
        [
            " A ",
            " B ",
            " C "
        ],
        {
            A: 'minecraft:piston',
            B: 'arrzenhanced:velocity_serum',
            C: 'minecraft:iron_nugget'
        }
    ).modifyResult((grid, result) => {
        var serum = grid.get(4); // fallback
        for (var z = 0; z < 9; z++) {
            serum = grid.get(z);
            // console.log(serum)
            if (serum?.nbt?.level != undefined) { break; }
        }
        return xferNBT(serum, result)
    })
    event.shaped(
        Item.of(`arrzenhanced:velocity_serum`, 1, {level: 0}),
        [
            "AB ",
            "CD ",
            "   "
        ],
        {
            A: Item.of('minecraft:potion', '{Potion:"minecraft:long_water_breathing"}').weakNBT(),
            B: Item.of('minecraft:potion', '{Potion:"minecraft:strong_regeneration"}').weakNBT(),
            C: Item.of('minecraft:potion', '{Potion:"minecraft:strong_swiftness"}').weakNBT(),
            D: Item.of('minecraft:potion', '{Potion:"minecraft:long_fire_resistance"}').weakNBT()
        }
    )
    velLevels.forEach((velocityLevel) => {
        event.shaped(
            Item.of(`arrzenhanced:velocity_serum`, 1, {level: velocityLevel}),
            [
                "AAA",
                "ABA",
                "AAA"
            ],
            {
                A: "minecraft:redstone",
                B: Item.of(`arrzenhanced:velocity_serum`, 1, {level: velocityLevel-1}).weakNBT()
            }
        )
    })
});

var xferNBT = (item1, item2) => {
    let nbt1 = item1?.nbt || {};
    let nbt2 = item2?.nbt || {};

    let tmp = Object.assign({}, nbt2, nbt1)
    return Item.of(item2).withNBT(tmp);
}