attach v1.0.3

A turtle utility module for ComputerCraft that provides safe block interaction, automatic tool management, and peripheral handling for turtles. Features: Automatic tool equipping based on action type (dig, attack, place), unsafe block protection to prevent accidentally digging storage blocks, peripheral auto-placement and wrapping, modem protection (never unequips modems), configurable default and always-equipped tools, and proxy wrappers for peripherals.

Installation

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua attach
View on GitHub →

This pattern downloads and runs libraries at runtime, automatically installing any that are missing:

-- Auto-install and require libraries
local libs = {"attach"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local attach = require(libDir .. "attach")

-- Use the library
-- (your code here)

Alternative: Direct download via wget:

wget https://raw.githubusercontent.com/Twijn/cc-misc/main/util/attach.lua

Examples

Using with Runtime Installation

This example shows how to download and use libraries with automatic installation:

-- Auto-install and require libraries
local libs = {"attach"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local attach = require(libDir .. "attach")

-- Use the library

Usage Examples

local attach = require("attach")
-- Set up default tools
attach.setDefaultEquipped("minecraft:diamond_pickaxe", "minecraft:diamond_sword")
attach.setAlwaysEquipped("computercraft:wireless_modem_advanced")
-- Safe digging (won't dig chests, other turtles, etc.)
attach.dig()      -- Automatically equips pickaxe
attach.digUp()    -- Also safe
attach.digDown()  -- Also safe
-- Attack with automatic sword equipping
attach.attack()
-- Find and wrap a peripheral
local modem = attach.find("modem")

Functions

module.setDefaultEquipped(leftTool, rightTool)

View source on GitHub

Set the default tools to be equipped on each side

Parameters:

module.setAlwaysEquipped(toolName)

View source on GitHub

Set a tool that should always be kept equipped (has highest priority)

Parameters:

module.wrap(peripheralName, side)

View source on GitHub

Wrap a peripheral, automatically placing it if not present

Parameters:
Returns: table # A proxy table with all peripheral methods that auto-replaces if removed

module.find(peripheralType)

View source on GitHub

Find and wrap a peripheral by type, equipping it as a tool if necessary

Parameters:
Returns: string|nil # Error message if peripheral was not found

module.getScanner()

View source on GitHub

Get a plethora block scanner if available Returns a cached scanner proxy if already found, otherwise tries to find and wrap one

Returns: table|nil # A scanner proxy with scan() method, or nil if not found

module.hasScanner()

View source on GitHub

Check if a plethora scanner is available

Returns: boolean # True if a scanner is available

module._debug()

View source on GitHub

Print debug information about the module state, peripherals, and inventory Pauses between sections for user to read (press Enter to continue)