Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Complete Turtle Client with ALL Commands
- local args = {...}
- local serverUrl = args[1] or "ws://localhost:8080"
- local turtleId = args[2] or ("turtle-" .. os.getComputerID())
- print("Connecting to MCP bridge at " .. serverUrl)
- print("Turtle ID: " .. turtleId)
- local ws, err = http.websocket(serverUrl)
- if not ws then
- print("Failed to connect: " .. tostring(err))
- return
- end
- print("Connected to MCP bridge!")
- -- Register this turtle
- ws.send(textutils.serializeJSON({
- type = "register",
- turtleId = turtleId,
- fuel = turtle.getFuelLevel()
- }))
- -- Complete command execution
- local function executeCommand(command)
- local result = {success = false, error = nil, data = nil}
- -- Movement Commands
- if command.action == "move" then
- local distance = command.distance or 1
- local success = true
- for i = 1, distance do
- local moveSuccess
- if command.direction == "forward" then
- moveSuccess = turtle.forward()
- elseif command.direction == "back" then
- moveSuccess = turtle.back()
- elseif command.direction == "up" then
- moveSuccess = turtle.up()
- elseif command.direction == "down" then
- moveSuccess = turtle.down()
- elseif command.direction == "turnLeft" then
- moveSuccess = turtle.turnLeft()
- elseif command.direction == "turnRight" then
- moveSuccess = turtle.turnRight()
- end
- if not moveSuccess then
- success = false
- result.error = "Movement blocked at step " .. i
- break
- end
- end
- result.success = success
- result.data = {moved = distance}
- -- Digging Commands
- elseif command.action == "dig" then
- local success
- if command.direction == "forward" then
- success = turtle.dig(command.side)
- elseif command.direction == "up" then
- success = turtle.digUp(command.side)
- elseif command.direction == "down" then
- success = turtle.digDown(command.side)
- end
- result.success = success
- -- Placing Commands
- elseif command.action == "place" then
- local success
- if command.direction == "forward" then
- success = turtle.place(command.text)
- elseif command.direction == "up" then
- success = turtle.placeUp(command.text)
- elseif command.direction == "down" then
- success = turtle.placeDown(command.text)
- end
- result.success = success
- -- Detection Commands
- elseif command.action == "detect" then
- local hasBlock
- if command.direction == "forward" then
- hasBlock = turtle.detect()
- elseif command.direction == "up" then
- hasBlock = turtle.detectUp()
- elseif command.direction == "down" then
- hasBlock = turtle.detectDown()
- end
- result.success = hasBlock
- -- Comparison Commands
- elseif command.action == "compare" then
- local matches
- if command.direction == "forward" then
- matches = turtle.compare()
- elseif command.direction == "up" then
- matches = turtle.compareUp()
- elseif command.direction == "down" then
- matches = turtle.compareDown()
- end
- result.success = matches
- elseif command.action == "compareTo" then
- result.success = turtle.compareTo(command.slot)
- -- Attack Commands
- elseif command.action == "attack" then
- local success
- if command.direction == "forward" then
- success = turtle.attack(command.side)
- elseif command.direction == "up" then
- success = turtle.attackUp(command.side)
- elseif command.direction == "down" then
- success = turtle.attackDown(command.side)
- end
- result.success = success
- -- Sucking Commands
- elseif command.action == "suck" then
- local success
- if command.direction == "forward" then
- success = turtle.suck(command.count)
- elseif command.direction == "up" then
- success = turtle.suckUp(command.count)
- elseif command.direction == "down" then
- success = turtle.suckDown(command.count)
- end
- result.success = success
- -- Dropping Commands
- elseif command.action == "drop" then
- local success
- if command.direction == "forward" then
- success = turtle.drop(command.count)
- elseif command.direction == "up" then
- success = turtle.dropUp(command.count)
- elseif command.direction == "down" then
- success = turtle.dropDown(command.count)
- end
- result.success = success
- -- Inventory Management
- elseif command.action == "select" then
- result.success = turtle.select(command.slot)
- elseif command.action == "getItemCount" then
- result.success = true
- result.data = {count = turtle.getItemCount(command.slot)}
- elseif command.action == "getItemSpace" then
- result.success = true
- result.data = {space = turtle.getItemSpace(command.slot)}
- elseif command.action == "getItemDetail" then
- local item = turtle.getItemDetail(command.slot, command.detailed)
- result.success = true
- result.data = {item = item}
- elseif command.action == "transferTo" then
- result.success = turtle.transferTo(command.slot, command.count)
- elseif command.action == "getSelectedSlot" then
- result.success = true
- result.data = {slot = turtle.getSelectedSlot()}
- -- Fuel Management
- elseif command.action == "refuel" then
- local fuelBefore = turtle.getFuelLevel()
- local success = turtle.refuel(command.count)
- local fuelAfter = turtle.getFuelLevel()
- result.success = success
- result.data = {
- fuel = fuelAfter,
- fuelGained = fuelAfter - fuelBefore
- }
- elseif command.action == "getFuelLevel" then
- result.success = true
- result.data = {fuel = turtle.getFuelLevel()}
- elseif command.action == "getFuelLimit" then
- result.success = true
- result.data = {limit = turtle.getFuelLimit()}
- -- Equipment Management
- elseif command.action == "equipLeft" then
- result.success = turtle.equipLeft()
- elseif command.action == "equipRight" then
- result.success = turtle.equipRight()
- elseif command.action == "getEquippedLeft" then
- local item = turtle.getEquippedLeft()
- result.success = true
- result.data = {item = item}
- elseif command.action == "getEquippedRight" then
- local item = turtle.getEquippedRight()
- result.success = true
- result.data = {item = item}
- -- Inspection Commands
- elseif command.action == "inspect" then
- local success, data
- if command.direction == "forward" then
- success, data = turtle.inspect()
- elseif command.direction == "up" then
- success, data = turtle.inspectUp()
- elseif command.direction == "down" then
- success, data = turtle.inspectDown()
- end
- result.success = success
- result.data = data
- -- Crafting
- elseif command.action == "craft" then
- local success = turtle.craft(command.limit)
- result.success = success
- result.data = {crafted = success and command.limit or 0}
- -- Complex Operations
- elseif command.action == "mine_area" then
- local width = command.width
- local length = command.length
- local depth = command.depth or 1
- result.success = true
- local blocksMined = 0
- for d = 1, depth do
- for l = 1, length do
- for w = 1, width do
- if turtle.dig() then
- blocksMined = blocksMined + 1
- end
- if w < width then
- turtle.forward()
- end
- end
- if l < length then
- if l % 2 == 1 then
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- else
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- end
- end
- end
- if d < depth then
- turtle.down()
- end
- end
- result.data = {blocksMined = blocksMined}
- -- Status Command (comprehensive)
- elseif command.action == "status" then
- result.success = true
- result.data = {
- fuel = turtle.getFuelLevel(),
- selectedSlot = turtle.getSelectedSlot(),
- inventory = {}
- }
- -- Try to get GPS coordinates
- local x, y, z = gps.locate()
- print("GPS coordinates: ", x, y, z)
- if x then
- result.data.position = {x = x, y = y, z = z, gps = true}
- else
- result.data.position = {x = nil, y = nil, z = nil, gps = false, error = "No GPS signal"}
- end
- -- Get full inventory
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item then
- result.data.inventory[i] = item
- end
- end
- end
- return result
- end
- -- Main loop
- print("Turtle ready for commands!")
- while true do
- local message = ws.receive()
- if message then
- local success, command = pcall(textutils.unserializeJSON, message)
- if success and command then
- print("Received command: " .. command.action)
- local result = executeCommand(command)
- ws.send(textutils.serializeJSON({
- type = "command_result",
- turtleId = turtleId,
- commandId = command.id,
- result = result
- }))
- end
- else
- print("Connection lost!")
- break
- end
- end
- ws.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement