Fix itemstack insertion logic

This commit is contained in:
Gabriel Tofvesson 2024-10-08 19:23:19 +02:00
parent 741ec36d84
commit 956d68b6e3
3 changed files with 52 additions and 25 deletions

View File

@ -70,7 +70,7 @@ local function loadState(fname, node)
return controller
end
local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage)
local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage, runImport)
local state = savedState or {}
state.tab = state.tab or 1
@ -86,10 +86,21 @@ local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage)
state.tab = state.tab - 1
setPage(nil)
end
return true
end
}
local _btnNext = Text:new{
local btnImport = Text:new{
text = "IMPORT",
bgColor = colors.gray,
fgColor = colors.white,
onClick = function(e, x, y, s)
runImport()
return true
end
}
local btnNext = Text:new{
text = " > ",
bgColor = state.tab == pages and colors.black or colors.gray,
fgColor = state.tab == pages and colors.red or colors.white,
@ -98,17 +109,21 @@ local function itemList(groups, wBudget, hBudget, savedState, onClick, setPage)
state.tab = state.tab + 1
setPage(nil)
end
return true
end
}
local btnNext = Padding:new{
left = wBudget - _btnNext:getWidth() - btnPrev:getWidth(),
element = _btnNext
}
local tabLine = List:new{
children = {
btnPrev,
btnNext
Padding:new{
left = math.floor((wBudget - btnPrev:getWidth() - btnNext:getWidth() - btnImport:getWidth())/2),
element = btnImport
},
Padding:new{
left = wBudget - btnPrev:getWidth() - btnImport:getWidth() - btnNext:getWidth(),
element = btnNext
}
},
vertical = false
}
@ -182,11 +197,8 @@ local function updatePageRender(state, pages)
state.currentPage = state.nextPage
state.nextPage = nil
if changingPage then
state.pageState = nil
end
state.monitor.clear()
state._pageRender = pages[state.currentPage](state)
state._pageRender = pages[state.currentPage](state, changingPage)
end
end
@ -212,7 +224,7 @@ local function renderDefault(state, rootElement)
end
local PAGES = {
MAIN = function(state)
MAIN = function(state, newPage)
local found = ItemGroup.collectStacks(state.controller:find(function(stack)
return not stack:isEmpty()
end))
@ -223,7 +235,7 @@ local PAGES = {
found,
state.width,
state.height,
state.pageState,
state.pageState[state.currentPage],
function(element, x, y, source, group)
print("Clicked: "..group:getDisplayName())
return true
@ -232,7 +244,7 @@ local PAGES = {
state.nextPage = page or state.currentPage
end
)
state.pageState = pageState
state.pageState[state.currentPage] = pageState
listResult:setParent(state.monitor)
return function()
@ -240,11 +252,11 @@ local PAGES = {
end
end,
GROUP_DETAIL = function(state)
GROUP_DETAIL = function(state, newPage)
end,
REQUEST = function(state)
REQUEST = function(state, newPage)
end
}

View File

@ -88,8 +88,8 @@ function Storage:find(query)
end
-- Find all stacks eligible to accept the given query
function Storage:findInsertTargets(query)
local result = self:find(function(stack) return stack:isEmpty() or stack:matches(query) end)
function Storage:findInsertTargets(sourceStack)
local result = self:find(function(stack) return sourceStack:canTransfer(stack) end)
-- Insertion should prioritize filling populated stacks
table.sort(result, function(a, b) return a:getcount() > b:getCount() end)
@ -97,13 +97,27 @@ function Storage:findInsertTargets(query)
return result
end
function Storage:findExtractTargets(query)
local result = self:find(query)
function Storage:findExtractTargets(targetStack)
-- Only transfer non-empty stacks
local result = self:find(function(stack) return (not stack:isEmpty()) and stack:canTransfer(targetStack) end)
-- Extraction should prioritize emptying populated stacks
table.sort(result, function(a, b) return a:getcount() < b:getCount() end)
-- Extraction should prioritize emptying populated stacks
table.sort(result, function(a, b) return a:getcount() < b:getCount() end)
return result
return result
end
function Storage:insertStack(stack)
local targets = self:findInsertTargets(stack)
for _,target in ipairs(targets) do
if stack:isEmpty() then
return true
end
stack:transferTo(target)
end
return false
end
return Storage

View File

@ -211,12 +211,13 @@ end
-- Determines if two stacks can be transferred to eachother
-- Empty stacks are always valid transfer nodes
function ItemStack:canTransfer(stack)
return self.name == stack.name and
return self:isEmpty() or stack:isEmpty() or (self.name == stack.name and
self.damage == stack.damage and
self.maxDamage == stack.maxDamage and
self.displayName == stack.displayName and
objEquals(self.enchantments, stack.enchantments)
objEquals(self.enchantments, stack.enchantments))
end
local function queryField(query, field)