diff --git a/itemcontroller.lua b/itemcontroller.lua index dab36cb..5fe9477 100644 --- a/itemcontroller.lua +++ b/itemcontroller.lua @@ -1,4 +1,5 @@ local Chest = require("storage.chest") +local ItemGroup = require("storage.itemgroup") local Storage = require("storage") local Text = require("gfx.text") local List = require("gfx.list") @@ -77,17 +78,17 @@ local monitor = peripheral.find("monitor") local width, height = monitor.getSize() -local function itemList(stacks, wBudget) +local function itemList(groups, wBudget) local bgColors = { colors.gray, colors.black } local entries = {} - for i=1,#stacks do - local stack = stacks[i] - local text = stack:getDisplayName() - local count = tostring(stack:getCount()) + for i=1,#groups do + local group = groups[i] + local text = group:getDisplayName() + local count = tostring(group:getItemCount()) -- Fit text inside of width budget local countLen = #count @@ -119,28 +120,30 @@ local function itemList(stacks, wBudget) }, vertical = false, onClick = function(table, x, y, source) - print("Clicked: "..stack:getDisplayName()) + print("Clicked: "..group:getDisplayName()) end } table.insert(entries, list) end - + return List:new{ children = entries, vertical = true } end -local count = 0 -local found = controller:find(function(stack) - local match = not stack:isEmpty() - if match then - count = count + 1 - end - return match and count <= 10 -end) +local found = ItemGroup.collectStacks(controller:find(function(stack) + return not stack:isEmpty() +end)) -local listResult = itemList(found, width) +table.sort(found, function(a, b) return a:getItemCount() > b:getItemCount() end) + +local subset = {} +for i=1,math.min(13, #found) do + table.insert(subset, found[i]) +end + +local listResult = itemList(subset, width) listResult:setParent(monitor) listResult:draw() \ No newline at end of file