import { world, system } from '@minecraft/server'
//missing texture virus block spreading but not spreading on the catalyst
try {
    world.beforeEvents.worldInitialize.subscribe(event => {
        event.blockComponentRegistry.registerCustomComponent('omni_science:missing_texture_block', {
            onTick(block) {
                if (block?.block?.isValid()) {
                    const blocktick = system.runInterval(() => {
                        if (block?.block?.isValid()) {
                            if ((!block?.block?.below(1)?.isAir && !block?.block?.below(1)?.isLiquid && block?.block?.below(1)?.typeId !== 'omni_science:missing_texture_catalyst' && block?.block?.below(1)?.typeId !== 'omni_science:missing_texture_block')) {
                                block?.block?.below(1)?.setType('omni_science:missing_texture_block')
                            } else { system.clearRun(blocktick) }
                            if ((!block?.block?.above(1)?.isAir && !block?.block?.above(1)?.isLiquid && block?.block?.above(1)?.typeId !== 'omni_science:missing_texture_catalyst' && block?.block?.above(1)?.typeId !== 'omni_science:missing_texture_block')) {
                                block?.block?.above(1)?.setType('omni_science:missing_texture_block')
                            } else { system.clearRun(blocktick) }
                            if ((!block?.block?.north(1)?.isAir && !block?.block?.north(1)?.isLiquid && block?.block?.north(1)?.typeId !== 'omni_science:missing_texture_catalyst' && block?.block?.north(1)?.typeId !== 'omni_science:missing_texture_block')) {
                                block?.block?.north(1)?.setType('omni_science:missing_texture_block')
                            } else { system.clearRun(blocktick) }
                            if ((!block?.block?.south(1)?.isAir && !block?.block?.south(1)?.isLiquid && block?.block?.south(1)?.typeId !== 'omni_science:missing_texture_catalyst' && block?.block?.south(1)?.typeId !== 'omni_science:missing_texture_block')) {
                                block?.block?.south(1)?.setType('omni_science:missing_texture_block')
                            } else { system.clearRun(blocktick) }
                            if ((!block?.block?.west(1)?.isAir && !block?.block?.west(1)?.isLiquid && block?.block?.west(1)?.typeId !== 'omni_science:missing_texture_catalyst' && block?.block?.west(1)?.typeId !== 'omni_science:missing_texture_block')) {
                                block?.block?.west(1)?.setType('omni_science:missing_texture_block')
                            } else { system.clearRun(blocktick) }
                            if ((!block?.block?.east(1)?.isAir && !block?.block?.east(1)?.isLiquid && block?.block?.east(1)?.typeId !== 'omni_science:missing_texture_catalyst' && block?.block?.east(1)?.typeId !== 'omni_science:missing_texture_block')) {
                                block?.block?.east(1)?.setType('omni_science:missing_texture_block')
                            } else { system.clearRun(blocktick) }
                        } else {
                            system.clearRun(blocktick)
                        }
                    }, 120)
                }
            }
        })
    })
    //missing texture catalyst setting a block below itself
    world.beforeEvents.worldInitialize.subscribe(event => {
        event.blockComponentRegistry.registerCustomComponent('omni_science:missing_texture_catalyst', {
            onRandomTick({ block }) {
                if (!block?.below(1).isAir || !block?.below(1).isLiquid) {
                    block?.below(1).setType('omni_science:missing_texture_block')
                }
            }
        })
    })
    //anti missing texture virus block spreading on the virus blocks but not spreading on the catalyst
    world.beforeEvents.worldInitialize.subscribe(event => {
        event.blockComponentRegistry.registerCustomComponent('omni_science:anti_missing_texture_block', {
            onTick({ block }) {
                try {
                    const chance = Math.random()
                    const { x, y, z } = block?.location
                    if (chance <= 1 / 6 && (!block?.below()?.isAir && !block?.below()?.isLiquid && block?.below()?.typeId !== 'omni_science:anti_missing_texture_catalyst')) {
                        try {
                            block?.dimension.runCommand(`fill ${x} ${y - 1} ${z} ${x} ${y - 1} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_block`)
                            block?.dimension.runCommand(`fill ${x} ${y - 1} ${z} ${x} ${y - 1} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_catalyst`)
                            system.runTimeout(() => {
                                block?.setType('air')
                            }, 200)
                        } catch { }
                    }
                    else if (chance <= 2 / 6 && (!block?.above()?.isAir && !block?.above()?.isLiquid && block?.above()?.typeId !== 'omni_science:anti_missing_texture_catalyst')) {
                        try {
                            block?.dimension.runCommand(`fill ${x} ${y + 1} ${z} ${x} ${y + 1} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_block`)
                            block?.dimension.runCommand(`fill ${x} ${y + 1} ${z} ${x} ${y + 1} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_catalyst`)
                            system.runTimeout(() => {
                                block?.setType('air')
                            }, 200)
                        } catch { }
                    }
                    else if (chance <= 3 / 6 && (!block?.north()?.isAir && !block?.north()?.isLiquid && block?.north()?.typeId !== 'omni_science:anti_missing_texture_catalyst')) {
                        try {
                            block?.dimension.runCommand(`fill ${x} ${y} ${z - 1} ${x} ${y} ${z - 1} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_block`)
                            block?.dimension.runCommand(`fill ${x} ${y} ${z - 1} ${x} ${y} ${z - 1} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_catalyst`)
                            system.runTimeout(() => {
                                block?.setType('air')
                            }, 200)
                        } catch { }
                    }
                    else if (chance <= 4 / 6 && (!block?.south()?.isAir && !block?.south()?.isLiquid && block?.south()?.typeId !== 'omni_science:anti_missing_texture_catalyst')) {
                        try {
                            block?.dimension.runCommand(`fill ${x} ${y} ${z + 1} ${x} ${y} ${z + 1} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_block`)
                            block?.dimension.runCommand(`fill ${x} ${y} ${z + 1} ${x} ${y} ${z + 1} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_catalyst`)
                            system.runTimeout(() => {
                                block?.setType('air')
                            }, 200)
                        } catch { }
                    }
                    else if (chance <= 5 / 6 && (!block?.west()?.isAir && !block?.west()?.isLiquid && block?.west()?.typeId !== 'omni_science:anti_missing_texture_catalyst')) {
                        try {
                            block?.dimension.runCommand(`fill ${x - 1} ${y} ${z} ${x - 1} ${y} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_block`)
                            block?.dimension.runCommand(`fill ${x - 1} ${y} ${z} ${x - 1} ${y} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_catalyst`)
                            system.runTimeout(() => {
                                block?.setType('air')
                            }, 200)
                        } catch { }
                    }
                    else if (chance <= 6 / 6 && (!block?.east()?.isAir && !block?.east()?.isLiquid && block?.east()?.typeId !== 'omni_science:anti_missing_texture_catalyst')) {
                        try {
                            block?.dimension.runCommand(`fill ${x + 1} ${y} ${z} ${x + 1} ${y} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_block`)
                            block?.dimension.runCommand(`fill ${x + 1} ${y} ${z} ${x + 1} ${y} ${z} omni_science:anti_missing_texture_block [] replace omni_science:missing_texture_catalyst`)
                            system.runTimeout(() => {
                                block?.setType('air')
                            }, 200)
                        } catch { }
                    }
                } catch { }
            }
        })
    })
    //anti missing texture catalyst setting a block below itself
    world.beforeEvents.worldInitialize.subscribe(event => {
        event.blockComponentRegistry.registerCustomComponent('omni_science:anti_missing_texture_catalyst', {
            onTick({ block }) {
                if (!block?.below(1).isAir || !block?.below().isLiquid) {
                    block?.below(1).setType('omni_science:anti_missing_texture_block')
                }
            }
        })
    })
} catch { }