Skip to content

Instantly share code, notes, and snippets.

@derekmurawsky
Last active May 21, 2026 01:40
Show Gist options
  • Select an option

  • Save derekmurawsky/1c6f50c35f5644de4a061ea387c95c2e to your computer and use it in GitHub Desktop.

Select an option

Save derekmurawsky/1c6f50c35f5644de4a061ea387c95c2e to your computer and use it in GitHub Desktop.
CCTweaked Mining Turtle Test
-- =========================================================
-- INDUSTRIAL MINING TURTLE (FIXED + STABLE)
-- =========================================================
-- =========================
-- CONFIG
-- =========================
local SHAFT_WIDTH = 1
local SHAFT_LENGTH = 1
local MAIN_WIDTH = 3
local MAIN_HEIGHT = 3
local BRANCH_WIDTH = 2
local BRANCH_HEIGHT = 2
local BRANCH_LENGTH = 20
local BRANCH_SPACING = 3
local MINE_HEIGHT_ABOVE_BEDROCK = 6
local TORCH_SPACING = 8
local MIN_FUEL_LEVEL = 500
local MIN_TORCHES = 16
local MAX_VEIN_DEPTH = 40
-- =========================
-- SLOT POLICY (IMPORTANT FIX)
-- =========================
-- 1 = fuel
-- 2 = torches
-- 3 = filler
-- 4 = supply chest
-- 5 = dump chest
-- 6-16 = mining ONLY
local MINING_SLOT_START = 6
local MINING_SLOT_END = 16
-- =========================
-- STATE
-- =========================
local pos = { x = 0, y = 0, z = 0, dir = 0 }
local stepsSinceTorch = 0
-- =========================
-- BLOCK LISTS
-- =========================
local ORES = {}
local TRASH = {
["minecraft:cobblestone"] = true,
["minecraft:cobbled_deepslate"] = true,
["minecraft:dirt"] = true,
["minecraft:gravel"] = true,
["minecraft:andesite"] = true,
["minecraft:diorite"] = true,
["minecraft:granite"] = true,
["minecraft:tuff"] = true,
["minecraft:netherrack"] = true
}
local FILLER = {
["minecraft:cobblestone"] = true,
["minecraft:cobbled_deepslate"] = true,
["minecraft:netherrack"] = true,
["minecraft:dirt"] = true
}
-- =========================
-- BASIC HELPERS
-- =========================
local function turnLeft()
turtle.turnLeft()
pos.dir = (pos.dir + 3) % 4
end
local function turnRight()
turtle.turnRight()
pos.dir = (pos.dir + 1) % 4
end
local function isOre(data)
return data and string.find(data.name, "ore")
end
local function isLava(data)
return data and string.find(data.name, "lava")
end
-- =========================
-- SLOT SAFETY (CRITICAL FIX)
-- =========================
local function ensureMiningSlot(slot)
return slot >= MINING_SLOT_START and slot <= MINING_SLOT_END
end
local function inventoryHasSpace()
for i = MINING_SLOT_START, MINING_SLOT_END do
if turtle.getItemCount(i) == 0 then
return true
end
end
return false
end
local function inventoryFull()
return not inventoryHasSpace()
end
-- =========================
-- CLEAN TRASH
-- =========================
local function cleanInventory()
for i = MINING_SLOT_START, MINING_SLOT_END do
local d = turtle.getItemDetail(i)
if d and TRASH[d.name] then
turtle.select(i)
turtle.drop()
end
end
end
-- =========================
-- ENDER CHEST SYSTEM
-- =========================
local function supplyChest()
turtle.select(4)
if turtle.detectDown() then turtle.digDown() end
turtle.placeDown()
end
local function dumpChest()
turtle.select(5)
if turtle.detectDown() then turtle.digDown() end
turtle.placeDown()
end
local function pickupChest()
turtle.digDown()
turtle.select(1)
end
local function restock()
supplyChest()
turtle.select(1)
turtle.suckDown()
turtle.select(2)
turtle.suckDown()
pickupChest()
end
local function dumpItems()
dumpChest()
for i = MINING_SLOT_START, MINING_SLOT_END do
local d = turtle.getItemDetail(i)
if d and not TRASH[d.name] then
turtle.select(i)
turtle.dropDown()
end
end
pickupChest()
end
-- =========================
-- MAINTENANCE
-- =========================
local function maintenance()
cleanInventory()
if turtle.getFuelLevel() < MIN_FUEL_LEVEL then
supplyChest()
turtle.select(1)
turtle.suckDown()
pickupChest()
turtle.refuel(8)
end
if turtle.getItemCount(2) < MIN_TORCHES then
restock()
end
if inventoryFull() then
dumpItems()
end
end
-- =========================
-- SAFE MOVEMENT (FIXED PICKUP ISSUE)
-- =========================
local function ensureSpace()
if inventoryFull() then
dumpItems()
end
end
local function forward()
ensureSpace()
while true do
local ok, data = turtle.inspect()
if ok then
if isLava(data) then
turtle.dig()
else
turtle.dig()
end
end
if turtle.forward() then break end
turtle.attack()
sleep(0.1)
end
if pos.dir == 0 then pos.z = pos.z - 1
elseif pos.dir == 1 then pos.x = pos.x + 1
elseif pos.dir == 2 then pos.z = pos.z + 1
elseif pos.dir == 3 then pos.x = pos.x - 1 end
end
local function up()
while not turtle.up() do
turtle.digUp()
turtle.attackUp()
sleep(0.1)
end
pos.y = pos.y + 1
end
local function down()
while not turtle.down() do
turtle.digDown()
turtle.attackDown()
sleep(0.1)
end
pos.y = pos.y - 1
end
-- =========================
-- TORCHES
-- =========================
local function placeTorch()
turtle.select(2)
turnLeft(); turnLeft()
turtle.placeDown()
turnLeft(); turnLeft()
turtle.select(1)
end
-- =========================
-- DIG AREA
-- =========================
local function digArea(w, h)
for x = 1, w do
for y = 1, h - 1 do turtle.digUp(); up() end
for y = 1, h - 1 do down() end
if x < w then
turnRight()
forward()
turnLeft()
end
end
turnLeft()
for i = 1, w - 1 do forward() end
turnRight()
end
-- =========================
-- ORE SCAN (simple safe version)
-- =========================
local function scanForOres()
local ok, data = turtle.inspect()
if ok and isOre(data) then
turtle.dig()
forward()
end
end
-- =========================
-- TUNNEL MINING
-- =========================
local function mineTunnel(len, w, h)
for i = 1, len do
forward()
digArea(w, h)
scanForOres()
stepsSinceTorch = stepsSinceTorch + 1
if stepsSinceTorch >= TORCH_SPACING then
placeTorch()
stepsSinceTorch = 0
end
maintenance()
end
end
-- =========================
-- SHAFT DESCENT
-- =========================
local function descend()
while true do
local ok, data = turtle.inspectDown()
if ok and data.name == "minecraft:bedrock" then break end
down()
maintenance()
end
for i = 1, MINE_HEIGHT_ABOVE_BEDROCK do
up()
end
end
-- =========================
-- MAIN LOOP
-- =========================
local function stripMine()
while true do
turnRight()
mineTunnel(BRANCH_LENGTH, BRANCH_WIDTH, BRANCH_HEIGHT)
turnLeft(); turnLeft()
for i = 1, BRANCH_LENGTH do forward() end
turnRight()
for i = 1, BRANCH_SPACING do
forward()
digArea(MAIN_WIDTH, MAIN_HEIGHT)
end
turnLeft()
mineTunnel(BRANCH_LENGTH, BRANCH_WIDTH, BRANCH_HEIGHT)
turnLeft(); turnLeft()
for i = 1, BRANCH_LENGTH do forward() end
turnLeft()
for i = 1, BRANCH_SPACING do
forward()
digArea(MAIN_WIDTH, MAIN_HEIGHT)
end
turnRight()
end
end
-- =========================
-- START
-- =========================
print("Industrial Miner Starting")
maintenance()
descend()
digArea(MAIN_WIDTH, MAIN_HEIGHT)
stripMine()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment