AnttiV

Untitled

Apr 21st, 2025
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | Gaming | 0 0
  1. local monitorSide = "top"
  2. local filePath = "/disk/items"
  3. local refreshTime = 30
  4.  
  5. local monitor = peripheral.wrap(monitorSide)
  6. if not monitor then
  7.     print("Error: No monitor on side '" .. monitorSide .. "'")
  8.     return
  9. end
  10.  
  11. monitor.setTextScale(1)
  12.  
  13. local function readItems()
  14.     if not fs.exists(filePath) then return {} end
  15.     local file = fs.open(filePath, "r")
  16.     local items = {}
  17.     local line = file.readLine()
  18.     while line do
  19.         table.insert(items, line)
  20.         line = file.readLine()
  21.     end
  22.     file.close()
  23.     return items
  24. end
  25.  
  26. local function writeItems(items)
  27.     local file = fs.open(filePath, "w")
  28.     for _, item in ipairs(items) do
  29.         file.writeLine(item)
  30.     end
  31.     file.close()
  32. end
  33.  
  34. local function displayItems(items)
  35.     monitor.setBackgroundColor(colors.black)
  36.     monitor.setTextColor(colors.white)
  37.     monitor.clear()
  38.  
  39.     for i, item in ipairs(items) do
  40.         monitor.setCursorPos(1, i)    -- item text
  41.         monitor.write(i .. ". " .. item)
  42.  
  43.         monitor.setCursorPos(30, i)   -- 'button'
  44.         monitor.setTextColor(colors.red)
  45.         monitor.write("[X]")
  46.         monitor.setTextColor(colors.white)
  47.     end
  48. end
  49.  
  50. local function detectClick(items, x, y)
  51.     -- Assuming 'button' starts at x=30, ends at 32 or 33
  52.     if y >=1 and y <= #items then
  53.         if x >= 30 and x <= 32 then
  54.             -- Remove the clicked item
  55.             table.remove(items, y)
  56.             writeItems(items)
  57.             displayItems(items)
  58.             print("Removed item at line " .. y)
  59.         end
  60.     end
  61. end
  62.  
  63. -- Main Loop
  64. while true do
  65.     local items = readItems()
  66.     displayItems(items)
  67.  
  68.     local timer = os.startTimer(refreshTime)
  69.     while true do
  70.         local event, p1, p2, p3 = os.pullEvent()
  71.  
  72.         if event == "monitor_touch" then
  73.             local side, x, y = p1, p2, p3
  74.             if side == monitorSide then
  75.                 detectClick(items, x, y)
  76.             end
  77.  
  78.         elseif event == "timer" and p1 == timer then
  79.             break  -- Time to refresh the list
  80.         end
  81.     end
  82. end
  83.  
Advertisement
Add Comment
Please, Sign In to add comment