From 18073db75353d0a5d7719b27eebdc2fbc84cd4d2 Mon Sep 17 00:00:00 2001
From: osmarks <osmarks@protonmail.com>
Date: Wed, 15 Aug 2018 17:33:37 +0100
Subject: [PATCH] improve IO

---
 IO.lua | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/IO.lua b/IO.lua
index 3a1f40c..ae16889 100644
--- a/IO.lua
+++ b/IO.lua
@@ -14,6 +14,10 @@ w.init()
 
 local chest = peripheral.wrap(conf.chest)
 
+local function get_num_stacks(total_items)
+    return math.ceil(total_items / 64)
+end
+
 while true do
     local stacks_stored = d.map(chest.list(), w.to_wyvern_item)
     local items_stored = w.collate_stacks(stacks_stored)
@@ -31,7 +35,9 @@ while true do
 
     for slot, item in pairs(stacks_stored) do
         local ii = w.get_internal_identifier(item)
-        if not conf.items[ii] or stacks_stored[ii] > (64 + conf.items[ii]) then -- if item is not in want list, send it back to storage
+        local stored = stacks_stored[ii] or 0
+        local wanted = conf.items[ii] or 0
+        if (get_num_stacks(stored) * 64) > wanted then -- if item is not in want list or we have too many, send it back to storage
             local result = w.unwrap(w.query_by_type("storage", {
                 type = "insert",
                 from_inventory = conf.chest,