import { world, system, ItemStack } from '@minecraft/server'

const blockList = [
    'minecraft:grass_block',
    'minecraft:dirt',
    'minecraft:oak_log'
]

world.beforeEvents.worldInitialize.subscribe(event => {
    event.blockComponentRegistry.registerCustomComponent('omni_science:lucky_block', {
        onPlayerDestroy({ block, player, dimension }) {
            const { x, y, z } = block.location
            const chance = Math.random()
            if (player.getGameMode() !== 'creative') {
                //lucky block spawning 10 cats
                if (chance <= 1 / 43) {
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                    block.dimension.spawnEntity('minecraft:cat', block.center())
                }
                //lucky block spawning 10 skeletons
                else if (chance <= 2 / 43) {
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton', block.center())
                }
                //lucky block replacing random blocks around them with emerald blocks
                else if (chance <= 3 / 43) {
                    const startCoord = { x: block.location.x - 3, y: block.location.y - 3, z: block.location.z - 3 }
                    const endCoord = { x: block.location.x + 3, y: block.location.y + 3, z: block.location.z + 3 }
                    for (const blockType of blockList) {
                        const command = `fill ${startCoord.x} ${startCoord.y} ${startCoord.z} ${endCoord.x} ${endCoord.y} ${endCoord.z} minecraft:emerald_block [] replace ${blockType}`;
                        block.dimension.runCommand(command)
                    }
                }
                //lucky block droping a knockback stick
                else if (chance <= 4 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:knockback_stick', 1), block.center())
                    })
                }
                //lucky block spawning a giant
                else if (chance <= 5 / 43) {
                    block.dimension.spawnEntity('omni_science:giant_zombie', block.center())
                }
                //lucky block places bedrock
                else if (chance <= 6 / 43) {
                    block.setType('minecraft:bedrock')
                }
                //lucky block generates obsidian cage
                else if (chance <= 7 / 43) {
                    world.structureManager.place('obsidian_cage', block.dimension, { x: player.location.x - 1, y: player.location.y - 1, z: player.location.z - 1 })
                }
                //lucky block generates anvil cage
                else if (chance <= 8 / 43) {
                    world.structureManager.place('anvil_cage', block.dimension, { x: player.location.x - 1, y: player.location.y - 1, z: player.location.z - 1 })
                }
                //lucky block generates mini desert pyramid
                else if (chance <= 9 / 43) {
                    block.dimension.runCommand(`structure load mini_desert_temple ${x} ${y - 1} ${z}`)
                }
                //lucky block places a diamond block
                else if (chance <= 10 / 43) {
                    block.setType('diamond_block')
                    block.dimension.spawnEntity('lightning_bolt', block.location)
                }
                //lucky blocks creates a big void with lava on the bottom and broccoli stem platform
                else if (chance <= 11 / 43) {
                    block.dimension.runCommand(`fill ${x - 5} ${y - 20} ${z - 5} ${x + 5} ${y} ${z + 5} air`)
                    block.dimension.runCommand(`fill ${x - 5} ${y - 20} ${z - 5} ${x + 5} ${y - 24} ${z + 5} flowing_lava`)
                    block.dimension.runCommand(`fill ${x - 2} ${y - 5} ${z - 2} ${x + 2} ${y - 5} ${z + 2} omni_science:broccoli_stem`)
                }
                //lucky block summons all hostile nether creatures
                else if (chance <= 12 / 43) {
                    block.dimension.spawnEntity('minecraft:ghast', block.center())
                    block.dimension.spawnEntity('minecraft:blaze', block.center())
                    block.dimension.spawnEntity('minecraft:hoglin', block.center())
                    block.dimension.spawnEntity('minecraft:magma_cube', block.center())
                    block.dimension.spawnEntity('minecraft:piglin', block.center())
                    block.dimension.spawnEntity('minecraft:wither_skeleton', block.center())
                }
                //lucky block summons five charged creepers
                else if (chance <= 13 / 43) {
                    block.dimension.spawnEntity('minecraft:creeper', block.center())
                    block.dimension.spawnEntity('minecraft:creeper', block.center())
                    block.dimension.spawnEntity('minecraft:creeper', block.center())
                    block.dimension.spawnEntity('minecraft:creeper', block.center())
                    block.dimension.spawnEntity('minecraft:creeper', block.center())
                    block.dimension.spawnEntity('lightning_bolt', block.location)
                }
                //lucky block drops a Mecha Giga Steve spawn egg
                else if (chance <= 14 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:mecha_giga_steve_spawn_egg', 1), block.center())
                    })
                }
                //lucky block drops 3 diamonds and a poop
                else if (chance <= 15 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:diamond', 12), block.center())
                        block.dimension.spawnItem(new ItemStack('omni_science:poop', 1), block.center())
                    })
                }
                //lucky block drops giant pufferfish spawn egg
                else if (chance <= 16 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:giant_pufferfish_spawn_egg', 1), block.center())
                    })
                }
                //lucky block drops mooshroom spawn egg and 3 bowls
                else if (chance <= 17 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:mooshroom_spawn_egg', 1), block.center())
                        block.dimension.spawnItem(new ItemStack('minecraft:bowl', 3), block.center())
                    })
                }
                //lucky block drops 1 dirt
                else if (chance <= 18 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:dirt', 1), block.center())
                    })
                }
                //lucky block drops you in lava
                else if (chance <= 19 / 43) {
                    block.dimension.runCommand(`fill ${x - 5} ${y - 20} ${z - 5} ${x + 5} ${y} ${z + 5} air`)
                    block.dimension.runCommand(`fill ${x - 5} ${y - 20} ${z - 5} ${x + 5} ${y - 24} ${z + 5} flowing_lava`)
                }
                //lucky block replaces random blocks around them with end_portal
                else if (chance <= 20 / 43) {
                    const startCoord = { x: block.location.x - 3, y: block.location.y - 3, z: block.location.z - 3 }
                    const endCoord = { x: block.location.x + 3, y: block.location.y + 3, z: block.location.z + 3 }
                    for (const blockType of blockList) {
                        const command = `fill ${startCoord.x} ${startCoord.y} ${startCoord.z} ${endCoord.x} ${endCoord.y} ${endCoord.z} minecraft:end_portal [] replace ${blockType}`;
                        block.dimension.runCommand(command)
                    }
                }
                //lucky block summons zombie meteor
                else if (chance <= 21 / 43) {
                    block.dimension.spawnEntity('omni_science:zombie_meteor', { x: x, y: 256, z: z })
                }
                //lucky block launches player
                else if (chance <= 22 / 43) {
                    player.applyKnockback(0, 0, 0, 5)
                }
                //lucky block drops 2 sticks
                else if (chance <= 23 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:stick', 2), block.center())
                    })
                }
                //lucky block drops anvils from the sky
                else if (chance <= 24 / 43) {
                    block.dimension.runCommand(`fill ${x - 5} ${y + 38} ${z - 5} ${x + 5} ${y + 40} ${z + 5} anvil`)
                }
                //lucky block summons 5 piglin brutes
                else if (chance <= 25 / 43) {
                    block.dimension.spawnEntity('minecraft:piglin_brute', block.center())
                    block.dimension.spawnEntity('minecraft:piglin_brute', block.center())
                    block.dimension.spawnEntity('minecraft:piglin_brute', block.center())
                    block.dimension.spawnEntity('minecraft:piglin_brute', block.center())
                    block.dimension.spawnEntity('minecraft:piglin_brute', block.center())
                }
                //lucky block drops elytra
                else if (chance <= 26 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:elytra', 1), block.center())
                    })
                }
                //lucky block drops jukebox
                else if (chance <= 27 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:jukebox', 1), block.center())
                    })
                }
                //lucky block summons evoker
                else if (chance <= 28 / 43) {
                    block.dimension.spawnEntity('minecraft:evocation_illager', block.center())
                }
                //lucky block summons lava cube
                else if (chance <= 29 / 43) {
                    block.dimension.runCommand(`fill ${x - 5} ${y - 20} ${z - 5} ${x + 5} ${y + 20} ${z + 5} lava`)
                }
                //lucky block summons many passive mobs
                else if (chance <= 30 / 43) {
                    block.dimension.spawnEntity('minecraft:sheep', block.center())
                    block.dimension.spawnEntity('minecraft:horse', block.center())
                    block.dimension.spawnEntity('minecraft:cow', block.center())
                    block.dimension.spawnEntity('minecraft:pig', block.center())
                    block.dimension.spawnEntity('minecraft:skeleton_horse', block.center())
                    block.dimension.spawnEntity('minecraft:armadillo', block.center())
                    block.dimension.spawnEntity('minecraft:sniffer', block.center())
                    block.dimension.spawnEntity('minecraft:turtle', block.center())
                    block.dimension.spawnEntity('minecraft:squid', block.center())
                    block.dimension.spawnEntity('minecraft:axolotl', block.center())
                    block.dimension.spawnEntity('minecraft:bat', block.center())
                    block.dimension.spawnEntity('minecraft:allay', block.center())
                    block.dimension.spawnEntity('minecraft:parrot', block.center())
                }
                //lucky blocks make a big hollow obsidian box
                else if (chance <= 31 / 43) {
                    block.dimension.runCommand(`fill ${x - 5} ${y - 3} ${z - 5} ${x + 5} ${y + 3} ${z + 5} obsidian [] hollow`)
                }
                //lucky blocks make a big hollow obsidian box with lava
                else if (chance <= 32 / 43) {
                    block.dimension.runCommand(`fill ${x - 5} ${y - 3} ${z - 5} ${x + 5} ${y + 3} ${z + 5} obsidian [] hollow`)
                    block.dimension.runCommand(`fill ${x - 4} ${y + 2} ${z - 4} ${x + 4} ${y + 2} ${z + 4} lava`)
                }
                //lucky block drops an anti missing texture catalyst
                else if (chance <= 33 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:anti_missing_texture_catalyst', 1), block.center())
                    })
                }
                //lucky block replacing blocks around them with missing texture blocks
                else if (chance <= 34 / 43) {
                    const startCoord = { x: block.location.x - 3, y: block.location.y - 3, z: block.location.z - 3 }
                    const endCoord = { x: block.location.x + 3, y: block.location.y + 3, z: block.location.z + 3 }
                    for (const blockType of blockList) {
                        const command = `fill ${startCoord.x} ${startCoord.y} ${startCoord.z} ${endCoord.x} ${endCoord.y} ${endCoord.z} omni_science:missing_texture_block [] replace ${blockType}`;
                        block.dimension.runCommand(command)
                    }
                }
                //lucky block sets time to night and spawns 5 tnt phantoms
                else if (chance <= 35 / 43) {
                    world.setTimeOfDay(15000)
                    block.dimension.spawnEntity('omni_science:tnt_phantom', block.center())
                    block.dimension.spawnEntity('omni_science:tnt_phantom', block.center())
                    block.dimension.spawnEntity('omni_science:tnt_phantom', block.center())
                    block.dimension.spawnEntity('omni_science:tnt_phantom', block.center())
                    block.dimension.spawnEntity('omni_science:tnt_phantom', block.center())
                }
                //lucky block drops a Creeper DJ spawn egg
                else if (chance <= 36 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:creeper_dj_spawn_egg', 1), block.center())
                    })
                }
                //lucky block drops a Squid Helmet
                else if (chance <= 37 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:squid_helmet', 1), block.center())
                    })
                }
                else if (chance <= 38 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('minecraft:iron_ingot', 16), block.center())
                    })
                }
                else if (chance <= 39 / 43) {
                    system.run(() => {
                        block.dimension.playSound("entity.mad_villager", block.location, { volume: 1, pitch: 1 })
                    })
                }
                else if (chance <= 40 / 43) {
                    system.run(() => {
                        const spawn = system.runInterval(() => {
                            block.dimension.spawnEntity("minecraft:xp_orb", block.location)
                            system.runTimeout(() => {
                                system.clearRun(spawn)
                            }, 200)
                        }, 10)
                    })
                }
                else if (chance <= 41 / 43) {
                    system.run(() => {
                        const diamond = system.runInterval(() => {
                            block.dimension.spawnItem(new ItemStack('minecraft:diamond', 1), block.center())
                            system.runTimeout(() => {
                                system.clearRun(diamond)
                            }, 20)
                        }, 5)
                    }
                    )
                }
                else if (chance <= 42 / 43) {
                    system.run(() => {
                        const meat = system.runInterval(() => {
                            block.dimension.spawnItem(new ItemStack('minecraft:cooked_beef', 1), block.center())
                            system.runTimeout(() => {
                                system.clearRun(meat)
                            }, 900)
                        }, 5)
                    })
                }
                else if (chance <= 43 / 43) {
                    system.run(() => {
                        block.dimension.spawnItem(new ItemStack('omni_science:bagel', 10), block.center())
                    })
                }
            }
        }
    }
    )
}
)