Advertisement
Shaka01

ME Bridge Stocker

Nov 12th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.97 KB | None | 0 0
  1. -- Initialize global variables
  2. local thresholdFile = "thresholds.txt"
  3. local meBridge = peripheral.wrap("top")  -- Change to the actual side of the ME Bridge
  4. local monitor = peripheral.find("monitor")  -- Adjust to the correct monitor
  5. local editing = false
  6. local sortBy = "name"  -- Default sort by name
  7. local thresholds = {}
  8.  
  9. -- Set monitor properties
  10. local monX, monY = monitor.getSize()
  11. local maxRows = monY - 2  -- Adjust based on row size
  12. local scrollPosition = 1  -- Tracks the first visible row
  13.  
  14. monitor.setTextScale(0.5)
  15. monitor.clear()
  16.  
  17. ---get API
  18. if fs.exists("API") == false then
  19. shell.run("pastebin", "get", "EzkfU5ZM", "API")
  20. end
  21. shaka = require("API")
  22.  
  23. -- Load thresholds from file
  24. local function loadThresholds()
  25.     local file = fs.open(thresholdFile, "r")
  26.     local thresholds = {}
  27.     if file then
  28.         for line in file.readLine do
  29.             local name, threshold = line:match("(.+)=(%d+)")
  30.             if name and threshold then
  31.                 thresholds[name] = tonumber(threshold)
  32.             end
  33.         end
  34.         file.close()
  35.     end
  36.     return thresholds
  37. end
  38.  
  39. -- Save thresholds to file
  40. local function saveThresholds()
  41.     local file = fs.open(thresholdFile, "w")
  42.     for name, threshold in pairs(thresholds) do
  43.         file.writeLine(name .. "=" .. threshold)
  44.     end
  45.     file.close()
  46. end
  47.  
  48. -- Update threshold for a given item
  49. local function setThreshold(name, value)
  50.     thresholds[name] = value
  51.     saveThresholds()
  52. end
  53.  
  54. -- Sort items by the selected criterion (name, count, or threshold)
  55. local function sortItems()
  56.     local items = {}
  57.     for name, threshold in pairs(thresholds) do
  58.         local item = meBridge.getItem({name = name}) or {}
  59.         table.insert(items, {name = name, count = item.amount or 0, threshold = threshold})
  60.     end
  61.     table.sort(items, function(a, b)
  62.         a.name = shaka.prettyName(a.name)
  63.         b.name = shaka.prettyName(b.name)
  64.         if sortBy == "name" then
  65.             return a.name < b.name
  66.         elseif sortBy == "count" then
  67.             return a.count > b.count
  68.         elseif sortBy == "threshold" then
  69.             return a.threshold > b.threshold
  70.         end
  71.     end)
  72.     return items
  73. end
  74.  
  75. -- Function to check crafting status for all items in parallel
  76. local function checkCraftingStatus(items)
  77.     local craftingStatus = {}  -- Table to store the crafting status for each item
  78.     local function checkStatusForItem(item)
  79.         local crafting = meBridge.isItemCrafting({name = item.name}) or false
  80.         craftingStatus[item.name] = crafting
  81.     end
  82.  
  83.     -- Run the crafting checks in parallel for all items
  84.     local parallelTasks = {}
  85.     for _, item in ipairs(items) do
  86.         table.insert(parallelTasks, function()
  87.             checkStatusForItem(item)
  88.         end)
  89.     end
  90.  
  91.     -- Execute all tasks in parallel
  92.     parallel.waitForAll(table.unpack(parallelTasks))
  93.  
  94.     return craftingStatus
  95. end
  96.  
  97.  
  98. -- Function to display item information with parallel rendering of rows
  99. local function displayMonitorInfoParallel(items)
  100.     local craftingStatus = checkCraftingStatus(items)  -- Get crafting statuses in parallel
  101.  
  102.     -- Clear the monitor before displaying updated content
  103.     monitor.setBackgroundColor(colors.green)
  104.     monitor.setTextColor(colors.black)
  105.     monitor.setCursorPos(1, 1)
  106.     monitor.clear()
  107.  
  108.     monitor.write("ITEM STATUS (Tap to Sort by " .. (sortBy == "name" and "Count" or "Name") .. ")")
  109.  
  110.     -- Display headers for item information
  111.     monitor.setCursorPos(1, 2)
  112.     monitor.setBackgroundColor(colors.gray)
  113.     monitor.setTextColor(colors.black)
  114.     monitor.clearLine()
  115.     monitor.write(string.format("%-22s %-8s %-10s %-9s", "Item Name", "Count", "Threshold", "Crafting"))
  116.  
  117.     -- Row number starts from 3 as headers take the first two lines
  118.     local row = 3
  119.     local totalItems = math.min(#items, scrollPosition + maxRows - 1)
  120.  
  121.     -- Parallel task creation for each row in the display
  122.     local parallelTasks = {}
  123.  
  124.     for i = scrollPosition, totalItems do
  125.         table.insert(parallelTasks, function()
  126.             local item = items[i]
  127.             local crafting = craftingStatus[item.name] or false  -- Get the crafting status from the parallel check
  128.  
  129.             -- Debugging: Print crafting status only for items being crafted
  130.             if crafting then
  131.                 print(string.format("Item: %s, Crafting: Yes", item.name))
  132.             end
  133.  
  134.             -- Set row color alternately for readability
  135.             monitor.setCursorPos(1, row)
  136.             monitor.setBackgroundColor(row % 2 == 0 and colors.gray or colors.black)
  137.             monitor.clearLine()
  138.  
  139.             monitor.setTextColor(colors.white)
  140.             monitor.write(string.format("%-23s", item.name))
  141.  
  142.             -- Display count with color depending on threshold
  143.             monitor.setTextColor(item.count < item.threshold and colors.red or colors.green)
  144.             monitor.write(string.format("%-9d", item.count))
  145.  
  146.             -- Display threshold
  147.             monitor.setTextColor(colors.white)
  148.             monitor.write(string.format("%-11d", item.threshold))
  149.  
  150.             -- Crafting status: Set to orange if crafting, otherwise light gray
  151.             if crafting then
  152.                 monitor.setTextColor(colors.orange)
  153.                 monitor.write("Yes")
  154.             else
  155.                 monitor.setTextColor(colors.lightGray)
  156.                 monitor.write("No")
  157.             end
  158.  
  159.             row = row + 1  -- Move to the next row for the next item
  160.         end)
  161.     end
  162.  
  163.     -- Run all tasks in parallel
  164.     parallel.waitForAll(table.unpack(parallelTasks))
  165.  
  166.     -- Display the last update time at the bottom
  167.     monitor.setCursorPos(1, row + 1)
  168.     monitor.setBackgroundColor(colors.black)
  169.     monitor.setTextColor(colors.lightGray)
  170.     monitor.clearLine()
  171.     monitor.write("Last update: " .. textutils.formatTime(os.time(), true))
  172. end
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. -- Handle touch input for sorting and scrolling
  180. local function monitorTouchHandler(items)
  181.     while true do
  182.         local event, side, x, y = os.pullEvent("monitor_touch")
  183.        
  184.         if y >= 2 and y < monY / 2 then  -- Scroll up
  185.             if scrollPosition > 1 then
  186.                 scrollPosition = scrollPosition - 1
  187.                 displayMonitorInfoParallel(items)
  188.             end
  189.         elseif y >= monY / 2 then  -- Scroll down
  190.             if scrollPosition + maxRows - 1 < #items then
  191.                 scrollPosition = scrollPosition + 1
  192.                 displayMonitorInfoParallel(items)
  193.             end
  194.         elseif y == 1 then  -- Toggle sorting
  195.             sortBy = (sortBy == "name") and "count" or "name"
  196.             displayMonitorInfoParallel(sortItems())
  197.         end
  198.     end
  199. end
  200.  
  201. -- Craft items if their count is below threshold
  202. local function checkAndCraft()
  203.     local items = meBridge.listItems()
  204.     for name, threshold in pairs(thresholds) do
  205.         local item = meBridge.getItem({name = name}) or {}
  206.         local count = item.amount or 0
  207.         if count < threshold then
  208.             local crafting = meBridge.isItemCrafting({name = name}) or false
  209.             if not crafting then
  210.                 print("Crafting " .. name .. "...")
  211.                 local success = meBridge.craftItem({name = name, count = threshold - count})
  212.                 print(success and "Crafted successfully" or "Crafting failed")
  213.             end
  214.         end
  215.     end
  216. end
  217.  
  218. -- Handle user input for threshold management
  219. local function promptThreshold()
  220.     editing = true
  221.     local item = turtle.getItemDetail(1)
  222.     if item then
  223.         local name = item.name
  224.         print("Set threshold for " .. name .. ":")
  225.         local threshold = tonumber(read())
  226.         if threshold then
  227.             setThreshold(name, threshold)
  228.             print("Threshold for " .. name .. " set to " .. threshold)
  229.         else
  230.             print("Invalid input")
  231.         end
  232.     else
  233.         print("Place item in slot 1")
  234.     end
  235.     editing = false
  236. end
  237.  
  238. -- Main loop
  239. local function main()
  240.     thresholds = loadThresholds()  -- Load thresholds once at the start
  241.     local items = sortItems()  -- Sort items based on the default criterion
  242.  
  243.     parallel.waitForAny(
  244.         function()  -- Handle keypresses for manual editing
  245.             while true do
  246.                 local event, key = os.pullEvent("key")
  247.                 if key == keys.space then
  248.                     editThresholdsFile()
  249.                 elseif key == keys.enter then
  250.                     promptThreshold()
  251.                 end
  252.                 sleep(0.1)
  253.             end
  254.         end,
  255.         function()  -- Monitor touch for interaction
  256.             monitorTouchHandler(items)
  257.         end,
  258.         function()  -- Run crafting checks and monitor updates
  259.             while true do
  260.                 checkAndCraft()
  261.                 items = sortItems()
  262.                 displayMonitorInfoParallel(items)
  263.                 sleep(1)  -- Refresh every second (adjust as needed)
  264.             end
  265.         end
  266.     )
  267. end
  268.  
  269. -- Run the main loop
  270. main()
  271.  
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement