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.
Recommended: Install via installer (handles dependencies automatically):
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:
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
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")
module.setDefaultEquipped(leftTool, rightTool)Set the default tools to be equipped on each side
leftTool (string|nil): The tool to equip on the left side (e.g., "minecraft:diamond_pickaxe")rightTool (string|nil): The tool to equip on the right side (e.g., "minecraft:diamond_sword")module.setAlwaysEquipped(toolName)Set a tool that should always be kept equipped (has highest priority)
toolName (string|nil): The tool that should always be equippedmodule.wrap(peripheralName, side)Wrap a peripheral, automatically placing it if not present
peripheralName (string): The name/type of peripheral to wrapside (string): The side to place/wrap the peripheral on ("top", "bottom", "front", "back", "left", "right")module.find(peripheralType)Find and wrap a peripheral by type, equipping it as a tool if necessary
peripheralType (string): The type of peripheral to find (e.g., "modem", "workbench")module.getScanner()Get a plethora block scanner if available Returns a cached scanner proxy if already found, otherwise tries to find and wrap one
module.hasScanner()Check if a plethora scanner is available
module._debug()Print debug information about the module state, peripherals, and inventory Pauses between sections for user to read (press Enter to continue)