Advertisement
sjorsw

turtle-client

Jul 5th, 2025 (edited)
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.52 KB | None | 0 0
  1. -- Complete Turtle Client with ALL Commands
  2. local args = {...}
  3. local serverUrl = args[1] or "ws://localhost:8080"
  4. local turtleId = args[2] or ("turtle-" .. os.getComputerID())
  5.  
  6. print("Connecting to MCP bridge at " .. serverUrl)
  7. print("Turtle ID: " .. turtleId)
  8.  
  9. local ws, err = http.websocket(serverUrl)
  10. if not ws then
  11.     print("Failed to connect: " .. tostring(err))
  12.     return
  13. end
  14.  
  15. print("Connected to MCP bridge!")
  16.  
  17. -- Register this turtle
  18. ws.send(textutils.serializeJSON({
  19.     type = "register",
  20.     turtleId = turtleId,
  21.     fuel = turtle.getFuelLevel()
  22. }))
  23.  
  24. -- Complete command execution
  25. local function executeCommand(command)
  26.     local result = {success = false, error = nil, data = nil}
  27.    
  28.     -- Movement Commands
  29.     if command.action == "move" then
  30.         local distance = command.distance or 1
  31.         local success = true
  32.        
  33.         for i = 1, distance do
  34.             local moveSuccess
  35.             if command.direction == "forward" then
  36.                 moveSuccess = turtle.forward()
  37.             elseif command.direction == "back" then
  38.                 moveSuccess = turtle.back()
  39.             elseif command.direction == "up" then
  40.                 moveSuccess = turtle.up()
  41.             elseif command.direction == "down" then
  42.                 moveSuccess = turtle.down()
  43.             elseif command.direction == "turnLeft" then
  44.                 moveSuccess = turtle.turnLeft()
  45.             elseif command.direction == "turnRight" then
  46.                 moveSuccess = turtle.turnRight()
  47.             end
  48.            
  49.             if not moveSuccess then
  50.                 success = false
  51.                 result.error = "Movement blocked at step " .. i
  52.                 break
  53.             end
  54.         end
  55.        
  56.         result.success = success
  57.         result.data = {moved = distance}
  58.        
  59.     -- Digging Commands
  60.     elseif command.action == "dig" then
  61.         local success
  62.         if command.direction == "forward" then
  63.             success = turtle.dig(command.side)
  64.         elseif command.direction == "up" then
  65.             success = turtle.digUp(command.side)
  66.         elseif command.direction == "down" then
  67.             success = turtle.digDown(command.side)
  68.         end
  69.         result.success = success
  70.        
  71.     -- Placing Commands
  72.     elseif command.action == "place" then
  73.         local success
  74.         if command.direction == "forward" then
  75.             success = turtle.place(command.text)
  76.         elseif command.direction == "up" then
  77.             success = turtle.placeUp(command.text)
  78.         elseif command.direction == "down" then
  79.             success = turtle.placeDown(command.text)
  80.         end
  81.         result.success = success
  82.        
  83.     -- Detection Commands
  84.     elseif command.action == "detect" then
  85.         local hasBlock
  86.         if command.direction == "forward" then
  87.             hasBlock = turtle.detect()
  88.         elseif command.direction == "up" then
  89.             hasBlock = turtle.detectUp()
  90.         elseif command.direction == "down" then
  91.             hasBlock = turtle.detectDown()
  92.         end
  93.         result.success = hasBlock
  94.        
  95.     -- Comparison Commands
  96.     elseif command.action == "compare" then
  97.         local matches
  98.         if command.direction == "forward" then
  99.             matches = turtle.compare()
  100.         elseif command.direction == "up" then
  101.             matches = turtle.compareUp()
  102.         elseif command.direction == "down" then
  103.             matches = turtle.compareDown()
  104.         end
  105.         result.success = matches
  106.        
  107.     elseif command.action == "compareTo" then
  108.         result.success = turtle.compareTo(command.slot)
  109.        
  110.     -- Attack Commands
  111.     elseif command.action == "attack" then
  112.         local success
  113.         if command.direction == "forward" then
  114.             success = turtle.attack(command.side)
  115.         elseif command.direction == "up" then
  116.             success = turtle.attackUp(command.side)
  117.         elseif command.direction == "down" then
  118.             success = turtle.attackDown(command.side)
  119.         end
  120.         result.success = success
  121.        
  122.     -- Sucking Commands
  123.     elseif command.action == "suck" then
  124.         local success
  125.         if command.direction == "forward" then
  126.             success = turtle.suck(command.count)
  127.         elseif command.direction == "up" then
  128.             success = turtle.suckUp(command.count)
  129.         elseif command.direction == "down" then
  130.             success = turtle.suckDown(command.count)
  131.         end
  132.         result.success = success
  133.        
  134.     -- Dropping Commands
  135.     elseif command.action == "drop" then
  136.         local success
  137.         if command.direction == "forward" then
  138.             success = turtle.drop(command.count)
  139.         elseif command.direction == "up" then
  140.             success = turtle.dropUp(command.count)
  141.         elseif command.direction == "down" then
  142.             success = turtle.dropDown(command.count)
  143.         end
  144.         result.success = success
  145.        
  146.     -- Inventory Management
  147.     elseif command.action == "select" then
  148.         result.success = turtle.select(command.slot)
  149.        
  150.     elseif command.action == "getItemCount" then
  151.         result.success = true
  152.         result.data = {count = turtle.getItemCount(command.slot)}
  153.        
  154.     elseif command.action == "getItemSpace" then
  155.         result.success = true
  156.         result.data = {space = turtle.getItemSpace(command.slot)}
  157.        
  158.     elseif command.action == "getItemDetail" then
  159.         local item = turtle.getItemDetail(command.slot, command.detailed)
  160.         result.success = true
  161.         result.data = {item = item}
  162.        
  163.     elseif command.action == "transferTo" then
  164.         result.success = turtle.transferTo(command.slot, command.count)
  165.        
  166.     elseif command.action == "getSelectedSlot" then
  167.         result.success = true
  168.         result.data = {slot = turtle.getSelectedSlot()}
  169.        
  170.     -- Fuel Management
  171.     elseif command.action == "refuel" then
  172.         local fuelBefore = turtle.getFuelLevel()
  173.         local success = turtle.refuel(command.count)
  174.         local fuelAfter = turtle.getFuelLevel()
  175.         result.success = success
  176.         result.data = {
  177.             fuel = fuelAfter,
  178.             fuelGained = fuelAfter - fuelBefore
  179.         }
  180.        
  181.     elseif command.action == "getFuelLevel" then
  182.         result.success = true
  183.         result.data = {fuel = turtle.getFuelLevel()}
  184.        
  185.     elseif command.action == "getFuelLimit" then
  186.         result.success = true
  187.         result.data = {limit = turtle.getFuelLimit()}
  188.        
  189.     -- Equipment Management
  190.     elseif command.action == "equipLeft" then
  191.         result.success = turtle.equipLeft()
  192.        
  193.     elseif command.action == "equipRight" then
  194.         result.success = turtle.equipRight()
  195.        
  196.     elseif command.action == "getEquippedLeft" then
  197.         local item = turtle.getEquippedLeft()
  198.         result.success = true
  199.         result.data = {item = item}
  200.        
  201.     elseif command.action == "getEquippedRight" then
  202.         local item = turtle.getEquippedRight()
  203.         result.success = true
  204.         result.data = {item = item}
  205.        
  206.     -- Inspection Commands
  207.     elseif command.action == "inspect" then
  208.         local success, data
  209.         if command.direction == "forward" then
  210.             success, data = turtle.inspect()
  211.         elseif command.direction == "up" then
  212.             success, data = turtle.inspectUp()
  213.         elseif command.direction == "down" then
  214.             success, data = turtle.inspectDown()
  215.         end
  216.         result.success = success
  217.         result.data = data
  218.        
  219.     -- Crafting
  220.     elseif command.action == "craft" then
  221.         local success = turtle.craft(command.limit)
  222.         result.success = success
  223.         result.data = {crafted = success and command.limit or 0}
  224.        
  225.     -- Complex Operations
  226.     elseif command.action == "mine_area" then
  227.         local width = command.width
  228.         local length = command.length
  229.         local depth = command.depth or 1
  230.        
  231.         result.success = true
  232.         local blocksMined = 0
  233.        
  234.         for d = 1, depth do
  235.             for l = 1, length do
  236.                 for w = 1, width do
  237.                     if turtle.dig() then
  238.                         blocksMined = blocksMined + 1
  239.                     end
  240.                     if w < width then
  241.                         turtle.forward()
  242.                     end
  243.                 end
  244.                
  245.                 if l < length then
  246.                     if l % 2 == 1 then
  247.                         turtle.turnRight()
  248.                         turtle.forward()
  249.                         turtle.turnRight()
  250.                     else
  251.                         turtle.turnLeft()
  252.                         turtle.forward()
  253.                         turtle.turnLeft()
  254.                     end
  255.                 end
  256.             end
  257.            
  258.             if d < depth then
  259.                 turtle.down()
  260.             end
  261.         end
  262.        
  263.         result.data = {blocksMined = blocksMined}
  264.        
  265.     -- Status Command (comprehensive)
  266.     elseif command.action == "status" then
  267.         result.success = true
  268.         result.data = {
  269.             fuel = turtle.getFuelLevel(),
  270.             selectedSlot = turtle.getSelectedSlot(),
  271.             inventory = {}
  272.         }
  273.        
  274.         -- Try to get GPS coordinates
  275.         local x, y, z = gps.locate()
  276.         print("GPS coordinates: ", x, y, z)
  277.         if x then
  278.             result.data.position = {x = x, y = y, z = z, gps = true}
  279.         else
  280.             result.data.position = {x = nil, y = nil, z = nil, gps = false, error = "No GPS signal"}
  281.         end
  282.        
  283.         -- Get full inventory
  284.         for i = 1, 16 do
  285.             local item = turtle.getItemDetail(i)
  286.             if item then
  287.                 result.data.inventory[i] = item
  288.             end
  289.         end
  290.     end
  291.    
  292.     return result
  293. end
  294.  
  295. -- Main loop
  296. print("Turtle ready for commands!")
  297. while true do
  298.     local message = ws.receive()
  299.     if message then
  300.         local success, command = pcall(textutils.unserializeJSON, message)
  301.         if success and command then
  302.             print("Received command: " .. command.action)
  303.            
  304.             local result = executeCommand(command)
  305.            
  306.             ws.send(textutils.serializeJSON({
  307.                 type = "command_result",
  308.                 turtleId = turtleId,
  309.                 commandId = command.id,
  310.                 result = result
  311.             }))
  312.         end
  313.     else
  314.         print("Connection lost!")
  315.         break
  316.     end
  317. end
  318.  
  319. ws.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement