Add IO component (like a ME interface)

This commit is contained in:
osmarks 2018-08-14 22:13:12 +01:00
parent f5b4589a78
commit 1bfec07316
2 changed files with 45 additions and 0 deletions

38
IO.lua Normal file
View File

@ -0,0 +1,38 @@
local d = require "luadash"
local w = require "lib"
local conf = w.load_config({
"chest",
"items"
}, {
items = {}
})
w.init()
local chest = peripheral.wrap(conf.chest)
while true do
local stacks_stored = chest.list()
local items_stored = w.collate_stacks(stacks_stored)
for item_name, quantity_desired in pairs(conf.items) do
local quantity_stocked = items_stored[item_name] or 0
if quantity_desired > quantity_stocked then -- if we have fewer items than are desired, extract some from store
local request = w.string_to_item(item_name)
request.type = "extract"
request.destination_inventory = w.chest
w.query_by_type("storage", request)
end
end
for slot, item in pairs(stacks_stored) do
if not items[get_internal_identifier(item)] then -- if item is not in want list, send it back to storage
w.query_by_type("storage", {
type = "insert",
from_inventory = conf.chest,
from_slot = slot
})
end
end
end

View File

@ -177,6 +177,13 @@ local function get_internal_identifier(item)
return n
end
-- Inverse of get_internal_identifier - parses that kind of string into ID/meta/NBT
local function string_to_item(s)
local mod, item, meta, NBT = string.match(s, "(%a+):(%a+):([0-9]+)#([0-9a-f]+)")
if not mod or not item or not meta then error(w.errors.make(w.errors.INTERNAL, "string did not match regex")) end
return { ID = mod .. ":" .. item, meta = meta, NBT = NBT }
end
-- GENERAL STUFF
-- Converts a table of the form {"x", "x", "y"} into {x = 2, y = 1}