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

const conversionLookup = {
    "ftb_bees:stem": "ftb_bees:stripped_stem",
    "ftb_bees:stem_full": "ftb_bees:stripped_stem_full",
};
world.afterEvents.itemUse.subscribe((event) => {
    const { source, itemStack } = event;
    if (!itemStack.hasTag("minecraft:is_axe")) {
        return;
    }
    // Find the block in front of the player
    const block = source.getBlockFromViewDirection({
        maxDistance: 6,
    });
    if (!block) {
        return;
    }
    const conversion = conversionLookup[block.block.typeId];
    if (!conversion) {
        return;
    }
    const location = block.block.location;
    const direction = block.block.permutation.getState("minecraft:facing_direction");
    if (!direction) {
        return;
    }
    if (itemStack.hasTag("minecraft:is_axe")) {
        source.dimension.runCommandAsync(`fill ${location.x} ${location.y} ${location.z} ${location.x} ${location.y} ${location.z} ${conversion} ["minecraft:facing_direction" = "${direction}"] replace ${block.block.typeId} ["minecraft:facing_direction" = "${direction}"]`);
        source.playSound("use.wood", {
            location: location,
        });
    }
});
