turtle-connector.lua
98 lines · 2.7 KB
Turtle Connector Script
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/turtle-manager/turtle-connector.lua
| 1 | -- Turtle Connector Script |
| 2 | -- This script helps connect turtles to CraftOS-PC Remote |
| 3 | -- Run this on any turtle to establish a connection to VS Code |
| 4 | |
| 5 | local function printBanner() |
| 6 | term.clear() |
| 7 | term.setCursorPos(1, 1) |
| 8 | print("========================================") |
| 9 | print(" TURTLE CONNECTOR FOR VS CODE") |
| 10 | print("========================================") |
| 11 | print() |
| 12 | end |
| 13 | |
| 14 | local function getTurtleInfo() |
| 15 | local label = os.getComputerLabel() or "Unlabeled" |
| 16 | local id = os.getComputerID() |
| 17 | return label, id |
| 18 | end |
| 19 | |
| 20 | local function checkRequirements() |
| 21 | local hasModem = false |
| 22 | local sides = {"left", "right", "top", "bottom", "front", "back"} |
| 23 | |
| 24 | for _, side in ipairs(sides) do |
| 25 | if peripheral.isPresent(side) then |
| 26 | local p = peripheral.wrap(side) |
| 27 | if p and p.isWirelessModem then |
| 28 | hasModem = true |
| 29 | break |
| 30 | end |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | return hasModem |
| 35 | end |
| 36 | |
| 37 | local function main() |
| 38 | printBanner() |
| 39 | |
| 40 | local label, id = getTurtleInfo() |
| 41 | print("Turtle Label: " .. label) |
| 42 | print("Turtle ID: " .. id) |
| 43 | print() |
| 44 | |
| 45 | -- Check for wireless modem |
| 46 | if not checkRequirements() then |
| 47 | print("ERROR: No wireless modem found!") |
| 48 | print("Please attach a wireless modem to any side of this turtle.") |
| 49 | print("Press any key to exit...") |
| 50 | os.pullEvent("key") |
| 51 | return |
| 52 | end |
| 53 | |
| 54 | print("Wireless modem detected!") |
| 55 | print() |
| 56 | print("To connect this turtle to VS Code:") |
| 57 | print("1. Install the CraftOS-PC extension in VS Code") |
| 58 | print("2. Click the CraftOS-PC button in the sidebar") |
| 59 | print("3. Click 'Connect to Remote'") |
| 60 | print("4. Copy the connection command from VS Code") |
| 61 | print("5. Paste it here and press Enter") |
| 62 | print() |
| 63 | print("Alternatively, visit: https://remote.craftos-pc.cc") |
| 64 | print() |
| 65 | |
| 66 | while true do |
| 67 | write("Connection command: ") |
| 68 | local command = read() |
| 69 | |
| 70 | if command and command ~= "" then |
| 71 | print("Executing connection command...") |
| 72 | print("Command: " .. command) |
| 73 | print() |
| 74 | |
| 75 | -- Execute the command |
| 76 | local success, result = pcall(function() |
| 77 | shell.run(command) |
| 78 | end) |
| 79 | |
| 80 | if success then |
| 81 | print("Connection established!") |
| 82 | print("You can now manage this turtle from VS Code.") |
| 83 | break |
| 84 | else |
| 85 | print("Connection failed: " .. tostring(result)) |
| 86 | print("Please try again or check your command.") |
| 87 | end |
| 88 | else |
| 89 | print("Please enter a valid connection command.") |
| 90 | end |
| 91 | |
| 92 | print() |
| 93 | end |
| 94 | end |
| 95 | |
| 96 | -- Run the main function |
| 97 | main() |
| 98 |