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

world.beforeEvents.worldInitialize.subscribe((Initiate) => {
    Initiate.blockComponentRegistry.registerCustomComponent("ecbl_bc:veldtlands_leave_replacement", {
        onTick: (e) => {
            system.runTimeout(() => {
                if (e.block?.isValid()) e.block.setType('ecbl_bc:acacia_remover');
            }, 20)
        }
    });

    Initiate.blockComponentRegistry.registerCustomComponent("ecbl_bc:veldtlands_log_replacement", {
        onTick: (e) => {
            let above = e.block.above();
            let foundLeaves = false;

            for (let i = 0; i < 7; i++) {
                if (!above || !above.isValid()) break;

                if (above.typeId === 'ecbl_bc:veldtlands_leave_replacement') {
                    foundLeaves = true;
                    break;
                }

                above = above.above();
            }

            e.block.setType(foundLeaves ? 'minecraft:air' : 'minecraft:acacia_log');
        }
    });
});