Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Inventory Sorter
- --[[
- Author: Tailsworth & Shadowwulf
- With credits to the author of RSWarehouse.lua
- // I tried to mark areas that were directly copied and/or modified
- // from the original RSWarehouse.lua
- 20230828
- TODO: Add monitor_touch handling event to handle sorting columns
- instead of text menu.
- TODO: Other TODOs below
- --]]
- -- Initialize Monitor // Taken directly from RSWarehouse.lua
- -- A future update may allow for multiple monitors. This would allow one
- -- monitor to be used for logging and another to be used for work requests.
- local monitor = peripheral.find("monitor")
- if not monitor then
- print("Monitor not found.")
- return
- end
- monitor.setTextScale(1)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setCursorBlink(false)
- print("Monitor initialized.")
- -- Initialize RS Bridge
- local bridge = peripheral.find("meBridge")
- if not bridge then error("ME Bridge not found.") end
- print("RS Bridge initialized.")
- sortName = true
- sortAscending = true
- -- Taken directly from RSWarehouse.lua
- -- Prints strings left, centered, or right justified at a specific row and
- -- specific foreground/background color.
- function mPrintRowJustified(mon, y, pos, text, ...)
- w, h = mon.getSize()
- fg = mon.getTextColor()
- bg = mon.getBackgroundColor()
- if pos == "left" then x = 1 end
- if pos == "center" then x = math.floor((w - #text) / 2) end
- if pos == "right" then x = w - #text end
- if #arg > 0 then mon.setTextColor(arg[1]) end
- if #arg > 1 then mon.setBackgroundColor(arg[2]) end
- mon.setCursorPos(x, y)
- mon.write(text)
- mon.setTextColor(fg)
- mon.setBackgroundColor(bg)
- end
- -- END Section copied directly from RSWarehouse.lua
- -- Utility function that displays the heading "Item Name" with the time centered
- -- and "Item Qty" on the right.
- -- Now shows which field is sorted and ascending (Az)/(0-9) or
- -- descending (zA)/(9-0)
- function displayHeader(mon)
- now = os.time()
- cycle = "Day"
- cycle_color = colors.orange
- if now >= 4 and now < 6 then
- cycle = "Sunrise"
- cycle_color = colors.orange
- elseif now >= 6 and now < 18 then
- cycle = "Day"
- cycle_color = colors.yellow
- elseif now >= 18 and now < 19.5 then
- cycle = "Sunset"
- cycle_color = colors.orange
- elseif now >= 19.5 or now < 5 then
- cycle = "Night"
- cycle_color = colors.red
- end
- sortedByName = "Item Name"
- sortedByQty = "Item Qty"
- timeDisplay = string.format("%s %s", cycle, textutils.formatTime(now, false))
- if(sortName) then
- sortedByName = "[" .. sortedByName .. "]"
- if(sortAscending) then
- sortedByName = sortedByName .. "(Az)"
- else
- sortedByName = sortedByName .. "(zA)"
- end
- else
- sortedByQty = "[" .. sortedByQty .. "]"
- if(sortAscending) then
- sortedByQty = sortedByQty .. "(0-9)"
- else
- sortedByQty = sortedByQty .. "(9-0)"
- end
- end
- --timeDisplay = timeDisplay .. string.rep(" ", #sortedByQty + 4)
- mPrintRowJustified(mon, 1, "center", timeDisplay, cycle_color)
- mPrintRowJustified(mon, 1, "left", sortedByName, colors.blue)
- mPrintRowJustified(mon, 1, "right", sortedByQty, colors.blue)
- end
- -- Code Modified from: RSWarehouse.lua
- -- This function should show all items that are stored in the RS system
- -- Right now it only shows items that do NOT have NBT data but it could
- -- be modified to use the NBT data in some way
- -- TODO: Make a recursive version of this function that won't scroll the output
- -- off of the screen.
- -- TODO: Output to the monitor
- function getInventory(rs)
- -- Get a list (table) of all items in the RS system
- items = rs.listItems()
- -- Sort the table (OLD table)
- -- table.sort(items, function (a, b) return a.name < b.name end)
- -- Create a new list (table) that has non-NBT items only
- -- (this could be updated to use the NBT data somehow?)
- nonNBTitems = {}
- for index, item in ipairs(items) do
- if not item.nbt then
- item.name = string.sub(item.name, string.find(item.name, ":") + 1)
- table.insert(nonNBTitems, item)
- end
- end
- -- Sort the table (NEW table)
- if(sortName) then
- if(sortAscending) then
- table.sort(nonNBTitems, function(a, b) return a.name < b.name end)
- else
- table.sort(nonNBTitems, function(a, b) return a.name > b.name end)
- end
- else
- if(sortAscending) then
- table.sort(nonNBTitems, function(a, b) return a.amount < b.amount end)
- else
- table.sort(nonNBTitems, function(a, b) return a.amount > b.amount end)
- end
- end
- ---return item_array
- return nonNBTitems
- end
- -- Output the inventory to the monitor
- function monitorInventory(mon, tbl)
- curColor = colors.green
- mon.clear()
- displayHeader(mon)
- line = 2
- w, h = mon.getSize()
- for index, item in pairs(tbl) do
- if item.amount > 128 then
- curColor = colors.green
- elseif
- item.amount <= 128 and item.amount >= 65 then
- curColor = colors.yellow
- else
- curColor = colors.red
- end
- if line > h then
- sleep(5)
- mon.clear()
- displayHeader(mon)
- line = 2
- end
- mPrintRowJustified(mon, line, "left", item.name, curColor)
- mPrintRowJustified(mon, line, "right", tostring(item.amount), curColor)
- line = line + 1
- end
- -- TODO: Make sure the "End of List" doesn't try to print off the screen... instead
- -- if the list is at the bottom of the monitor, print the "End of List" on the next empty page
- -- before sleeping/restarting
- print(tostring(line) .. " " .. tostring(h))
- mPrintRowJustified(mon, line, "center", "End of list", colors.orange)
- sleep(10)
- end
- -- The MAIN function
- function runMain()
- while true do
- monitorInventory(monitor, getInventory(bridge))
- end
- end
- print("Choose your sorting!")
- print("1) Sort by iten name")
- print("2) Sort by item quantity")
- userInput = read()
- if(tonumber(userInput) == 2) then
- sortName = false
- end
- print(" ")
- print("Choose your order!")
- print("A) Sort in ascending order")
- print("D) Sort in descending order")
- userInput = read()
- if(string.upper(userInput) == "D") then
- sortAscending = false
- end
- runMain()
Advertisement
Add Comment
Please, Sign In to add comment