// replaces the current block with the most common nearby blocks
import { EquipmentSlot, ItemStack, world, ItemEnchantableComponent } from "@minecraft/server";

world.beforeEvents.worldInitialize.subscribe((Initiate) => {
    Initiate.blockComponentRegistry.registerCustomComponent("ecbl_bc:drop_leaves", {
        onPlayerDestroy: (e) => {
            const { player, dimension, block } = e;

            if (!player || !player.isValid() || player.getGameMode() === 'creative') return;

            /** @type {ItemStack} */
            const item = player.getComponent('minecraft:equippable')?.getEquipment(EquipmentSlot.Mainhand);
            if (!item) return;

            /** @type {ItemEnchantableComponent} */
            const enchantments = item.getComponent('minecraft:enchantable')?.getEnchantment('silk_touch');

            if (item.typeId === 'minecraft:shears' || Boolean(enchantments)) {
                dimension.spawnItem(new ItemStack('minecraft:oak_leaves', 1), block.location);
            }
        }
    });
});