const $MinecraftServer = Java.loadClass('net.minecraft.server.MinecraftServer')
function doCommandForBlock(server, command) {
    if (!(server instanceof $MinecraftServer)) return
    return server.runCommandSilent(command)
}

ForgeEvents.onEvent('net.minecraftforge.event.level.BlockEvent$NeighborNotifyEvent', event => {
    let block = event.state.block
    if (block.id !== 'minecraft:water') return;
    for (let dir of event.notifiedSides) {
        const pos = event.pos.relative(dir);
        const target = event.level.getBlockState(pos);
        if (target.is('kubejs:carpet')) {
            event.level.setBlock(pos, Block.getBlock('kubejs:soggy_carpet').defaultBlockState(), 3);
            doCommandForBlock(event.level.server, `/execute positioned ${pos.x} ${pos.y} ${pos.z} run playsound minecraft:entity.generic.splash block @a ~ ~ ~ 0.5 2`)
            break;
        }
    }
});

ForgeEvents.onEvent('net.minecraftforge.event.level.BlockEvent$NeighborNotifyEvent', event => {
    let block = event.state.block
    if (block.id !== 'minecraft:water') return;
    for (let dir of event.notifiedSides) {
        const pos = event.pos.relative(dir);
        const target = event.level.getBlockState(pos);
        if (target.is('kubejs:carpet_stairs')) {
            event.level.setBlock(pos, Block.getBlock('kubejs:soggy_carpet_stairs').defaultBlockState(), 3);
            doCommandForBlock(event.level.server, `/execute positioned ${pos.x} ${pos.y} ${pos.z} run playsound minecraft:entity.generic.splash block @a ~ ~ ~ 0.5 2`)
            break;
        }
    }
});

ForgeEvents.onEvent('net.minecraftforge.event.level.BlockEvent$NeighborNotifyEvent', event => {
    let block = event.state.block
    if (block.id !== 'minecraft:water') return;
    for (let dir of event.notifiedSides) {
        const pos = event.pos.relative(dir);
        const target = event.level.getBlockState(pos);
        if (target.is('kubejs:carpet_slab')) {
            event.level.setBlock(pos, Block.getBlock('kubejs:soggy_carpet_slab').defaultBlockState(), 3);
            doCommandForBlock(event.level.server, `/execute positioned ${pos.x} ${pos.y} ${pos.z} run playsound minecraft:entity.generic.splash block @a ~ ~ ~ 0.5 2`)
            break;
        }
    }
});