Advertisement
Shaka01

mainPC show changed items

Jan 31st, 2023 (edited)
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local inventory = peripheral.wrap("right")
  2. local monitor = peripheral.wrap("top")
  3. local inventoryTable = nil
  4. local a, b = monitor.getSize()
  5. local y = 3
  6. local sleepTime = 1
  7. local quit = false
  8.  
  9. -- Set the background color to black and the text color to white
  10. monitor.setBackgroundColor(colors.black)
  11. monitor.clear()
  12.  
  13. function writeTitle()
  14.   monitor.setCursorPos(1, 1)
  15.   monitor.setTextColor(colors.yellow)
  16.   monitor.setBackgroundColor(colors.gray)
  17.   monitor.clearLine()
  18.   monitor.write("Inventory changes:")
  19.   monitor.setBackgroundColor(colors.black)
  20. end
  21.  
  22.  
  23. function doall()
  24.   local monitorWidth, monitorHeight = monitor.getSize()
  25.   local lineHeight = 1 -- adjust this if your font size is larger than 1
  26.   local maxLines = math.floor((monitorHeight - 2) / lineHeight)
  27.   local lines = {}
  28.  
  29.   while true do
  30.     local newInventoryTable = {}
  31.     for slot, item in pairs(inventory.list()) do
  32.       -- Replace the "_" characters with spaces
  33.       local itemName = string.gsub(item.name, "_", " ")
  34.       -- Remove everything before the ":" character
  35.       itemName = string.match(itemName, ": *(.*)") or itemName
  36.       newInventoryTable[slot] = {name = itemName, count = item.count}
  37.     end
  38.  
  39.     -- Compare the new table to the previous table if both are not nil
  40.     if inventoryTable then
  41.       for slot, item in pairs(newInventoryTable) do
  42.         local prevItem = inventoryTable[slot] or {name = "", count = 0}
  43.         if prevItem.name ~= item.name or prevItem.count ~= item.count then
  44.           -- The item has changed, add it to the lines table
  45.           local change = item.count - prevItem.count
  46.           local sign = ""
  47.           if change > 0 then
  48.             sign = "+"
  49.           elseif change < 0 then
  50.             sign = ""
  51.           end
  52.           table.insert(lines, {text = item.name .. " " .. sign .. change, change = change})
  53.         end
  54.       end
  55.  
  56.       -- Check for items that were removed
  57.       for slot, prevItem in pairs(inventoryTable) do
  58.         if not newInventoryTable[slot] then
  59.           -- The item was removed, add it to the lines table
  60.           table.insert(lines, {text = prevItem.name .. " " .. -prevItem.count, change = -prevItem.count})
  61.         end
  62.       end
  63.     end
  64.  
  65.     -- Update the previous table
  66.     inventoryTable = newInventoryTable
  67.  
  68.     -- Write the title
  69.     writeTitle()
  70.  
  71.     -- Scroll the lines table
  72.     local startLine = math.max(#lines - maxLines + 1, 1)
  73.     local endLine = #lines
  74.     for i = startLine, endLine do
  75.       local line = lines[i]
  76.       monitor.setCursorPos(1, i - startLine + 3)
  77.       monitor.setTextColor(colors.white)
  78.       if line.change > 0 then
  79.         monitor.setTextColor(colors.green)
  80.       elseif line.change < 0 then
  81.         monitor.setTextColor(colors.orange)
  82.       end
  83.       monitor.clearLine()
  84.       -- centerText(monitor, i - startLine + 2, line.text)
  85.       monitor.write(line.text)
  86.     end
  87.  
  88.     -- Truncate the lines table if it's too long
  89.     while #lines > maxLines do
  90.       table.remove(lines, 1)
  91.     end
  92.  
  93.     -- Sleep for a bit
  94.     os.sleep(sleepTime)
  95.   end
  96. end
  97.  
  98.  
  99. inventoryTable = nil
  100.  
  101.  
  102.  
  103. function getMonitorTouch()
  104.     local event, side, x, y = os.pullEvent("monitor_touch")
  105.     if event then
  106.         quit = true
  107.     end
  108. end
  109.  
  110. while true do
  111.     if quit == true then
  112.         break
  113.     end
  114.     parallel.waitForAny(doall, getMonitorTouch)
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement