import { system, world, ItemStack, GameMode, Entity, PlayerInteractWithEntityAfterEvent } from '@minecraft/server';

const axeIds = [
    'minecraft:wooden_axe',
    'minecraft:stone_axe',
    'minecraft:iron_axe',
    'minecraft:diamond_axe',
    'minecraft:golden_axe',
    'minecraft:netherite_axe'
];


world.beforeEvents.itemUseOn.subscribe(evd => {
    const { source: player, block, itemStack: itemUsed } = evd;

    if (!itemUsed) return;

    if (axeIds.includes(itemUsed.typeId)) {
        if (block.permutation.matches('ecbl_bc:hollow_birch_log_branches')) {
            const blockPosition = block.location;

            system.run(() => {
                // Verificação para cada direção
                player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_branches["minecraft:block_face"="north"]`).then((result) => {
                    if (result.successCount > 0) {
                        player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="north"]`);
                        player.runCommand(`structure load stick_drop ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
                        player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
                    }
                }).catch((error) => {
                    console.error(`Erro ao executar o comando: ${error}`);
                });

                player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_branches["minecraft:block_face"="south"]`).then((result) => {
                    if (result.successCount > 0) {
                        player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="south"]`);
                        player.runCommand(`structure load stick_drop ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
                        player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
                    }
                }).catch((error) => {
                    console.error(`Erro ao executar o comando: ${error}`);
                });

                player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_branches["minecraft:block_face"="west"]`).then((result) => {
                    if (result.successCount > 0) {
                        player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="west"]`);
                        player.runCommand(`structure load stick_drop ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
                        player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
                    }
                }).catch((error) => {
                    console.error(`Erro ao executar o comando: ${error}`);
                });

                player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_branches["minecraft:block_face"="east"]`).then((result) => {
                    if (result.successCount > 0) {
                        player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="east"]`);
                        player.runCommand(`structure load stick_drop ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
                        player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
                    }
                }).catch((error) => {
                    console.error(`Erro ao executar o comando: ${error}`);
                });

                player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_branches["minecraft:block_face"="up"]`).then((result) => {
                    if (result.successCount > 0) {
                        player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="up"]`);
                        player.runCommand(`structure load stick_drop ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
                        player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
                    }
                }).catch((error) => {
                    console.error(`Erro ao executar o comando: ${error}`);
                });

                player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_branches["minecraft:block_face"="down"]`).then((result) => {
                    if (result.successCount > 0) {
                        player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="down"]`);
                        player.runCommand(`structure load stick_drop ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
                        player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
                    }
                }).catch((error) => {
                    console.error(`Erro ao executar o comando: ${error}`);
                });
            });
        }
    }
});





world.beforeEvents.itemUseOn.subscribe(evd => {
    const { source: player, block, itemStack: itemUsed } = evd;
  
    if (!itemUsed) return;
  
    if (axeIds.includes(itemUsed.typeId)) {
      if (block.permutation.matches('ecbl_bc:hollow_birch_log_brown_mushrooms')) {
        const blockPosition = block.location;
  
        system.run(() => {
          // Verificação para cada direção
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_brown_mushrooms["minecraft:block_face"="north"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="north"]`);
              player.runCommand(`structure load mushroom_brown ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_brown_mushrooms["minecraft:block_face"="south"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="south"]`);
              player.runCommand(`structure load mushroom_brown ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_brown_mushrooms["minecraft:block_face"="west"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="west"]`);
              player.runCommand(`structure load mushroom_brown ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_brown_mushrooms["minecraft:block_face"="east"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="east"]`);
              player.runCommand(`structure load mushroom_brown ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_brown_mushrooms["minecraft:block_face"="up"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="up"]`);
              player.runCommand(`structure load mushroom_brown ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_brown_mushrooms["minecraft:block_face"="down"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="down"]`);
              player.runCommand(`structure load mushroom_brown ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
        });
      }
    }
  });
  


  world.beforeEvents.itemUseOn.subscribe(evd => {
    const { source: player, block, itemStack: itemUsed } = evd;
  
    if (!itemUsed) return;
  
    if (axeIds.includes(itemUsed.typeId)) {
      if (block.permutation.matches('ecbl_bc:hollow_birch_log_red_mushrooms')) {
        const blockPosition = block.location;
  
        system.run(() => {
          // Verificação para cada direção
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_red_mushrooms["minecraft:block_face"="north"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="north"]`);
              player.runCommand(`structure load mushroom_red ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_red_mushrooms["minecraft:block_face"="south"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="south"]`);
              player.runCommand(`structure load mushroom_red ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_red_mushrooms["minecraft:block_face"="west"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="west"]`);
              player.runCommand(`structure load mushroom_red ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_red_mushrooms["minecraft:block_face"="east"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="east"]`);
              player.runCommand(`structure load mushroom_red ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_red_mushrooms["minecraft:block_face"="up"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="up"]`);
              player.runCommand(`structure load mushroom_red ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
  
          player.runCommandAsync(`testforblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_red_mushrooms["minecraft:block_face"="down"]`).then((result) => {
            if (result.successCount > 0) {
              player.runCommand(`setblock ${blockPosition.x} ${blockPosition.y} ${blockPosition.z} ecbl_bc:hollow_birch_log_normal["minecraft:block_face"="down"]`);
              player.runCommand(`structure load mushroom_red ${blockPosition.x} ${blockPosition.y + 1} ${blockPosition.z}`);
              player.playSound('use.wood', { pitch: 1, location: blockPosition, volume: 1 });
            }
          }).catch((error) => {
            console.error(`Erro ao executar o comando: ${error}`);
          });
        });
      }
    }
  });
  