nicogb

Untitled

Dec 17th, 2023
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. -- Inventory Sorter
  2. --[[
  3. Author: Tailsworth & Shadowwulf
  4. With credits to the author of RSWarehouse.lua
  5. // I tried to mark areas that were directly copied and/or modified
  6. // from the original RSWarehouse.lua
  7. 20230828
  8.  
  9. TODO: Add monitor_touch handling event to handle sorting columns
  10. instead of text menu.
  11. TODO: Other TODOs below
  12. --]]
  13.  
  14. -- Initialize Monitor // Taken directly from RSWarehouse.lua
  15. -- A future update may allow for multiple monitors. This would allow one
  16. -- monitor to be used for logging and another to be used for work requests.
  17. local monitor = peripheral.find("monitor")
  18. if not monitor then
  19. print("Monitor not found.")
  20. return
  21. end
  22. monitor.setTextScale(1)
  23. monitor.clear()
  24. monitor.setCursorPos(1, 1)
  25. monitor.setCursorBlink(false)
  26. print("Monitor initialized.")
  27.  
  28. -- Initialize RS Bridge
  29. local bridge = peripheral.find("meBridge")
  30. if not bridge then error("ME Bridge not found.") end
  31. print("RS Bridge initialized.")
  32.  
  33. sortName = true
  34. sortAscending = true
  35.  
  36. -- Taken directly from RSWarehouse.lua
  37. -- Prints strings left, centered, or right justified at a specific row and
  38. -- specific foreground/background color.
  39. function mPrintRowJustified(mon, y, pos, text, ...)
  40. w, h = mon.getSize()
  41. fg = mon.getTextColor()
  42. bg = mon.getBackgroundColor()
  43.  
  44. if pos == "left" then x = 1 end
  45. if pos == "center" then x = math.floor((w - #text) / 2) end
  46. if pos == "right" then x = w - #text end
  47.  
  48. if #arg > 0 then mon.setTextColor(arg[1]) end
  49. if #arg > 1 then mon.setBackgroundColor(arg[2]) end
  50. mon.setCursorPos(x, y)
  51. mon.write(text)
  52. mon.setTextColor(fg)
  53. mon.setBackgroundColor(bg)
  54. end
  55. -- END Section copied directly from RSWarehouse.lua
  56.  
  57. -- Utility function that displays the heading "Item Name" with the time centered
  58. -- and "Item Qty" on the right.
  59. -- Now shows which field is sorted and ascending (Az)/(0-9) or
  60. -- descending (zA)/(9-0)
  61. function displayHeader(mon)
  62. now = os.time()
  63.  
  64. cycle = "Day"
  65. cycle_color = colors.orange
  66. if now >= 4 and now < 6 then
  67. cycle = "Sunrise"
  68. cycle_color = colors.orange
  69. elseif now >= 6 and now < 18 then
  70. cycle = "Day"
  71. cycle_color = colors.yellow
  72. elseif now >= 18 and now < 19.5 then
  73. cycle = "Sunset"
  74. cycle_color = colors.orange
  75. elseif now >= 19.5 or now < 5 then
  76. cycle = "Night"
  77. cycle_color = colors.red
  78. end
  79.  
  80. sortedByName = "Item Name"
  81. sortedByQty = "Item Qty"
  82. timeDisplay = string.format("%s %s", cycle, textutils.formatTime(now, false))
  83. if(sortName) then
  84. sortedByName = "[" .. sortedByName .. "]"
  85. if(sortAscending) then
  86. sortedByName = sortedByName .. "(Az)"
  87. else
  88. sortedByName = sortedByName .. "(zA)"
  89. end
  90. else
  91. sortedByQty = "[" .. sortedByQty .. "]"
  92. if(sortAscending) then
  93. sortedByQty = sortedByQty .. "(0-9)"
  94. else
  95. sortedByQty = sortedByQty .. "(9-0)"
  96. end
  97. end
  98. --timeDisplay = timeDisplay .. string.rep(" ", #sortedByQty + 4)
  99.  
  100.  
  101. mPrintRowJustified(mon, 1, "center", timeDisplay, cycle_color)
  102. mPrintRowJustified(mon, 1, "left", sortedByName, colors.blue)
  103. mPrintRowJustified(mon, 1, "right", sortedByQty, colors.blue)
  104. end
  105.  
  106. -- Code Modified from: RSWarehouse.lua
  107. -- This function should show all items that are stored in the RS system
  108. -- Right now it only shows items that do NOT have NBT data but it could
  109. -- be modified to use the NBT data in some way
  110. -- TODO: Make a recursive version of this function that won't scroll the output
  111. -- off of the screen.
  112. -- TODO: Output to the monitor
  113. function getInventory(rs)
  114. -- Get a list (table) of all items in the RS system
  115. items = rs.listItems()
  116.  
  117. -- Sort the table (OLD table)
  118. -- table.sort(items, function (a, b) return a.name < b.name end)
  119. -- Create a new list (table) that has non-NBT items only
  120. -- (this could be updated to use the NBT data somehow?)
  121. nonNBTitems = {}
  122. for index, item in ipairs(items) do
  123. if not item.nbt then
  124. item.name = string.sub(item.name, string.find(item.name, ":") + 1)
  125. table.insert(nonNBTitems, item)
  126. end
  127. end
  128.  
  129. -- Sort the table (NEW table)
  130. if(sortName) then
  131. if(sortAscending) then
  132. table.sort(nonNBTitems, function(a, b) return a.name < b.name end)
  133. else
  134. table.sort(nonNBTitems, function(a, b) return a.name > b.name end)
  135. end
  136. else
  137. if(sortAscending) then
  138. table.sort(nonNBTitems, function(a, b) return a.amount < b.amount end)
  139. else
  140. table.sort(nonNBTitems, function(a, b) return a.amount > b.amount end)
  141. end
  142. end
  143.  
  144. ---return item_array
  145. return nonNBTitems
  146. end
  147.  
  148. -- Output the inventory to the monitor
  149. function monitorInventory(mon, tbl)
  150. curColor = colors.green
  151. mon.clear()
  152. displayHeader(mon)
  153. line = 2
  154.  
  155. w, h = mon.getSize()
  156. for index, item in pairs(tbl) do
  157. if item.amount > 128 then
  158. curColor = colors.green
  159. elseif
  160. item.amount <= 128 and item.amount >= 65 then
  161. curColor = colors.yellow
  162. else
  163. curColor = colors.red
  164. end
  165. if line > h then
  166. sleep(5)
  167. mon.clear()
  168.  
  169. displayHeader(mon)
  170. line = 2
  171. end
  172. mPrintRowJustified(mon, line, "left", item.name, curColor)
  173. mPrintRowJustified(mon, line, "right", tostring(item.amount), curColor)
  174. line = line + 1
  175. end
  176. -- TODO: Make sure the "End of List" doesn't try to print off the screen... instead
  177. -- if the list is at the bottom of the monitor, print the "End of List" on the next empty page
  178. -- before sleeping/restarting
  179. print(tostring(line) .. " " .. tostring(h))
  180. mPrintRowJustified(mon, line, "center", "End of list", colors.orange)
  181. sleep(10)
  182. end
  183.  
  184. -- The MAIN function
  185. function runMain()
  186. while true do
  187. monitorInventory(monitor, getInventory(bridge))
  188. end
  189. end
  190.  
  191. print("Choose your sorting!")
  192. print("1) Sort by iten name")
  193. print("2) Sort by item quantity")
  194. userInput = read()
  195. if(tonumber(userInput) == 2) then
  196. sortName = false
  197. end
  198. print(" ")
  199. print("Choose your order!")
  200. print("A) Sort in ascending order")
  201. print("D) Sort in descending order")
  202. userInput = read()
  203. if(string.upper(userInput) == "D") then
  204. sortAscending = false
  205. end
  206.  
  207. runMain()
Advertisement
Add Comment
Please, Sign In to add comment