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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_spectral_sand_active', {
        onPlace: e => {
            const { block } = e;
            block.dimension.spawnEntity(('old_nether:spectral_sand_active'), block.location);
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_temporal_nexus_active', {
        onPlace: e => {
            const { block } = e;
            block.dimension.runCommand("clear @p old_nether:luminous_stone 0 1")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_amethyst_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound place.amethyst_block @a[r=16] ~ ~ ~ 0.5 1")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_soul_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound soul.death @a[r=16] ~ ~ ~ 0.25 0.8")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_anomalous_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound dig.netherrack @a[r=16] ~ ~ ~ 0.5 1")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_sculk_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound break.sculk @a[r=16] ~ ~ ~ 1 1")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_wart_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound place.dripstone_block @a[r=16] ~ ~ ~ 0.5 1")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_ash_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound dig.basalt @a[r=16] ~ ~ ~ 0.4 1")
        }
    });
});

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockComponentRegistry.registerCustomComponent('old_nether:on_place_withered_sound', {
        beforeOnPlayerPlace: e => {
            const { player } = e;
            player.runCommandAsync("playsound spirit.death @a[r=16] ~ ~ ~ 0.12 1")
        }
    });
});

//Anomalous Door
const processedBlocks = new Map();

world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_anomalous_door', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const northBlock = block.north(); // Get the block to the east
            const southBlock = block.south(); // Get the block to the east
            const eastBlock = block.east(); // Get the block to the east
            const westBlock = block.west(); // Get the block to the east
            const { x, y, z } = block.location;
            if (processedBlocks.has(`${x},${y},${z}`)) {
                return; // Exit early if block has already been processed
            }
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (aboveBlock.typeId === 'minecraft:air' && !block.permutation.getState('old_nether:upper_block_bit')) {
                const aboveBlockPermutation = BlockPermutation.resolve('old_nether:anomalous_door', {
                    'old_nether:upper_block_bit': true,
                    'minecraft:cardinal_direction': cardinalDirection
                });
                aboveBlock.setPermutation(aboveBlockPermutation);
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (cardinalDirection === 'south' && eastBlock?.typeId === 'old_nether:anomalous_door') {
                const eastBlockStates = eastBlock.permutation.getAllStates();
                if (!eastBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const eastBlockAbove = aboveBlock.east();
                if (
                    aboveBlockCardinalDirection === 'south' &&
                    eastBlockAbove?.typeId === 'old_nether:anomalous_door'
                ) {
                    const eastBlockAboveStates = eastBlockAbove.permutation.getAllStates();
                    if (!eastBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...eastBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'east' && northBlock?.typeId === 'old_nether:anomalous_door') {
                const northBlockStates = northBlock.permutation.getAllStates();
                if (!northBlockStates['old_nether:door_hinge_bit']) {
                    // Change the alpha_livestock:door_hinge_bit state of the placed block to true
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const northBlockAbove = aboveBlock.north();
                if (
                    aboveBlockCardinalDirection === 'east' &&
                    northBlockAbove?.typeId === 'old_nether:anomalous_door'
                ) {
                    const northBlockAboveStates = northBlockAbove.permutation.getAllStates();
                    if (!northBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...northBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'north' && westBlock?.typeId === 'old_nether:anomalous_door') {
                const westBlockStates = westBlock.permutation.getAllStates();
                if (!westBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const westBlockAbove = aboveBlock.west();
                if (
                    aboveBlockCardinalDirection === 'north' &&
                    westBlockAbove?.typeId === 'old_nether:anomalous_door'
                ) {
                    const westBlockAboveStates = westBlockAbove.permutation.getAllStates();
                    if (!westBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...westBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'west' && southBlock?.typeId === 'old_nether:anomalous_door') {
                const southBlockStates = southBlock.permutation.getAllStates();
                if (!southBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const southBlockAbove = aboveBlock.south();
                if (
                    aboveBlockCardinalDirection === 'west' &&
                    southBlockAbove?.typeId === 'old_nether:anomalous_door'
                ) {
                    const southBlockAboveStates = southBlockAbove.permutation.getAllStates();
                    if (!southBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...southBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            processedBlocks.set(`${x},${y},${z}`, true);
        }
    });
});

// Subscribe to the 'playerBreakBlock' event to remove processed block entry
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;
    const { x, y, z } = block.location;

    // Remove the block's entry from the processedBlocks map
    processedBlocks.delete(`${x},${y},${z}`);

    // Check if the block above or below the destroyed block is also alpha_livestock:door
    const aboveBlock = block.above();
    const belowBlock = block.below();
    if (belowBlock.typeId === 'old_nether:anomalous_door') {
        belowBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y - 1},${z}`); // Remove the below block's entry
    } else if (aboveBlock.typeId === 'old_nether:anomalous_door') {
        aboveBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y + 1},${z}`); // Remove the above block's entry
    }
});

//Sculk Door
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_sculk_door', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const northBlock = block.north(); // Get the block to the east
            const southBlock = block.south(); // Get the block to the east
            const eastBlock = block.east(); // Get the block to the east
            const westBlock = block.west(); // Get the block to the east
            const { x, y, z } = block.location;
            if (processedBlocks.has(`${x},${y},${z}`)) {
                return; // Exit early if block has already been processed
            }
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (aboveBlock.typeId === 'minecraft:air' && !block.permutation.getState('old_nether:upper_block_bit')) {
                const aboveBlockPermutation = BlockPermutation.resolve('old_nether:sculk_door', {
                    'old_nether:upper_block_bit': true,
                    'minecraft:cardinal_direction': cardinalDirection
                });
                aboveBlock.setPermutation(aboveBlockPermutation);
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (cardinalDirection === 'south' && eastBlock?.typeId === 'old_nether:sculk_door') {
                const eastBlockStates = eastBlock.permutation.getAllStates();
                if (!eastBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const eastBlockAbove = aboveBlock.east();
                if (
                    aboveBlockCardinalDirection === 'south' &&
                    eastBlockAbove?.typeId === 'old_nether:sculk_door'
                ) {
                    const eastBlockAboveStates = eastBlockAbove.permutation.getAllStates();
                    if (!eastBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...eastBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'east' && northBlock?.typeId === 'old_nether:sculk_door') {
                const northBlockStates = northBlock.permutation.getAllStates();
                if (!northBlockStates['old_nether:door_hinge_bit']) {
                    // Change the alpha_livestock:door_hinge_bit state of the placed block to true
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const northBlockAbove = aboveBlock.north();
                if (
                    aboveBlockCardinalDirection === 'east' &&
                    northBlockAbove?.typeId === 'old_nether:sculk_door'
                ) {
                    const northBlockAboveStates = northBlockAbove.permutation.getAllStates();
                    if (!northBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...northBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'north' && westBlock?.typeId === 'old_nether:sculk_door') {
                const westBlockStates = westBlock.permutation.getAllStates();
                if (!westBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const westBlockAbove = aboveBlock.west();
                if (
                    aboveBlockCardinalDirection === 'north' &&
                    westBlockAbove?.typeId === 'old_nether:sculk_door'
                ) {
                    const westBlockAboveStates = westBlockAbove.permutation.getAllStates();
                    if (!westBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...westBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'west' && southBlock?.typeId === 'old_nether:sculk_door') {
                const southBlockStates = southBlock.permutation.getAllStates();
                if (!southBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const southBlockAbove = aboveBlock.south();
                if (
                    aboveBlockCardinalDirection === 'west' &&
                    southBlockAbove?.typeId === 'old_nether:sculk_door'
                ) {
                    const southBlockAboveStates = southBlockAbove.permutation.getAllStates();
                    if (!southBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...southBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            processedBlocks.set(`${x},${y},${z}`, true);
        }
    });
});

// Subscribe to the 'playerBreakBlock' event to remove processed block entry
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;
    const { x, y, z } = block.location;

    // Remove the block's entry from the processedBlocks map
    processedBlocks.delete(`${x},${y},${z}`);

    // Check if the block above or below the destroyed block is also alpha_livestock:door
    const aboveBlock = block.above();
    const belowBlock = block.below();
    if (belowBlock.typeId === 'old_nether:sculk_door') {
        belowBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y - 1},${z}`); // Remove the below block's entry
    } else if (aboveBlock.typeId === 'old_nether:sculk_door') {
        aboveBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y + 1},${z}`); // Remove the above block's entry
    }
});

//Soul Door
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_soul_door', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const northBlock = block.north(); // Get the block to the east
            const southBlock = block.south(); // Get the block to the east
            const eastBlock = block.east(); // Get the block to the east
            const westBlock = block.west(); // Get the block to the east
            const { x, y, z } = block.location;
            if (processedBlocks.has(`${x},${y},${z}`)) {
                return; // Exit early if block has already been processed
            }
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (aboveBlock.typeId === 'minecraft:air' && !block.permutation.getState('old_nether:upper_block_bit')) {
                const aboveBlockPermutation = BlockPermutation.resolve('old_nether:soul_door', {
                    'old_nether:upper_block_bit': true,
                    'minecraft:cardinal_direction': cardinalDirection
                });
                aboveBlock.setPermutation(aboveBlockPermutation);
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (cardinalDirection === 'south' && eastBlock?.typeId === 'old_nether:soul_door') {
                const eastBlockStates = eastBlock.permutation.getAllStates();
                if (!eastBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const eastBlockAbove = aboveBlock.east();
                if (
                    aboveBlockCardinalDirection === 'south' &&
                    eastBlockAbove?.typeId === 'old_nether:soul_door'
                ) {
                    const eastBlockAboveStates = eastBlockAbove.permutation.getAllStates();
                    if (!eastBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...eastBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'east' && northBlock?.typeId === 'old_nether:soul_door') {
                const northBlockStates = northBlock.permutation.getAllStates();
                if (!northBlockStates['old_nether:door_hinge_bit']) {
                    // Change the alpha_livestock:door_hinge_bit state of the placed block to true
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const northBlockAbove = aboveBlock.north();
                if (
                    aboveBlockCardinalDirection === 'east' &&
                    northBlockAbove?.typeId === 'old_nether:soul_door'
                ) {
                    const northBlockAboveStates = northBlockAbove.permutation.getAllStates();
                    if (!northBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...northBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'north' && westBlock?.typeId === 'old_nether:soul_door') {
                const westBlockStates = westBlock.permutation.getAllStates();
                if (!westBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const westBlockAbove = aboveBlock.west();
                if (
                    aboveBlockCardinalDirection === 'north' &&
                    westBlockAbove?.typeId === 'old_nether:soul_door'
                ) {
                    const westBlockAboveStates = westBlockAbove.permutation.getAllStates();
                    if (!westBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...westBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'west' && southBlock?.typeId === 'old_nether:soul_door') {
                const southBlockStates = southBlock.permutation.getAllStates();
                if (!southBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const southBlockAbove = aboveBlock.south();
                if (
                    aboveBlockCardinalDirection === 'west' &&
                    southBlockAbove?.typeId === 'old_nether:soul_door'
                ) {
                    const southBlockAboveStates = southBlockAbove.permutation.getAllStates();
                    if (!southBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...southBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            processedBlocks.set(`${x},${y},${z}`, true);
        }
    });
});

// Subscribe to the 'playerBreakBlock' event to remove processed block entry
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;
    const { x, y, z } = block.location;

    // Remove the block's entry from the processedBlocks map
    processedBlocks.delete(`${x},${y},${z}`);

    // Check if the block above or below the destroyed block is also alpha_livestock:door
    const aboveBlock = block.above();
    const belowBlock = block.below();
    if (belowBlock.typeId === 'old_nether:soul_door') {
        belowBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y - 1},${z}`); // Remove the below block's entry
    } else if (aboveBlock.typeId === 'old_nether:soul_door') {
        aboveBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y + 1},${z}`); // Remove the above block's entry
    }
});

//Wart Door
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_wart_door', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const northBlock = block.north(); // Get the block to the east
            const southBlock = block.south(); // Get the block to the east
            const eastBlock = block.east(); // Get the block to the east
            const westBlock = block.west(); // Get the block to the east
            const { x, y, z } = block.location;
            if (processedBlocks.has(`${x},${y},${z}`)) {
                return; // Exit early if block has already been processed
            }
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (aboveBlock.typeId === 'minecraft:air' && !block.permutation.getState('old_nether:upper_block_bit')) {
                const aboveBlockPermutation = BlockPermutation.resolve('old_nether:wart_door', {
                    'old_nether:upper_block_bit': true,
                    'minecraft:cardinal_direction': cardinalDirection
                });
                aboveBlock.setPermutation(aboveBlockPermutation);
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (cardinalDirection === 'south' && eastBlock?.typeId === 'old_nether:wart_door') {
                const eastBlockStates = eastBlock.permutation.getAllStates();
                if (!eastBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const eastBlockAbove = aboveBlock.east();
                if (
                    aboveBlockCardinalDirection === 'south' &&
                    eastBlockAbove?.typeId === 'old_nether:wart_door'
                ) {
                    const eastBlockAboveStates = eastBlockAbove.permutation.getAllStates();
                    if (!eastBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...eastBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'east' && northBlock?.typeId === 'old_nether:wart_door') {
                const northBlockStates = northBlock.permutation.getAllStates();
                if (!northBlockStates['old_nether:door_hinge_bit']) {
                    // Change the alpha_livestock:door_hinge_bit state of the placed block to true
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const northBlockAbove = aboveBlock.north();
                if (
                    aboveBlockCardinalDirection === 'east' &&
                    northBlockAbove?.typeId === 'old_nether:wart_door'
                ) {
                    const northBlockAboveStates = northBlockAbove.permutation.getAllStates();
                    if (!northBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...northBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'north' && westBlock?.typeId === 'old_nether:wart_door') {
                const westBlockStates = westBlock.permutation.getAllStates();
                if (!westBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const westBlockAbove = aboveBlock.west();
                if (
                    aboveBlockCardinalDirection === 'north' &&
                    westBlockAbove?.typeId === 'old_nether:wart_door'
                ) {
                    const westBlockAboveStates = westBlockAbove.permutation.getAllStates();
                    if (!westBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...westBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'west' && southBlock?.typeId === 'old_nether:wart_door') {
                const southBlockStates = southBlock.permutation.getAllStates();
                if (!southBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const southBlockAbove = aboveBlock.south();
                if (
                    aboveBlockCardinalDirection === 'west' &&
                    southBlockAbove?.typeId === 'old_nether:wart_door'
                ) {
                    const southBlockAboveStates = southBlockAbove.permutation.getAllStates();
                    if (!southBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...southBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            processedBlocks.set(`${x},${y},${z}`, true);
        }
    });
});

// Subscribe to the 'playerBreakBlock' event to remove processed block entry
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;
    const { x, y, z } = block.location;

    // Remove the block's entry from the processedBlocks map
    processedBlocks.delete(`${x},${y},${z}`);

    // Check if the block above or below the destroyed block is also alpha_livestock:door
    const aboveBlock = block.above();
    const belowBlock = block.below();
    if (belowBlock.typeId === 'old_nether:wart_door') {
        belowBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y - 1},${z}`); // Remove the below block's entry
    } else if (aboveBlock.typeId === 'old_nether:wart_door') {
        aboveBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y + 1},${z}`); // Remove the above block's entry
    }
});

//Ash Door
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_ash_door', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const northBlock = block.north(); // Get the block to the east
            const southBlock = block.south(); // Get the block to the east
            const eastBlock = block.east(); // Get the block to the east
            const westBlock = block.west(); // Get the block to the east
            const { x, y, z } = block.location;
            if (processedBlocks.has(`${x},${y},${z}`)) {
                return; // Exit early if block has already been processed
            }
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (aboveBlock.typeId === 'minecraft:air' && !block.permutation.getState('old_nether:upper_block_bit')) {
                const aboveBlockPermutation = BlockPermutation.resolve('old_nether:ash_door', {
                    'old_nether:upper_block_bit': true,
                    'minecraft:cardinal_direction': cardinalDirection
                });
                aboveBlock.setPermutation(aboveBlockPermutation);
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (cardinalDirection === 'south' && eastBlock?.typeId === 'old_nether:ash_door') {
                const eastBlockStates = eastBlock.permutation.getAllStates();
                if (!eastBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const eastBlockAbove = aboveBlock.east();
                if (
                    aboveBlockCardinalDirection === 'south' &&
                    eastBlockAbove?.typeId === 'old_nether:ash_door'
                ) {
                    const eastBlockAboveStates = eastBlockAbove.permutation.getAllStates();
                    if (!eastBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...eastBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'east' && northBlock?.typeId === 'old_nether:ash_door') {
                const northBlockStates = northBlock.permutation.getAllStates();
                if (!northBlockStates['old_nether:door_hinge_bit']) {
                    // Change the alpha_livestock:door_hinge_bit state of the placed block to true
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const northBlockAbove = aboveBlock.north();
                if (
                    aboveBlockCardinalDirection === 'east' &&
                    northBlockAbove?.typeId === 'old_nether:ash_door'
                ) {
                    const northBlockAboveStates = northBlockAbove.permutation.getAllStates();
                    if (!northBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...northBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'north' && westBlock?.typeId === 'old_nether:ash_door') {
                const westBlockStates = westBlock.permutation.getAllStates();
                if (!westBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const westBlockAbove = aboveBlock.west();
                if (
                    aboveBlockCardinalDirection === 'north' &&
                    westBlockAbove?.typeId === 'old_nether:ash_door'
                ) {
                    const westBlockAboveStates = westBlockAbove.permutation.getAllStates();
                    if (!westBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...westBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'west' && southBlock?.typeId === 'old_nether:ash_door') {
                const southBlockStates = southBlock.permutation.getAllStates();
                if (!southBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const southBlockAbove = aboveBlock.south();
                if (
                    aboveBlockCardinalDirection === 'west' &&
                    southBlockAbove?.typeId === 'old_nether:ash_door'
                ) {
                    const southBlockAboveStates = southBlockAbove.permutation.getAllStates();
                    if (!southBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...southBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            processedBlocks.set(`${x},${y},${z}`, true);
        }
    });
});

// Subscribe to the 'playerBreakBlock' event to remove processed block entry
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;
    const { x, y, z } = block.location;

    // Remove the block's entry from the processedBlocks map
    processedBlocks.delete(`${x},${y},${z}`);

    // Check if the block above or below the destroyed block is also alpha_livestock:door
    const aboveBlock = block.above();
    const belowBlock = block.below();
    if (belowBlock.typeId === 'old_nether:ash_door') {
        belowBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y - 1},${z}`); // Remove the below block's entry
    } else if (aboveBlock.typeId === 'old_nether:ash_door') {
        aboveBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y + 1},${z}`); // Remove the above block's entry
    }
});

//Withered Door
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_withered_door', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const northBlock = block.north(); // Get the block to the east
            const southBlock = block.south(); // Get the block to the east
            const eastBlock = block.east(); // Get the block to the east
            const westBlock = block.west(); // Get the block to the east
            const { x, y, z } = block.location;
            if (processedBlocks.has(`${x},${y},${z}`)) {
                return; // Exit early if block has already been processed
            }
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (aboveBlock.typeId === 'minecraft:air' && !block.permutation.getState('old_nether:upper_block_bit')) {
                const aboveBlockPermutation = BlockPermutation.resolve('old_nether:withered_door', {
                    'old_nether:upper_block_bit': true,
                    'minecraft:cardinal_direction': cardinalDirection
                });
                aboveBlock.setPermutation(aboveBlockPermutation);
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (cardinalDirection === 'south' && eastBlock?.typeId === 'old_nether:withered_door') {
                const eastBlockStates = eastBlock.permutation.getAllStates();
                if (!eastBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const eastBlockAbove = aboveBlock.east();
                if (
                    aboveBlockCardinalDirection === 'south' &&
                    eastBlockAbove?.typeId === 'old_nether:withered_door'
                ) {
                    const eastBlockAboveStates = eastBlockAbove.permutation.getAllStates();
                    if (!eastBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...eastBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'east' && northBlock?.typeId === 'old_nether:withered_door') {
                const northBlockStates = northBlock.permutation.getAllStates();
                if (!northBlockStates['old_nether:door_hinge_bit']) {
                    // Change the alpha_livestock:door_hinge_bit state of the placed block to true
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const northBlockAbove = aboveBlock.north();
                if (
                    aboveBlockCardinalDirection === 'east' &&
                    northBlockAbove?.typeId === 'old_nether:withered_door'
                ) {
                    const northBlockAboveStates = northBlockAbove.permutation.getAllStates();
                    if (!northBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...northBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'north' && westBlock?.typeId === 'old_nether:withered_door') {
                const westBlockStates = westBlock.permutation.getAllStates();
                if (!westBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const westBlockAbove = aboveBlock.west();
                if (
                    aboveBlockCardinalDirection === 'north' &&
                    westBlockAbove?.typeId === 'old_nether:withered_door'
                ) {
                    const westBlockAboveStates = westBlockAbove.permutation.getAllStates();
                    if (!westBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...westBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            if (cardinalDirection === 'west' && southBlock?.typeId === 'old_nether:withered_door') {
                const southBlockStates = southBlock.permutation.getAllStates();
                if (!southBlockStates['old_nether:door_hinge_bit']) {
                    const newPermutation = BlockPermutation.resolve(block.typeId, {
                        ...currentStates,
                        'old_nether:door_hinge_bit': true
                    });
                    block.setPermutation(newPermutation);
                }
                const aboveBlockCardinalDirection = aboveBlock.permutation.getState('minecraft:cardinal_direction');
                const southBlockAbove = aboveBlock.south();
                if (
                    aboveBlockCardinalDirection === 'west' &&
                    southBlockAbove?.typeId === 'old_nether:withered_door'
                ) {
                    const southBlockAboveStates = southBlockAbove.permutation.getAllStates();
                    if (!southBlockAboveStates['old_nether:door_hinge_bit']) {
                        const newAbovePermutation = BlockPermutation.resolve(aboveBlock.typeId, {
                            ...southBlockAboveStates,
                            'old_nether:door_hinge_bit': true
                        });
                        aboveBlock.setPermutation(newAbovePermutation);
                    }
                }
            }
            processedBlocks.set(`${x},${y},${z}`, true);
        }
    });
});

// Subscribe to the 'playerBreakBlock' event to remove processed block entry
world.afterEvents.playerBreakBlock.subscribe(eventData => {
    const { block } = eventData;
    const { x, y, z } = block.location;

    // Remove the block's entry from the processedBlocks map
    processedBlocks.delete(`${x},${y},${z}`);

    // Check if the block above or below the destroyed block is also alpha_livestock:door
    const aboveBlock = block.above();
    const belowBlock = block.below();
    if (belowBlock.typeId === 'old_nether:withered_door') {
        belowBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y - 1},${z}`); // Remove the below block's entry
    } else if (aboveBlock.typeId === 'old_nether:withered_door') {
        aboveBlock.setType('minecraft:air');
        processedBlocks.delete(`${x},${y + 1},${z}`); // Remove the above block's entry
    }
});

//Anomalous Fence Gate
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_anomalous_fence_gate', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (cardinalDirection === 'south') {
                const newalpha_livestockDirection = true;
                currentStates['old_nether:direction'] = newalpha_livestockDirection;
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (aboveBlock.typeId === 'minecraft:air') {
                const newAboveStates = { ...currentStates };
                if (cardinalDirection === 'south') {
                    delete newAboveStates['minecraft:cardinal_direction'];
                }
                newAboveStates['old_nether:invisible'] = true;
                aboveBlock.setPermutation(BlockPermutation.resolve('old_nether:anomalous_fence_gate', newAboveStates));
            }
        }
    });
});

//Sculk Fence Gate
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_sculk_fence_gate', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (cardinalDirection === 'south') {
                const newalpha_livestockDirection = true;
                currentStates['old_nether:direction'] = newalpha_livestockDirection;
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (aboveBlock.typeId === 'minecraft:air') {
                const newAboveStates = { ...currentStates };
                if (cardinalDirection === 'south') {
                    delete newAboveStates['minecraft:cardinal_direction'];
                }
                newAboveStates['old_nether:invisible'] = true;
                aboveBlock.setPermutation(BlockPermutation.resolve('old_nether:sculk_fence_gate', newAboveStates));
            }
        }
    });
});

//Soul Fence Gate
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_soul_fence_gate', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (cardinalDirection === 'south') {
                const newalpha_livestockDirection = true;
                currentStates['old_nether:direction'] = newalpha_livestockDirection;
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (aboveBlock.typeId === 'minecraft:air') {
                const newAboveStates = { ...currentStates };
                if (cardinalDirection === 'south') {
                    delete newAboveStates['minecraft:cardinal_direction'];
                }
                newAboveStates['old_nether:invisible'] = true;
                aboveBlock.setPermutation(BlockPermutation.resolve('old_nether:soul_fence_gate', newAboveStates));
            }
        }
    });
});

//Wart Fence Gate
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_wart_fence_gate', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (cardinalDirection === 'south') {
                const newalpha_livestockDirection = true;
                currentStates['old_nether:direction'] = newalpha_livestockDirection;
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (aboveBlock.typeId === 'minecraft:air') {
                const newAboveStates = { ...currentStates };
                if (cardinalDirection === 'south') {
                    delete newAboveStates['minecraft:cardinal_direction'];
                }
                newAboveStates['old_nether:invisible'] = true;
                aboveBlock.setPermutation(BlockPermutation.resolve('old_nether:wart_fence_gate', newAboveStates));
            }
        }
    });
});

//Ash Fence Gate
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_ash_fence_gate', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (cardinalDirection === 'south') {
                const newalpha_livestockDirection = true;
                currentStates['old_nether:direction'] = newalpha_livestockDirection;
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (aboveBlock.typeId === 'minecraft:air') {
                const newAboveStates = { ...currentStates };
                if (cardinalDirection === 'south') {
                    delete newAboveStates['minecraft:cardinal_direction'];
                }
                newAboveStates['old_nether:invisible'] = true;
                aboveBlock.setPermutation(BlockPermutation.resolve('old_nether:ash_fence_gate', newAboveStates));
            }
        }
    });
});

//Withered Fence Gate
world.beforeEvents.worldInitialize.subscribe(eventData => {
    eventData.blockComponentRegistry.registerCustomComponent('old_nether:on_place_withered_fence_gate', {
        onPlace(e) {
            const { block } = e;
            const aboveBlock = block.above();
            const currentStates = block.permutation.getAllStates();
            const cardinalDirection = currentStates['minecraft:cardinal_direction'];
            if (cardinalDirection === 'south') {
                const newalpha_livestockDirection = true;
                currentStates['old_nether:direction'] = newalpha_livestockDirection;
                const newPermutation = BlockPermutation.resolve(block.typeId, currentStates);
                block.setPermutation(newPermutation);
            }
            if (aboveBlock.typeId === 'minecraft:air') {
                const newAboveStates = { ...currentStates };
                if (cardinalDirection === 'south') {
                    delete newAboveStates['minecraft:cardinal_direction'];
                }
                newAboveStates['old_nether:invisible'] = true;
                aboveBlock.setPermutation(BlockPermutation.resolve('old_nether:withered_fence_gate', newAboveStates));
            }
        }
    });
});