Advertisement
Sedrowow

FancyRemote

May 1st, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. -- turtle_remote.lua
  2. os.loadAPI("flex.lua")
  3.  
  4. local modem_channel = 8464 -- Use the same channel as your receive_pocket.lua
  5. local turtle_channel = 6464 -- The channel the turtle is listening on (from flex_options.cfg or default)
  6.  
  7. local modem
  8. local p = flex.getPeripheral("modem")
  9. if #p > 0 then
  10.     modem = peripheral.wrap(p[1])
  11.     modem.open(modem_channel)
  12. else
  13.     print("Please attach a wireless or ender modem")
  14.     sleep(2)
  15.     return
  16. end
  17.  
  18. local turtle_status = {} -- Table to store the last received status from the turtle
  19.  
  20. local function displayStatus()
  21.     term.clear()
  22.     term.setCursorPos(1, 1)
  23.  
  24.     print("--- Turtle Status ---")
  25.     if next(turtle_status) == nil then
  26.         print("Waiting for status update...")
  27.     else
  28.         print("Fuel: " .. tostring(turtle_status.fuel))
  29.         print("Position: X=" .. tostring(turtle_status.position.x) .. ", Y=" .. tostring(turtle_status.position.y) .. ", Z=" .. tostring(turtle_status.position.z))
  30.         print("Mining: " .. tostring(turtle_status.is_mining))
  31.         print("Estimated Time: " .. tostring(turtle_status.estimated_time))
  32.  
  33.         print("\nInventory:")
  34.         if next(turtle_status.inventory) == nil then
  35.             print("  Empty")
  36.         else
  37.             for slot, item in pairs(turtle_status.inventory) do
  38.                 print("  Slot " .. tostring(slot) .. ": " .. item.name .. " (" .. tostring(item.count) .. ")")
  39.             end
  40.         end
  41.     end
  42.  
  43.     print("\n--- Commands ---")
  44.     print("Press S to Refresh Status")
  45.     print("Press U to Force Unload")
  46.     print("Press Q to Quit")
  47. end
  48.  
  49. local function sendCommand(command_type)
  50.     local command_message = {
  51.         type = "command",
  52.         command = command_type
  53.     }
  54.     modem.transmit(modem_channel, turtle_channel, command_message)
  55. end
  56.  
  57. -- Initial status request
  58. sendCommand("status_request")
  59.  
  60. while true do
  61.     displayStatus()
  62.  
  63.     local event, param = os.pullEvent("key" or "modem_message")
  64.  
  65.     if event == "key" then
  66.         local key_code = param
  67.         if key_code == keys.s then
  68.             sendCommand("status_request")
  69.         elseif key_code == keys.u then
  70.             sendCommand("unload_command")
  71.         elseif key_code == keys.q then
  72.             break -- Exit the loop
  73.         end
  74.     elseif event == "modem_message" then
  75.         local modemSide, senderChannel, replyChannel, message, senderDistance = param, select(2, os.pullEvent()), select(3, os.pullEvent()), select(4, os.pullEvent()), select(5, os.pullEvent())
  76.  
  77.         -- Check if the message is from the expected turtle (optional but recommended)
  78.         -- You might need to send the turtle's ID with its status updates
  79.  
  80.         if type(message) == "table" and message.type == "status" then
  81.             turtle_status = message
  82.         elseif type(message) == "table" and message.type == "ack" then
  83.             print("\nCommand '" .. message.command .. "' acknowledged.")
  84.             sleep(1) -- Show acknowledgment message briefly
  85.         end
  86.     end
  87. end
  88.  
  89. modem.close(modem_channel)
  90. term.clear()
  91. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement