milo: lock chest with multiple items
This commit is contained in:
@@ -52,6 +52,7 @@ function itemDB:splitKey(key, item)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function itemDB:get(key)
|
function itemDB:get(key)
|
||||||
|
if not key then error('itemDB:get: key is required', 2) end
|
||||||
if type(key) == 'string' then
|
if type(key) == 'string' then
|
||||||
key = self:splitKey(key)
|
key = self:splitKey(key)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -293,6 +293,19 @@ 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 }, ':')
|
||||||
@@ -320,23 +333,27 @@ _G._debug('INS: %s(%d): %s[%d] -> %s',
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- find a chest locked with this item
|
-- find a chest locked with this item
|
||||||
for remote in self:onlineAdapters() do
|
for node in self:onlineAdapters() do
|
||||||
if remote.lock == key then
|
if isLockedWith(node, key) then
|
||||||
insert(remote.adapter)
|
insert(node.adapter)
|
||||||
if count > 0 then -- TODO: only if void flag set
|
if count > 0 and node.void then
|
||||||
total = total + self:trash(source, slot, count)
|
total = total + self:trash(source, slot, count)
|
||||||
|
return total
|
||||||
end
|
end
|
||||||
|
--return total
|
||||||
|
end
|
||||||
|
if count <= 0 then
|
||||||
return total
|
return total
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- is this item in some chest
|
-- is this item in some chest
|
||||||
if self.cache[key] then
|
if self.cache[key] then
|
||||||
for _, adapter in self:onlineAdapters() do
|
for node, adapter in self:onlineAdapters() do
|
||||||
if count <= 0 then
|
if count <= 0 then
|
||||||
return total
|
return total
|
||||||
end
|
end
|
||||||
if adapter.cache and adapter.cache[key] and not adapter.lock then
|
if adapter.cache and adapter.cache[key] and not node.lock then
|
||||||
insert(adapter)
|
insert(adapter)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -20,21 +20,25 @@ local storageView = UI.Window {
|
|||||||
},
|
},
|
||||||
[2] = UI.Checkbox {
|
[2] = UI.Checkbox {
|
||||||
formLabel = 'Locked', formKey = 'lockWith',
|
formLabel = 'Locked', formKey = 'lockWith',
|
||||||
help = 'Locks chest to a single item type',
|
help = 'Locks chest to current item types',
|
||||||
},
|
},
|
||||||
[3] = UI.Text {
|
[3] = UI.Text {
|
||||||
x = 16, ex = -2, y = 3,
|
x = 16, ex = -2, y = 3,
|
||||||
value = '',
|
value = '',
|
||||||
},
|
},
|
||||||
[4] = UI.TextEntry {
|
[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',
|
||||||
help = 'Refresh periodically',
|
shadowText = 'seconds between refresh',
|
||||||
limit = 4,
|
limit = 4,
|
||||||
validate = 'numeric',
|
validate = 'numeric',
|
||||||
shadowText = 'seconds between refresh',
|
help = 'Refresh periodically',
|
||||||
},
|
},
|
||||||
[5] = UI.TextArea {
|
[6] = UI.TextArea {
|
||||||
x = 12, ex = -2, y = 5,
|
x = 12, ex = -2, y = 6,
|
||||||
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',
|
||||||
},
|
},
|
||||||
@@ -53,9 +57,24 @@ local storageView = UI.Window {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
@@ -76,32 +95,34 @@ function storageView:isValidFor(node)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function storageView:setNode(node)
|
function storageView:setNode(node)
|
||||||
self.machine = node
|
self.node = node
|
||||||
self.form:setValues(node)
|
self.form:setValues(node)
|
||||||
self.form[3].value = node.lock and itemDB:getName(node.lock) or ''
|
self:showLockTypes()
|
||||||
end
|
end
|
||||||
|
|
||||||
function storageView:eventHandler(event)
|
function storageView: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.machine.name] and device[self.machine.name].list then
|
if device[self.node.name] and device[self.node.name].list then
|
||||||
local _, slot = next(device[self.machine.name].list())
|
local lock = { }
|
||||||
if slot then
|
for _, slot in pairs(device[self.node.name].list()) do
|
||||||
self.machine.lock = itemDB:makeKey(slot)
|
lock[itemDB:makeKey(slot)] = true
|
||||||
self.form[3].value = itemDB:getName(slot)
|
end
|
||||||
else
|
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 to lock' })
|
message = 'The chest must contain the item(s) to lock' })
|
||||||
self.form[3].value = false
|
self.form[3].value = false
|
||||||
self.form[3]:draw()
|
else
|
||||||
|
self.node.lock = lock
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
self.machine.lock = nil
|
self.node.lock = nil
|
||||||
self.form[3].value = ''
|
self.form[3].value = ''
|
||||||
end
|
end
|
||||||
|
self:showLockTypes()
|
||||||
self.form[3]:draw()
|
self.form[3]:draw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user