Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Program
- rednet.open("right") -- Adjust if your modem is on a different side
- print("Turtle ready. Waiting for commands...")
- -- Function to ensure the turtle has a block to place
- local function ensureBlock()
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- while true do
- local sender, message, protocol = rednet.receive("trapdoor")
- if message == "toggle" then
- print("Received toggle command")
- local success = false
- local action = ""
- if turtle.detect() then
- -- If there's a block in front, dig it
- success = turtle.dig()
- action = "removed"
- else
- -- If there's no block, try to place one
- if ensureBlock() then
- success = turtle.place()
- action = "placed"
- else
- print("No blocks in inventory to place")
- end
- end
- if success then
- print("Block " .. action .. " successfully")
- rednet.send(sender, "Block " .. action, "trapdoor_response")
- else
- print("Failed to " .. (action == "removed" and "remove" or "place") .. " block")
- rednet.send(sender, "Failed to " .. (action == "removed" and "remove" or "place") .. " block", "trapdoor_response")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment