Advertisement
Cwackers

receive.lua

May 26th, 2024 (edited)
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Cloned
  2. -- Program to receive messages from computers/
  3. -- turtles using flex.lua "send" function
  4. -- <Flexico64@gmail.com>
  5.  
  6. --------------------------------------
  7. -- |¯\|¯¯] /¯]|¯¯][¯¯]\\  //|¯¯]|¯\ --
  8. -- | /| ] | [ | ]  ][  \\// | ] | / --
  9. -- | \|__] \_]|__][__]  \/  |__]| \ --
  10. --------------------------------------
  11.  
  12. local log_file = "log.txt"
  13. local options_file = "flex_options.cfg"
  14. os.loadAPI("flex.lua")
  15. local modem_channel = 6464
  16.  
  17.  
  18. if fs.exists(options_file) then
  19.  local file = fs.open("flex_options.cfg", "r")
  20.  local line = file.readLine()
  21.  while line ~= nil do
  22.   if string.find(line, "modem_channel=") == 1 then
  23.    modem_channel = tonumber( string.sub(
  24.          line, 15, string.len(line) ) )
  25.    break
  26.   end --if
  27.   line = file.readLine()
  28.  end --while
  29.  file.close()
  30. end --if
  31.  
  32.  
  33. local modem
  34. local p = flex.getPeripheral("modem")
  35. if #p > 0 then
  36.  modem = peripheral.wrap(p[1])
  37.  modem.open(modem_channel)
  38. else
  39.  flex.printColors("Please attach a wireless"
  40.    .." or ender modem\n", colors.red)
  41.  sleep(2)
  42.  return
  43. end --if/else
  44.  
  45. local monitor
  46. p = flex.getPeripheral("monitor")
  47. if #p > 0 then
  48.  monitor = peripheral.wrap(p[1])
  49.  term.redirect(monitor)
  50.  monitor.clear()
  51.  monitor.setCursorPos(1,1)
  52.  monitor.setTextScale(0.5)
  53. end --if
  54. local lcd_x,lcd_y = monitor.getSize()
  55.  
  56.  
  57. local file, line
  58. local filelist = {}
  59. if fs.exists(log_file) then
  60.  file = fs.open(log_file, "r")
  61.  line = file.readLine()
  62.  
  63.  while line ~= nil do
  64.  
  65.   if line ~= "" or ( line == "" and
  66.      filelist[#filelist] ~= "" ) then
  67.    filelist[#filelist+1] = line
  68.   end --if
  69.  
  70.   line = file.readLine()
  71.  end --while
  72.  file.close()
  73.  file = fs.open(log_file, "a")
  74.  
  75. else
  76.  -- Log file does not exist: make one!
  77.  file = fs.open(log_file, "w")
  78.  
  79. end --if/else
  80.  
  81.  
  82. local x, y
  83. y = math.max(1,#filelist-lcd_y)
  84. for x=y, #filelist do
  85.  flex.printColors(filelist[x])
  86. end --for
  87.  
  88. if filelist[#filelist] ~= "" then
  89.  file.writeLine("")
  90. end --if
  91. file.close()
  92.  
  93.  
  94. term.setTextColor(colors.white)
  95. print("Waiting for message on channel "
  96.       ..tostring(modem_channel).."...")
  97.  
  98. while true do
  99.  local event, modemSide, senderChannel,
  100.     replyChannel, message, senderDistance =
  101.     os.pullEvent("modem_message")
  102.  
  103.  file = fs.open(log_file, "a")
  104.  file.writeLine(message)
  105.  file.close()
  106.  
  107.  flex.printColors(message)
  108.  
  109.  sleep(0.01)
  110. end --while
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement