(async (bot) => {

    // Planning for this turn:
    // The previous error was `AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert.ok(slot < 9)`,
    // which means the item `newPickaxe` was not in a hotbar slot (0-8) when `setQuickBarSlot` was called.
    // I need to ensure the pickaxe is moved to a hotbar slot first if it's not already there.
    
    // Step 0: Ensure bot has a pickaxe to clear obstacles
    let hasPickaxe = bot.inventory.items().some(item => item.name.includes("pickaxe")); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    if (!hasPickaxe) {
        bot.chat("No pickaxe found. Crafting one now."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        // Find a temporary spot to place the crafting table for self-crafting
        let botPos = bot.entity.position.offset(0, -1, 0).floored(); // Block bot is standing on
        let tempCraftingTablePos = null; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
        // Search for a suitable spot (air block above, solid block below) in a 2-block radius
        let foundSpot = false; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        for (let dy = -1; dy <= 1; dy++) {
            for (let dx = -2; dx <= 2; dx++) {
                for (let dz = -2; dz <= 2; dz++) {
                    if (dx === 0 && dy === 0 && dz === 0) continue; // Don't try current spot
                    let potentialPos = botPos.offset(dx, dy, dz); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                    let potentialBlockAt = bot.blockAt(potentialPos); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                    let potentialBlockBelow = bot.blockAt(potentialPos.offset(0, -1, 0)); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
                    if (potentialBlockAt && potentialBlockAt.name === "air" && potentialBlockBelow && potentialBlockBelow.type !== 0) {
                        tempCraftingTablePos = potentialPos; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                        foundSpot = true; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                        break; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                    }
                }
                if (foundSpot) break; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
            if (foundSpot) break; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    
        if (!foundSpot) {
            bot.chat("Could not find any suitable spot to place temporary crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            throw new Error("Could not find suitable spot for temporary crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    
        bot.chat(`Placing temporary crafting table at ${tempCraftingTablePos.x}, ${tempCraftingTablePos.y}, ${tempCraftingTablePos.z}.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        const craftingTableItem = bot.inventory.items().find(item => item.name === "crafting_table"); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        if (!craftingTableItem) {
            bot.chat("Error: No crafting table in inventory to place. Cannot craft pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            throw new Error("No crafting table in inventory to craft pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    
        // Move crafting table to hotbar if not already there
        if (craftingTableItem.slot > 8) {
            let emptyHotbarSlot = bot.inventory.emptySlotRange(0, 8); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            if (emptyHotbarSlot.length > 0) {
                await bot.moveSlotItem(craftingTableItem.slot, emptyHotbarSlot[0]); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                craftingTableItem = bot.inventory.items().find(item => item.name === "crafting_table"); // Re-find after move
            } else {
                bot.chat("No empty hotbar slot for crafting table. Cannot place it."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                throw new Error("No empty hotbar slot for crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        }
        await bot.setQuickBarSlot(craftingTableItem.slot); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        await skills.placeBlock(bot, "crafting_table", tempCraftingTablePos.x, tempCraftingTablePos.y, tempCraftingTablePos.z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        await skills.wait(bot, 500); // Give a moment for the block to place
    
        const craftingTableBlock = bot.blockAt(tempCraftingTablePos); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        if (!craftingTableBlock || craftingTableBlock.name !== "crafting_table") {
            bot.chat("Failed to place crafting table. Cannot craft pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            throw new Error("Failed to place crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    
        const stonePickaxeRecipe = bot.recipesFor("stone_pickaxe", null, 1, craftingTableBlock)[0]; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        if (stonePickaxeRecipe) {
            bot.chat("Crafting stone pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            await bot.craft.craftItem(stonePickaxeRecipe, 1, craftingTableBlock); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            bot.chat("Crafted stone pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        } else {
            bot.chat("No recipe found for stone pickaxe. This shouldn't happen with cobblestone and sticks."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            throw new Error("No recipe for stone pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    
        // Reclaim the crafting table
        bot.chat("Breaking temporary crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        let newPickaxe = bot.inventory.items().find(item => item.name.includes("pickaxe")); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        if (newPickaxe) {
            // Move pickaxe to hotbar if not already there
            if (newPickaxe.slot > 8) {
                let emptyHotbarSlot = bot.inventory.emptySlotRange(0, 8); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                if (emptyHotbarSlot.length > 0) {
                    await bot.moveSlotItem(newPickaxe.slot, emptyHotbarSlot[0]); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                    newPickaxe = bot.inventory.items().find(item => item.name.includes("pickaxe")); // Re-find after move
                } else {
                    bot.chat("No empty hotbar slot for pickaxe. Cannot equip for breaking."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                    // Try to break with current equipped item if no slot, this might fail but is a last resort.
                }
            }
            if (newPickaxe && newPickaxe.slot <= 8) { // Check again if it's now in hotbar
                await bot.setQuickBarSlot(newPickaxe.slot); // Equip the newly crafted pickaxe
            } else {
                bot.chat("Warning: Pickaxe not found in hotbar after crafting for breaking crafting table. Attempting to break with current item."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        } else {
            bot.chat("Warning: Pickaxe not found after crafting for breaking crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
        await skills.breakBlockAt(bot, tempCraftingTablePos.x, tempCraftingTablePos.y, tempCraftingTablePos.z); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        await skills.wait(bot, 500); // Wait for item to drop and be collected
        bot.chat("Reclaimed crafting table."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    
    } else {
        bot.chat("Already have a pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    }
    
    // Ensure a pickaxe is equipped for general pathfinding/breaking
    const equippedPickaxe = bot.inventory.items().find(item => item.name.includes("pickaxe")); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    if (equippedPickaxe) {
        if (equippedPickaxe.slot > 8) { // If not in hotbar
            let emptyHotbarSlot = bot.inventory.emptySlotRange(0, 8); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            if (emptyHotbarSlot.length > 0) {
                await bot.moveSlotItem(equippedPickaxe.slot, emptyHotbarSlot[0]); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                const movedPickaxe = bot.inventory.items().find(item => item.name.includes("pickaxe") && item.slot === emptyHotbarSlot[0]); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                if (movedPickaxe) {
                    await bot.setQuickBarSlot(movedPickaxe.slot); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                    bot.chat(`Equipped ${movedPickaxe.name} after moving to hotbar.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                } else {
                    bot.chat("Failed to move pickaxe to hotbar and equip."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                }
            } else {
                bot.chat("No empty hotbar slot to move pickaxe for equipping."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
                throw new Error("No empty hotbar slot for pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            }
        } else { // Already in hotbar
            await bot.setQuickBarSlot(equippedPickaxe.slot); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
            bot.chat(`Equipped ${equippedPickaxe.name}.`); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        }
    } else {
        bot.chat("No pickaxe found even after attempting to craft. Cannot proceed without a pickaxe."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
        throw new Error("No pickaxe to proceed."); if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}
    }

log(bot, 'Code finished.');

})