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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('v360:fern_thatch', {
        onRandomTick({ block }) {
            const { x, y, z } = block.location
            const random = Math.random()
            if (random <= 1 / 6) {
                block.dimension.spawnEntity('alex:vallumraptor', block.center())
                block.dimension.runCommand(`setblock ${x} ${y} ${z} air [] destroy`)
            }
            else if (random <= 2 / 6) {
                block.dimension.spawnEntity('alex:grottoceratops', block.center())
                block.dimension.runCommand(`setblock ${x} ${y} ${z} air [] destroy`)
            }
            else if (random <= 3 / 6) {
                block.dimension.spawnEntity('alex:relicheirus', block.center())
                block.dimension.runCommand(`setblock ${x} ${y} ${z} air [] destroy`)
            }
            else if (random <= 4 / 6) {
                block.dimension.spawnEntity('alex:tremorsaurus', block.center())
                block.dimension.runCommand(`setblock ${x} ${y} ${z} air [] destroy`)
            }
            else if (random <= 5 / 6) {
                block.dimension.spawnEntity('alex:subterranodon', block.center())
                block.dimension.runCommand(`setblock ${x} ${y} ${z} air [] destroy`)
            }
            else if (random <= 6 / 6) {
                block.dimension.spawnEntity('alex:trilocaris', block.center())
                block.dimension.runCommand(`setblock ${x} ${y} ${z} air [] destroy`)
            }
        },
        onPlayerDestroy({ block }) {
            const random = Math.random()
            if (random <= 1 / 2) {
                block.dimension.spawnEntity('alex:primal', block.center())
            }
            else if (random <= 2 / 4) {
                block.dimension.spawnEntity('alex:primal', block.center())
                block.dimension.spawnEntity('alex:primal', block.center())
            }
            else if (random <= 3 / 4) {
                block.dimension.spawnEntity('alex:primal', block.center())
                block.dimension.spawnEntity('alex:primal', block.center())
                block.dimension.spawnEntity('alex:primal', block.center())
            }
            else if (random <= 4 / 4) {
                block.dimension.spawnEntity('alex:primal', block.center())
                block.dimension.spawnEntity('alex:primal', block.center())
                block.dimension.spawnEntity('alex:primal', block.center())
                block.dimension.spawnEntity('alex:primal', block.center())
            }
        }
    })
})