Advertisement
Sedrowow

FancyReciever

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