const $KineticBlock = Java.loadClass(
    "com.simibubi.create.content.kinetics.base.KineticBlockEntity"
);


const maxCount = 2
// BlockEvents.rightClicked(/create:/, (event) => {
BlockEvents.placed(/create:/, (event) => {
    const { player, hand, item, block, server, level } = event;

    // Potential Checks
    // if (hand != "MAIN_HAND") return;
    // if (!player) return;
    // if (!player.isPlayer()) return;
    // if (player.isFake()) return;

    server.scheduleInTicks(1, () => {

        // Get the Kinetic Block
        let kinBlock
        try {
            kinBlock = $KineticBlock(level.getBlockEntity(block.pos))
        } catch (error) {
            return
        }
        //let kinBlock = $KineticBlock(level.getBlockEntity(block.pos))
        // Get the Network
        let network = kinBlock.getOrCreateNetwork();
        let members = network.members;
        let count = 0

        members.forEach((member) => {
            if (member.getClass().toString().includes("waterwheel")) {
                count++
                if(count >= maxCount) {
                    return
                }
            }
        })

        if(count > maxCount) {
            player.tell(`There is a limit of ${maxCount} Water Wheels per Create system.`)
            player.tell(`Consider using Diesel Generators for higher Rotational Power demands.`)
            block.set("minecraft:air")
            block.popItem(block.id);
        }
        // player.tell(`There are ${count} Water Wheels in this network.`)
    })
});