import { world, GameMode, system } from "@minecraft/server";

system.beforeEvents.startup.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('rustic:beehive', {
        onRandomTick: e => {
            const { block, dimension } = e;
            const blockLocation = block.center()
            dimension.runCommand(`playsound mob.bee.pollinate @a ${blockLocation.x} ${blockLocation.y} ${blockLocation.z} 0.30`)
        },
        onPlayerDestroy: e => {
            const { player, block, dimension, destroyedBlockPermutation } = e;
            const blockLocation = block.center()
            if (player.matches({ gameMode: GameMode.survival })) {
                if (Math.random() < 0.4) {
                    const bee = dimension.spawnEntity("minecraft:bee",blockLocation)
                    bee.triggerEvent("hive_destroyed")
                }
            }
        },
    })
})