89 lines
2.7 KiB
Lua
89 lines
2.7 KiB
Lua
-- Crafting Turtle Script
|
|
-- Run this on the crafting turtle (turtle with crafting table).
|
|
-- Polls its own crafting grid slots and auto-crafts when items appear.
|
|
-- No modem required — works via the wired network inventory access.
|
|
|
|
-- Crafting grid slots in a turtle's 4x4 inventory
|
|
local CRAFT_SLOTS = {1, 2, 3, 5, 6, 7, 9, 10, 11}
|
|
|
|
-- How long to wait after detecting items before crafting,
|
|
-- giving the master time to finish placing all ingredients.
|
|
local SETTLE_DELAY = 1.0
|
|
|
|
-- Polling interval (seconds)
|
|
local POLL_INTERVAL = 0.5
|
|
|
|
print("=================================")
|
|
print(" Crafting Turtle (Auto-Craft)")
|
|
print("=================================")
|
|
print("")
|
|
|
|
-- Verify this is a crafting turtle
|
|
if not turtle or not turtle.craft then
|
|
print("[ERR] No turtle.craft() available!")
|
|
print(" This must be a Crafting Turtle.")
|
|
return
|
|
end
|
|
|
|
print("Polling crafting slots for items...")
|
|
print("Ready. Items placed in grid will be auto-crafted.")
|
|
print("")
|
|
|
|
local function hasCraftingItems()
|
|
for _, slot in ipairs(CRAFT_SLOTS) do
|
|
if turtle.getItemCount(slot) > 0 then
|
|
return true
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
local function describeGrid()
|
|
local parts = {}
|
|
for _, slot in ipairs(CRAFT_SLOTS) do
|
|
local detail = turtle.getItemDetail(slot)
|
|
if detail then
|
|
table.insert(parts, string.format(" slot %d: %s x%d", slot, detail.name, detail.count))
|
|
end
|
|
end
|
|
return table.concat(parts, "\n")
|
|
end
|
|
|
|
while true do
|
|
if hasCraftingItems() then
|
|
-- Wait for the master to finish placing all ingredients
|
|
print("[CRAFT] Items detected in grid, waiting " .. SETTLE_DELAY .. "s...")
|
|
sleep(SETTLE_DELAY)
|
|
|
|
-- Check again — items might have been pulled back (abort)
|
|
if hasCraftingItems() then
|
|
print("[CRAFT] Grid contents:")
|
|
print(describeGrid())
|
|
print("[CRAFT] Attempting craft...")
|
|
|
|
local ok, err = turtle.craft()
|
|
|
|
if ok then
|
|
-- Show what was crafted
|
|
local results = {}
|
|
for slot = 1, 16 do
|
|
local detail = turtle.getItemDetail(slot)
|
|
if detail then
|
|
table.insert(results, string.format("%s x%d", detail.name, detail.count))
|
|
end
|
|
end
|
|
print("[CRAFT] Success! Result: " .. table.concat(results, ", "))
|
|
else
|
|
print("[CRAFT] Failed: " .. tostring(err))
|
|
end
|
|
|
|
-- Wait for master to pull results before polling again
|
|
sleep(2)
|
|
else
|
|
print("[CRAFT] Items removed before crafting (aborted by master)")
|
|
end
|
|
end
|
|
|
|
sleep(POLL_INTERVAL)
|
|
end
|