Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Moves items from SRC to DEST as specified by INPUT_FILE, which is a JSON
- -- file in the format obtained by using the copy button of St'ructure Tools'
- -- Copy Paste Gadget
- local SRC = "left"
- local DEST = "right"
- local INPUT_FILE = "input.json"
- local src = peripheral.wrap(SRC)
- local dest = peripheral.wrap(DEST)
- local function clear_dest()
- for slot, item in pairs(dest.list()) do
- local pushCount = dest.pushItems(SRC, slot)
- if pushCount < item.count then
- print("Not enough room in SRC to clear DEST into")
- error()
- end
- end
- end
- local needed = {}
- do
- local success, input = pcall(function()
- io.input(INPUT_FILE)
- return textutils.unserializeJSON(io.read("*a")).header.material_list.root_entry
- end)
- if not success then
- print("Error deserializing JSON")
- error()
- end
- for _, v in pairs(input) do
- needed[v.item.item] = v.count
- end
- end
- clear_dest()
- local srcMats = {}
- do
- for slot, item in pairs(src.list()) do
- srcMats[item.name] = srcMats[item.name] or {slots = {}, total = 0}
- local t = srcMats[item.name]
- t.slots[#t.slots + 1] = slot
- t.total = t.total + item.count
- end
- end
- local errored = false
- -- Move mats to dest
- for item, neededCount in pairs(needed) do
- local stillNeeded = neededCount
- local t = srcMats[item] or {slots = {}, total = 0}
- for _, slot in ipairs(t.slots) do
- stillNeeded = stillNeeded - dest.pullItems(SRC, slot, neededCount)
- if stillNeeded <= 0 then
- break
- end
- end
- if stillNeeded > 0 then
- print("Missing " .. stillNeeded .. "/" .. neededCount .. " " .. item)
- errored = true
- end
- end
- if errored then
- -- Detect the possibility that dest is not big enough
- local filled_dest_slots = 0
- for _ in pairs(dest.list()) do
- filled_dest_slots = filled_dest_slots + 1
- end
- if filled_dest_slots >= dest.size() then
- print("Warning: previous errors may be due to destination storage not being big enough")
- end
- clear_dest()
- error()
- end
- print("Success")
Add Comment
Please, Sign In to add comment