druidbruce

AEst/stockpile craftingRequester

Apr 25th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. os.loadAPI("stockpile/settingsPersistence")
  2. os.loadAPI("stockpile/itemSearcher")
  3.  
  4. local aeInterface = peripheral.wrap("back")
  5. local STOCK_PERCENTAGE = 0.05
  6. local activeCraftingJobs = {}
  7.  
  8. function requestCrafting(item, number)
  9. aeInterface.requestCrafting(item, number)
  10. end
  11.  
  12. function numItemsToStock(stockedItem, craftableItem)
  13. local numStocked = craftableItem.size
  14. local numWanted = stockedItem.threshold
  15.  
  16. if numStocked >= numWanted then
  17. return 0
  18. else
  19. local targetCount = math.ceil(numWanted * (1+STOCK_PERCENTAGE))
  20.  
  21. return targetCount - numStocked
  22. end
  23. end
  24.  
  25. function monitorStockedItems()
  26. while true do
  27. settingsPersistence.load()
  28. stockedItems = settingsPersistence.getStockedItems()
  29. itemSearcher.refreshItems()
  30.  
  31. for key, stockedItem in pairs(stockedItems) do
  32. local craftableItem = itemSearcher.findCraftableItem(stockedItem.item)
  33.  
  34. if craftableItem ~= nil then
  35. local stockCount = numItemsToStock(stockedItem, craftableItem)
  36.  
  37. if stockCount > 0 and activeCraftingJobs[key] == nil then
  38. log("Requesting " .. stockCount .. " " .. key)
  39.  
  40. activeCraftingJobs[key] = true
  41. aeInterface.requestCrafting(stockedItem.item, stockCount)
  42. end
  43. else
  44. log("ERROR: monitored item couldn't be found: " .. key)
  45. end
  46. end
  47.  
  48. sleep(10)
  49. end
  50. end
  51.  
  52. function monitorFinishedCraftingJobs()
  53. while true do
  54. local e, craftedItem, eventType = os.pullEvent("crafting_state")
  55. local itemKey = settingsPersistence.itemKey(craftedItem.fingerprint)
  56.  
  57. log("Crafting job for " .. itemKey .. " finished")
  58. activeCraftingJobs[itemKey] = nil
  59. end
  60. end
  61.  
  62. function log(msg)
  63. end
Add Comment
Please, Sign In to add comment