MtnMCG

tp turtle

Sep 7th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. -- Turtle Program
  2.  
  3. rednet.open("right") -- Adjust if your modem is on a different side
  4.  
  5. print("Turtle ready. Waiting for commands...")
  6.  
  7. -- Function to ensure the turtle has a block to place
  8. local function ensureBlock()
  9. for i = 1, 16 do
  10. if turtle.getItemCount(i) > 0 then
  11. turtle.select(i)
  12. return true
  13. end
  14. end
  15. return false
  16. end
  17.  
  18. while true do
  19. local sender, message, protocol = rednet.receive("trapdoor")
  20.  
  21. if message == "toggle" then
  22. print("Received toggle command")
  23.  
  24. local success = false
  25. local action = ""
  26.  
  27. if turtle.detect() then
  28. -- If there's a block in front, dig it
  29. success = turtle.dig()
  30. action = "removed"
  31. else
  32. -- If there's no block, try to place one
  33. if ensureBlock() then
  34. success = turtle.place()
  35. action = "placed"
  36. else
  37. print("No blocks in inventory to place")
  38. end
  39. end
  40.  
  41. if success then
  42. print("Block " .. action .. " successfully")
  43. rednet.send(sender, "Block " .. action, "trapdoor_response")
  44. else
  45. print("Failed to " .. (action == "removed" and "remove" or "place") .. " block")
  46. rednet.send(sender, "Failed to " .. (action == "removed" and "remove" or "place") .. " block", "trapdoor_response")
  47. end
  48. end
  49. end
Advertisement
Add Comment
Please, Sign In to add comment