AnonymusHochgenuss

TurtleVar

Jun 27th, 2026 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. -- Fetch the arguments passed to the program
  2. local args = { ... }
  3.  
  4. -- Check if an argument was provided
  5. if #args == 0 then
  6. print("Usage: turtle <command> [arguments]")
  7. return
  8. end
  9.  
  10. -- The first argument is the function name (e.g., "up", "place")
  11. local command = args[1]
  12. local apiFunction = turtle[command]
  13.  
  14. -- Check if the command actually exists in the turtle API and is a function
  15. if type(apiFunction) == "function" then
  16.  
  17. -- Prepare any extra arguments (like the '2' in "turtle select 2")
  18. local functionArgs = {}
  19. for i = 2, #args do
  20. -- Convert to numbers where possible, since shell arguments are always strings
  21. local num = tonumber(args[i])
  22. if num then
  23. table.insert(functionArgs, num)
  24. else
  25. table.insert(functionArgs, args[i])
  26. end
  27. end
  28.  
  29. -- Execute the function safely using pcall (protected call) and unpack
  30. local success, result, errorMsg = pcall(apiFunction, table.unpack(functionArgs))
  31.  
  32. if success then
  33. -- Print the result of the action (true/false)
  34. print("Success: " .. tostring(result))
  35. else
  36. -- If something went terribly wrong with the code itself
  37. print("Error: " .. tostring(result))
  38. end
  39. else
  40. print("Invalid turtle command: " .. tostring(command))
  41. end
Advertisement
Add Comment
Please, Sign In to add comment