Add crafting turtle script to listen for craft commands via modem

This commit is contained in:
MayaTheShy
2026-03-16 00:07:20 -04:00
parent 718cce0c7b
commit 10ec47611b

45
craftingTurtle.lua Normal file
View File

@@ -0,0 +1,45 @@
-- Crafting Turtle Script
-- Run this on the crafting turtle (turtle with crafting table).
-- Listens for craft commands from the master computer via wired modem.
local CRAFT_CHANNEL = 4203
local CRAFT_REPLY_CHANNEL = 4204
-- Find modem (wired or wireless)
local modem = peripheral.find("modem")
if not modem then
print("[ERR] No modem found! Attach a wired modem.")
return
end
modem.open(CRAFT_CHANNEL)
print("=================================")
print(" Crafting Turtle (Listener)")
print("=================================")
print("")
print("Listening on channel " .. CRAFT_CHANNEL)
print("Ready for craft commands from master.")
print("")
while true do
local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
if channel == CRAFT_CHANNEL and type(message) == "table" and message.type == "craft" then
local count = message.count or 1
print(string.format("[CRAFT] Received craft request (count=%d)", count))
local ok, err = turtle.craft(count)
if ok then
print("[CRAFT] Success!")
else
print("[CRAFT] Failed: " .. tostring(err))
end
modem.transmit(replyChannel, CRAFT_CHANNEL, {
type = "craft_result",
success = ok,
error = not ok and tostring(err) or nil,
})
end
end