milo: lock chest with multiple items

This commit is contained in:
kepler155c
2018-11-13 17:18:55 -05:00
parent 6870378422
commit 16c56aa092
3 changed files with 90 additions and 68 deletions

View File

@@ -40,6 +40,15 @@ if config.remoteDefaults then
config.remoteDefaults = nil config.remoteDefaults = nil
end end
-- TODO: remove - temporary
for _, node in pairs(config.nodes) do
if node.lock and type(node.lock) == 'string' then
node.lock = {
[ node.lock ] = true,
}
end
end
local modem = Peripheral.get('wired_modem') local modem = Peripheral.get('wired_modem')
if not modem or not modem.getNameLocal then if not modem or not modem.getNameLocal then
error('Wired modem is not connected') error('Wired modem is not connected')

View File

@@ -293,19 +293,6 @@ function Storage:export(target, slot, count, item)
return total return total
end end
local function isLockedWith(node, key)
if node.lock then
if type(node.lock) == 'string' then
return node.lock == key
end
for k in pairs(node.lock) do
if k == key then
return true
end
end
end
end
function Storage:import(source, slot, count, item) function Storage:import(source, slot, count, item)
local total = 0 local total = 0
local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':') local key = item.key or table.concat({ item.name, item.damage, item.nbtHash }, ':')
@@ -334,7 +321,7 @@ _G._debug('INS: %s(%d): %s[%d] -> %s',
-- find a chest locked with this item -- find a chest locked with this item
for node in self:onlineAdapters() do for node in self:onlineAdapters() do
if isLockedWith(node, key) then if node.lock and node.lock[key] then
insert(node.adapter) insert(node.adapter)
if count > 0 and node.void then if count > 0 and node.void then
total = total + self:trash(source, slot, count) total = total + self:trash(source, slot, count)

View File

@@ -5,7 +5,7 @@ local colors = _G.colors
local device = _G.device local device = _G.device
local storageView = UI.Window { local storageView = UI.Window {
title = 'Storage Options', title = 'Storage Options - General',
index = 2, index = 2,
backgroundColor = colors.cyan, backgroundColor = colors.cyan,
form = UI.Form { form = UI.Form {
@@ -18,63 +18,24 @@ local storageView = UI.Window {
validate = 'numeric', validate = 'numeric',
shadowText = 'Numeric priority', shadowText = 'Numeric priority',
}, },
[2] = UI.Checkbox { [2] = UI.TextEntry {
formLabel = 'Locked', formKey = 'lockWith',
help = 'Locks chest to current item types',
},
[3] = UI.Text {
x = 16, ex = -2, y = 3,
value = '',
},
[4] = UI.Checkbox {
formLabel = 'Void', formKey = 'void',
help = 'Void items if locked chest is full',
},
[5] = UI.TextEntry {
formLabel = 'Refresh', formKey = 'refreshInterval', formLabel = 'Refresh', formKey = 'refreshInterval',
shadowText = 'seconds between refresh', shadowText = 'seconds between refresh',
limit = 4, limit = 4,
validate = 'numeric', validate = 'numeric',
help = 'Refresh periodically', help = 'Refresh periodically',
}, },
[6] = UI.TextArea { [3] = UI.TextArea {
x = 12, ex = -2, y = 6, x = 12, ex = -2, y = 4,
textColor = colors.yellow, textColor = colors.yellow,
value = 'Only specify if you are manually taking items out of this inventory. Value should be > 10', value = 'Only specify if you are manually taking items out of this inventory. Value should be > 10',
}, },
--[[
[4] = UI.Checkbox {
formLabel = 'Void', formKey = 'voidExcess',
help = 'Void excess if locked - TODO',
pruneEmpty = true,
},
[5] = UI.Checkbox {
formLabel = 'Partition', formKey = 'partition',
help = 'TODO',
pruneEmpty = true,
},
]]--
}, },
} }
function storageView:showLockTypes()
local types = { }
if self.node.lock then
if type(self.node.lock) == 'string' then
self.form[3].value = self.node.lock
return
end
for name in pairs(self.node.lock) do
table.insert(types, itemDB:getName(name))
end
end
self.form[3].value = table.concat(types, ', ')
end
function storageView:enable() function storageView:enable()
UI.Window.enable(self) UI.Window.enable(self)
self:focusFirst() self:focusFirst()
self:showLockTypes()
end end
function storageView:validate() function storageView:validate()
@@ -95,36 +56,101 @@ function storageView:isValidFor(node)
end end
function storageView:setNode(node) function storageView:setNode(node)
self.form:setValues(node)
end
UI:getPage('nodeWizard').wizard:add({ storageGeneral = storageView })
--[[ Locking Page ]]--
local lockView = UI.Window {
title = 'Storage Options - Locking',
index = 3,
backgroundColor = colors.cyan,
form = UI.Form {
x = 1, y = 1, ex = -1, ey = 3,
manualControls = true,
[1] = UI.Checkbox {
formLabel = 'Locked', formKey = 'lockWith',
help = 'Locks chest to current item types',
},
[2] = UI.Checkbox {
formLabel = 'Void', formKey = 'void',
help = 'Void items if locked chest is full',
},
},
grid = UI.ScrollingGrid {
x = 2, ex = -2, y = 4, ey = -2,
columns = {
{ heading = 'Name', key = 'displayName' },
},
sortColumn = 'displayName',
},
}
function lockView:showLockTypes()
self.grid.values = { }
if self.node.lock then
for key in pairs(self.node.lock) do
table.insert(self.grid.values, {
displayName = itemDB:getName(key),
key = key,
})
end
end
self.grid:update()
self.grid:draw()
end
function lockView:enable()
UI.Window.enable(self)
self:focusFirst()
end
function lockView:validate()
return self.form:save()
end
function lockView:isValidType(node)
local m = device[node.name]
return m and m.pullItems and {
name = 'Storage',
value = 'storage',
help = 'Use for item storage',
}
end
function lockView:isValidFor(node)
return node.mtype == 'storage'
end
function lockView:setNode(node)
self.node = node self.node = node
self.form:setValues(node) self.form:setValues(node)
self:showLockTypes() self:showLockTypes()
end end
function storageView:eventHandler(event) function lockView:eventHandler(event)
if event.type == 'checkbox_change' and event.element.formKey == 'lockWith' then if event.type == 'checkbox_change' and event.element.formKey == 'lockWith' then
if event.checked then if event.checked then
if device[self.node.name] and device[self.node.name].list then if device[self.node.name] and device[self.node.name].list then
local lock = { } local list = device[self.node.name].list()
for _, slot in pairs(device[self.node.name].list()) do if not next(list) then
lock[itemDB:makeKey(slot)] = true
end
if not next(lock) then
self:emit({ self:emit({
type = 'general_error', type = 'general_error',
field = event.element, field = event.element,
message = 'The chest must contain the item(s) to lock' }) message = 'The chest must contain the item(s) to lock' })
self.form[3].value = false
else else
self.node.lock = lock self.node.lock = { }
for _, slot in pairs(list) do
self.node.lock[itemDB:makeKey(slot)] = true
end
end end
end end
else else
self.node.lock = nil self.node.lock = nil
self.form[3].value = ''
end end
self:showLockTypes() self:showLockTypes()
self.form[3]:draw()
end end
end end
UI:getPage('nodeWizard').wizard:add({ storage = storageView }) UI:getPage('nodeWizard').wizard:add({ storageLock = lockView })