Advertisement
TheReturningVoid

Untitled

Jan 7th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. local function getDevices(deviceType) -- This function was taken from https://github.com/sandalle/minecraft_bigreactor_control and has been modified by https://github.com/Sanwi
  2.     local deviceName = nil
  3.     local deviceIndex = 1
  4.     local deviceList, deviceNames = {}, {} -- Empty array, which grows as we need
  5.     local peripheralList = peripheral.getNames() -- Get table of connected peripherals
  6.     for peripheralIndex = 1, #peripheralList do -- Log every device found
  7.         if (string.lower(peripheral.getType(peripheralList[peripheralIndex])) == deviceType) then -- Log devices found which match deviceType and which device index we give them
  8.             deviceNames[deviceIndex] = peripheralList[peripheralIndex]
  9.             deviceList[deviceIndex] = peripheral.wrap(peripheralList[peripheralIndex])
  10.             deviceIndex = deviceIndex + 1
  11.         end
  12.     end -- for peripheralIndex = 1, #peripheralList do
  13.     return deviceList, deviceNames
  14. end
  15.  
  16. local function createFile(name,content)
  17.   if fs.exists(name) then --if file exists
  18.     return
  19.   else
  20.     file = fs.open(name, "w")
  21.     file.write(content)
  22.     file.flush()
  23.   end
  24. end
  25.  
  26. local function readFile(fileName)
  27.   if fs.exists(fileName) then
  28.     fileTable = {name=fileName,content=fs.open(fileName, "r").readAll()}
  29.     cFile.close()
  30.     return fileTable
  31.   else
  32.     return "File not found"
  33.   end
  34. end
  35.  
  36. local function listen(modem,channels)
  37.   --Open channels
  38.   for i=1, #channels do
  39.     modem.open(channels[i])
  40.   end
  41.   --Listen
  42.   local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  43.   modem.closeAll()
  44.   return event, modemSide, senderChannel, replyChannel, message, senderDistance
  45. end
  46.  
  47. function main()
  48.   local channels = {400,401,402}
  49.  
  50.   while true do
  51.     write("> ")
  52.     deviceList, deviceNames = getDevices("modem")
  53.     local input = read();
  54.     if input == "exit" or input == "bye" then
  55.       print("Goodbye")
  56.       error() -- Only temporary until I figure out how to properly terminate a program
  57.     else
  58.       modem.transmit(replyChannel,senderChannel,input)
  59.       local event, modemSide, senderChannel, replyChannel, message, senderDistance = listen(deviceList[1],channels)
  60.       return message
  61.     end
  62.     write("\n")
  63.   end
  64. end
  65.  
  66. term.setTextColor(colors.yellow)
  67. write("Sulphur")
  68. term.setTextColor(colors.lightGray)
  69. write("Cloud")
  70. term.setTextColor(colors.white)
  71. print(" v. dev-0.0.1")
  72.  
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement