Advertisement
Loneranger419

checkFluid

Feb 1st, 2025 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | None | 0 0
  1. local side = "right" -- Change this to your peripheral side
  2. local device = peripheral.wrap(side)
  3.  
  4. if not device then
  5.     print("No peripheral found on " .. side .. "!")
  6.     return
  7. end
  8.  
  9. -- Attempt to get the fluid list
  10. local success, fluidList = pcall(device.listFluid)
  11.  
  12. if not success or type(fluidList) ~= "table" then
  13.     print("Error: listFluid() did not return a valid table.")
  14.     return
  15. end
  16.  
  17. -- Process fluid data
  18. local totalAmount = 0
  19. local fluidLines = {}
  20.  
  21. for _, fluid in ipairs(fluidList) do
  22.     if type(fluid.amount) == "number" then
  23.         totalAmount = totalAmount + fluid.amount
  24.     end
  25.     table.insert(fluidLines, fluid.displayName .. ": " .. fluid.amount)
  26. end
  27.  
  28. -- Add total amount at the end
  29. table.insert(fluidLines, "--------------------")
  30. table.insert(fluidLines, "Total Fluid Amount: " .. totalAmount)
  31.  
  32. -- Scrollable output handling
  33. local outputOffset = 0
  34. local outputPageSize = 10 -- Lines per page
  35.  
  36. local function displayOutput()
  37.     term.clear()
  38.     term.setCursorPos(1,1)
  39.    
  40.     print("Fluid Storage List (Scroll Up/Down):\n")
  41.  
  42.     for i = 1, outputPageSize do
  43.         local index = i + outputOffset
  44.         if index > #fluidLines then break end
  45.         print(fluidLines[index])
  46.     end
  47.  
  48.     print("\nScroll Up/Down to navigate, Q to exit.")
  49. end
  50.  
  51. displayOutput()
  52.  
  53. while true do
  54.     local event, key = os.pullEvent()
  55.    
  56.     if event == "mouse_scroll" then
  57.         if key == 1 and outputOffset + outputPageSize < #fluidLines then
  58.             outputOffset = outputOffset + 1
  59.         elseif key == -1 and outputOffset > 0 then
  60.             outputOffset = outputOffset - 1
  61.         end
  62.         displayOutput()
  63.  
  64.     elseif event == "key" then
  65.         if key == keys.q then break -- Exit on 'Q'
  66.         elseif key == keys.down and outputOffset + outputPageSize < #fluidLines then
  67.             outputOffset = outputOffset + 1
  68.             displayOutput()
  69.         elseif key == keys.up and outputOffset > 0 then
  70.             outputOffset = outputOffset - 1
  71.             displayOutput()
  72.         end
  73.     end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement