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

// Subscribe to 'playerPlaceBlock' event
world.afterEvents.playerPlaceBlock.subscribe(eventData => {
    const { block } = eventData;

    system.run(() => {
        // Check if the placed block is 'pu_biomes:cave_sand'
        if (block.typeId !== 'pu_biomes:cave_sand') return;

        // Get the block below
        const belowBlock = block.below();

        // Check if the block below is 'pu_biomes:hotrock'
        if (belowBlock?.typeId === 'pu_biomes:hotrock') {
            // Get the current block state
            const blockState = belowBlock.permutation.getAllStates();
            
            // Update 'pu_biomes:is_sandy' to true
            blockState['pu_biomes:is_sandy'] = true;
            belowBlock.setPermutation(BlockPermutation.resolve('pu_biomes:hotrock', blockState));
        }
    });
});

// Subscribe to 'playerBreakBlock' event
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;

    system.run(() => {
        // Check if the broken block is 'pu_biomes:cave_sand'
        if (block.typeId == 'pu_biomes:cave_sand') return;

        // Get the block below
        const belowBlock = block.below();

        // Check if the block below is 'pu_biomes:hotrock'
        if (belowBlock?.typeId === 'pu_biomes:hotrock') {
            // Get the current block state
            const blockState = belowBlock.permutation.getAllStates();
            
            // Update 'pu_biomes:is_sandy' to false
            blockState['pu_biomes:is_sandy'] = false;
            belowBlock.setPermutation(BlockPermutation.resolve('pu_biomes:hotrock', blockState));
        }
    });
});