Compare commits
No commits in common. "349ce2bc3494b320f27b26e06e40e8c9e77b3029" and "1482437161dabf1c72e81c7b7ae68da3bdc8f695" have entirely different histories.
349ce2bc34
...
1482437161
@ -1,5 +1,4 @@
|
|||||||
local Chest = require("storage.chest")
|
local Chest = require("storage.chest")
|
||||||
local ItemGroup = require("storage.itemgroup")
|
|
||||||
local Storage = require("storage")
|
local Storage = require("storage")
|
||||||
local Text = require("gfx.text")
|
local Text = require("gfx.text")
|
||||||
local List = require("gfx.list")
|
local List = require("gfx.list")
|
||||||
@ -78,17 +77,17 @@ local monitor = peripheral.find("monitor")
|
|||||||
local width, height = monitor.getSize()
|
local width, height = monitor.getSize()
|
||||||
|
|
||||||
|
|
||||||
local function itemList(groups, wBudget)
|
local function itemList(stacks, wBudget)
|
||||||
local bgColors = {
|
local bgColors = {
|
||||||
colors.gray,
|
colors.gray,
|
||||||
colors.black
|
colors.black
|
||||||
}
|
}
|
||||||
local entries = {}
|
local entries = {}
|
||||||
|
|
||||||
for i=1,#groups do
|
for i=1,#stacks do
|
||||||
local group = groups[i]
|
local stack = stacks[i]
|
||||||
local text = group:getDisplayName()
|
local text = stack:getDisplayName()
|
||||||
local count = tostring(group:getItemCount())
|
local count = tostring(stack:getCount())
|
||||||
|
|
||||||
-- Fit text inside of width budget
|
-- Fit text inside of width budget
|
||||||
local countLen = #count
|
local countLen = #count
|
||||||
@ -120,7 +119,7 @@ local function itemList(groups, wBudget)
|
|||||||
},
|
},
|
||||||
vertical = false,
|
vertical = false,
|
||||||
onClick = function(table, x, y, source)
|
onClick = function(table, x, y, source)
|
||||||
print("Clicked: "..group:getDisplayName())
|
print("Clicked: "..stack:getDisplayName())
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
table.insert(entries, list)
|
table.insert(entries, list)
|
||||||
@ -132,18 +131,16 @@ local function itemList(groups, wBudget)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local found = ItemGroup.collectStacks(controller:find(function(stack)
|
local count = 0
|
||||||
return not stack:isEmpty()
|
local found = controller:find(function(stack)
|
||||||
end))
|
local match = not stack:isEmpty()
|
||||||
|
if match then
|
||||||
table.sort(found, function(a, b) return a:getItemCount() > b:getItemCount() end)
|
count = count + 1
|
||||||
|
|
||||||
local subset = {}
|
|
||||||
for i=1,math.min(13, #found) do
|
|
||||||
table.insert(subset, found[i])
|
|
||||||
end
|
end
|
||||||
|
return match and count <= 10
|
||||||
|
end)
|
||||||
|
|
||||||
local listResult = itemList(subset, width)
|
local listResult = itemList(found, width)
|
||||||
|
|
||||||
listResult:setParent(monitor)
|
listResult:setParent(monitor)
|
||||||
listResult:draw()
|
listResult:draw()
|
@ -1,151 +0,0 @@
|
|||||||
local ItemGroup = {}
|
|
||||||
ItemGroup.__index = ItemGroup
|
|
||||||
|
|
||||||
--- Create a group of managed itemstacks
|
|
||||||
---@param stack table ItemStack to compare to
|
|
||||||
---@return table ItemGroup instance representing stack type
|
|
||||||
function ItemGroup:from(stack)
|
|
||||||
local obj = { stack }
|
|
||||||
setmetatable(obj, self)
|
|
||||||
obj.__index = obj
|
|
||||||
return obj
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup.collectStacks(stacks)
|
|
||||||
local groups = {}
|
|
||||||
for _,stack in ipairs(stacks) do
|
|
||||||
for _,group in ipairs(groups) do
|
|
||||||
if group:addStack(stack) then
|
|
||||||
goto continue
|
|
||||||
end
|
|
||||||
end
|
|
||||||
table.insert(groups, ItemGroup:from(stack))
|
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
return groups
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:getStackCount()
|
|
||||||
return #self
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:getDisplayName()
|
|
||||||
return self:_getIdentityStack():getDisplayName()
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:getName()
|
|
||||||
return self:_getIdentityStack():getName()
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:getEnchantments()
|
|
||||||
return self:_getIdentityStack():getEnchantments()
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:_getIdentityStack()
|
|
||||||
return self[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:_iterateStacks()
|
|
||||||
return ipairs(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:_addStack(stack)
|
|
||||||
table.insert(self, stack)
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:_removeStack(stackOrIndex)
|
|
||||||
if type(stackOrIndex) == "number" then
|
|
||||||
return table.remove(self, stackOrIndex) ~= nil
|
|
||||||
else
|
|
||||||
for index,stack in self:_iterateStacks() do
|
|
||||||
if stack == stackOrIndex then
|
|
||||||
return self:_removeStack(index)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:canAddStack(stack)
|
|
||||||
if self:_getIdentityStack():canTransfer(stack) then
|
|
||||||
for _,chkStack in self:_iterateStacks() do
|
|
||||||
if chkStack == stack then
|
|
||||||
-- Already in the group
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self:_addStack(stack)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:addStack(stack)
|
|
||||||
if self:canAddStack(stack) then
|
|
||||||
self:_addStack(stack)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:getItemCount()
|
|
||||||
if self:_getIdentityStack():isEmpty() then
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
local sum = 0
|
|
||||||
for _,stack in self:_iterateStacks() do
|
|
||||||
sum = sum + stack:getCount()
|
|
||||||
end
|
|
||||||
return sum
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:_copyTrackedStackList()
|
|
||||||
local collect = {}
|
|
||||||
for index,stack in self:_iterateStacks() do
|
|
||||||
table.insert(collect, { stack = stack, index = index })
|
|
||||||
end
|
|
||||||
return collect
|
|
||||||
end
|
|
||||||
|
|
||||||
function ItemGroup:defragment()
|
|
||||||
if self:_getIdentityStack():isEmpty() then
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
|
|
||||||
local entries = self:_copyStackList()
|
|
||||||
table.sort(entries, function(a, b)
|
|
||||||
return a.stack:getCount() > b.stack:getCount()
|
|
||||||
end)
|
|
||||||
local endPtr = #entries
|
|
||||||
local startPtr = endPtr
|
|
||||||
for i=1,endPtr do
|
|
||||||
local entry = entries[i].stack
|
|
||||||
if entry:getMaxCount() ~= entry:getCount() then
|
|
||||||
startPtr = i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local toRemove = {}
|
|
||||||
while startPtr < endPtr do
|
|
||||||
local from = entries[endPtr]
|
|
||||||
local to = entries[startPtr]
|
|
||||||
|
|
||||||
local emptied, _ = from.stack:transferTo(to.stack)
|
|
||||||
if emptied then
|
|
||||||
table.insert(toRemove, from.index)
|
|
||||||
endPtr = endPtr - 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if (not emptied) or (to.stack:getCount() == to.stack:getMaxCount()) then
|
|
||||||
startPtr = startPtr + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
table.sort(toRemove, function(a, b) return a > b end)
|
|
||||||
for _,index in ipairs(toRemove) do
|
|
||||||
self:_removeStack(index)
|
|
||||||
end
|
|
||||||
|
|
||||||
return #toRemove
|
|
||||||
end
|
|
||||||
|
|
||||||
return ItemGroup
|
|
@ -154,14 +154,14 @@ function ItemStack:_modify(countDelta, stack)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function ItemStack:transferTo(target, count)
|
function ItemStack:transferTo(target, count)
|
||||||
local cap = math.min(count or self:getCount(), target:getMaxCount() - target:getCount(), self:getCount())
|
local cap = math.min(count, target.maxCount - target.getCount(), self:getCount())
|
||||||
|
|
||||||
-- If we can't transfer any data, then
|
-- If we can't transfer any data, then
|
||||||
if cap == 0 then
|
if cap == 0 then
|
||||||
return count == 0, 0
|
return count == 0, 0
|
||||||
end
|
end
|
||||||
|
|
||||||
local result, xfer = pcall(self:getInventory().pushItems, peripheral.getName(target:getInventory()), self:getSlot(), cap, target:getSlot())
|
local result, xfer = pcall(self.inv.pushItems, peripheral.getName(target.inv), self.slot, cap, target.slot)
|
||||||
|
|
||||||
if not result then
|
if not result then
|
||||||
return false, xfer
|
return false, xfer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user