From 656e317b92ce9805bf3a85953d34e9be21bea1db Mon Sep 17 00:00:00 2001 From: osmarks Date: Fri, 27 Jul 2018 12:41:07 +0100 Subject: [PATCH] Reindex & clear on insert --- backend-chests.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backend-chests.lua b/backend-chests.lua index 103fbef..4100078 100644 --- a/backend-chests.lua +++ b/backend-chests.lua @@ -102,11 +102,13 @@ local function search(query, threshold) return d.sort_by(results, function(x) return x.extra end) -- sort returned results by closeness to query end +-- Retrives items from a location in storage and puts them in the buffer local function fetch_by_location(loc, limit) local peripheral_name, slot, limit = loc.inventory, loc.slot, limit or 64 return peripheral.call(conf.buffer_internal, "pullItems", peripheral_name, slot, limit, BUFFER_OUT_SLOT) end +-- Clears out the buffer into storage. local function clear_buffer() for i = 1, peripheral.call(conf.buffer_internal, "size") do local space_location = find_space() @@ -152,10 +154,14 @@ local function server(command) if not inventory_with_space then return w.errors.make(w.errors.NOSPACE) end -- if there's not space, say so in error if command.from_inventory and command.from_slot then - peripheral.call(conf.buffer_external, "pullItems", command.from_inventory, command.from_slot, command.quantity, BUFFER_IN_SLOT) + peripheral.call(conf.buffer_external, "pullItems", command.from_inventory, command.from_slot, command.quantity, BUFFER_IN_SLOT) -- pull from from_inventory to buffer end - local moved = peripheral.call(conf.buffer_internal, "pushItems", inventory_with_space, BUFFER_IN_SLOT) + local moved = peripheral.call(conf.buffer_internal, "pushItems", inventory_with_space, BUFFER_IN_SLOT) -- push from buffer to free space + + if moved == 0 then clear_buffer() end -- if for some reason it broke and there's no space, clear out buffer + os.queueEvent("reindex", inventory_with_space) + return { moved = moved } end end @@ -167,5 +173,6 @@ local function indexer_thread() end end +clear_buffer() w.init() parallel.waitForAll(function() os.queueEvent("reindex") w.serve(server, "storage") end, indexer_thread) \ No newline at end of file