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

world.beforeEvents.worldInitialize.subscribe((Initiate) => {
    Initiate.blockComponentRegistry.registerCustomComponent("ecbl_bc:become_try_shortgrass", {
        onTick: (e) => {
            const below = e.block.below();
            const rotation = ['north', 'east', 'south', 'west'][Math.floor(Math.random() * 4)];
            if (below.isValid() && below.typeId === 'ecbl_bc:dry_grass_block') {
                e.block.setType('ecbl_bc:dry_grass')
                e.block.setPermutation(e.block.permutation.withState("minecraft:cardinal_direction", rotation))
            } else {
                e.block.setType('minecraft:air');
            }
        }
    });

    Initiate.blockComponentRegistry.registerCustomComponent("ecbl_bc:become_try_tallgrass", {
        onTick: (e) => {
            const below = e.block.below();

            if (below.isValid() && below.typeId === 'ecbl_bc:dry_grass_block') {
                const rotation = ['north', 'east', 'south', 'west'][Math.floor(Math.random() * 4)];

                e.block.setType('ecbl_bc:dry_tall_grass');
                let permutation = e.block.permutation.withState("ecbl_bc:upper_block_bit", false);
                permutation = permutation.withState("minecraft:cardinal_direction", rotation);

                e.block.setPermutation(permutation)

                const above = e.block.above();
                above.setType('ecbl_bc:dry_tall_grass')
                above.setPermutation(above.permutation.withState("minecraft:cardinal_direction", rotation))
            } else {
                e.block.setType('minecraft:air');
            }
        }
    });
});