Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- maxItems = 15 --number of items to show
- -- define an array of colors
- local colors = {1, 32}
- -- wrap around the monitor
- local side = "top" ---monitor
- local inv = peripheral.wrap("right")
- local wrap = peripheral.wrap(side)
- local currCol = 1
- if wrap == nil then
- print("No peripherals on side: " .. side)
- return
- end
- -- set the background color
- local bgColor = 32768
- wrap.setBackgroundColor(bgColor)
- -- scan the inventory using inventory.list()
- tInventory = inv.list()
- if tInventory == nil then
- print("Unable to access inventory")
- return
- end
- -- create a table to store the item counts
- local tCounts = {}
- for i,v in pairs(tInventory) do
- -- extract the item name from v.name
- local sName = string.match(v.name, ": *(.*)")
- if sName == nil then
- sName = v.name
- end
- -- replace any _ characters with spaces
- sName = string.gsub(sName, "_", " ")
- if tCounts[sName] == nil then
- tCounts[sName] = v.count
- else
- tCounts[sName] = tCounts[sName] + v.count
- end
- end
- -- sort the counts in descending order
- local tSorted = {}
- for k,v in pairs(tCounts) do
- table.insert(tSorted, {name=k, count=v})
- end
- table.sort(tSorted, function(a,b) return a.count > b.count end)
- -- display the top 10 items on the monitor
- wrap.setTextScale(0.5) -- set the text scale to 1
- wrap.clear() -- clear the monitor
- wrap.setCursorPos(1,1) -- set the cursor position to (1,1)
- wrap.setTextColor(2048)
- wrap.write("Top ".. maxItems.." items in storage:\n")
- for i=1,maxItems do
- if tSorted[i] ~= nil then
- wrap.setCursorPos(1,i+2) -- set the cursor position to (1,i+1)
- if currCol == 2 then
- currCol = 1
- elseif currCol == 1 then
- currCol = 2
- end
- wrap.setTextColor(colors[currCol]) -- set the text color
- wrap.write(tSorted[i].name .. ": " .. tSorted[i].count)
- end
- end
- local event, side, x, y = os.pullEvent("monitor_touch")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement