diff --git a/inferium-harvester.lua b/inferium-harvester.lua index 776340a..6dfe0a4 100644 --- a/inferium-harvester.lua +++ b/inferium-harvester.lua @@ -79,18 +79,19 @@ function isSeed(item) end -- Inventory utils --- the slot of the item is returned -local function waitForItem(inventory, itemName, count, sleepTime) - sleepTime = sleepTime or 5 -- TODO: DEFAULT_IDLE_TIME - - while true do - for slot, item in pairs(inventory.list()) do - if item.name == itemName and item.count >= count then - return slot - end +local function getItemSlot(inventory, itemName) + for slot, item in pairs(inventory.list()) do + if item and item.name == itemName then + return slot end + end +end - os.sleep(IDLE_TIME) +local function getCountItem(inventory, itemName) + for _, item in pairs(inventory.list()) do + if item and item.name == itemName then + return item.count + end end end @@ -338,17 +339,22 @@ local function retrieveSeeds(seeds) local bufferInventory = turtleUtils.waitForInventory('front', WAIT_ITEM_IDLE_TIME) for seedName, count in pairs(seedsCount) do - local slot = waitForItem(storageInventory, seedName, count, WAIT_ITEM_IDLE_TIME) - local pushOk = storageInventory.pushItems(peripheral.getName(bufferInventory), slot, count) + local slot = getItemSlot(storageInventory, seedName) - if not pushOk then - error('retrieveSeeds error: cannot pushItems from storage to buffer') - end + if slot then + local inventoryCount = getCountItem(storageInventory, seedName) + local realCount = math.min(count, inventoryCount) + local pushOk = storageInventory.pushItems(peripheral.getName(bufferInventory), slot, realCount) - local suckOk = turtle.suck() + if not pushOk then + error('retrieveSeeds error: cannot pushItems from storage to buffer') + end - if not suckOk then - error('retrieveSeeds error: cannot suck items from buffer') + local suckOk = turtle.suck() + + if not suckOk then + error('retrieveSeeds error: cannot suck items from buffer') + end end end end