druidbruce

infusion_altar_auto/startup

Jun 13th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. crafting = false
  2. altar = peripheral.wrap("container_pedestal_0")
  3. currItem = nil
  4.  
  5. essentiaRequester = peripheral.wrap("tileinterface_2")
  6. infusionRequester = peripheral.wrap("tileinterface_0")
  7. pedestalRequester = peripheral.wrap("tileinterface_1")
  8.  
  9. redstone.setAnalogOutput("bottom",0)
  10.  
  11. function isAltarFull()
  12. return altar.getStackInSlot(1) ~= nil
  13. end
  14.  
  15. function altarItem()
  16. return altar.getStackInSlot(1)["display_name"]
  17. end
  18.  
  19. function enableInfusion()
  20. redstone.setAnalogOutput("bottom",15)
  21. end
  22.  
  23. function disableInfusion()
  24. redstone.setAnalogOutput("bottom", 0)
  25. end
  26.  
  27. function removeFromAltar()
  28. altar.pushItem("down",1)
  29. end
  30.  
  31. os.loadAPI("json")
  32.  
  33. function loadRecipes()
  34. recipes = {}
  35. for i,path in ipairs(fs.list("recipes")) do
  36. f = fs.open("recipes/"..path, "r")
  37. j = f.readAll()
  38. f.close()
  39. recipe = json.decode(j)
  40. table.insert(recipes,recipe)
  41. end
  42. return recipes
  43. end
  44.  
  45. craftsWaiting = {}
  46. craftingJobs = {}
  47.  
  48. function dump(o)
  49. print("Dumping ")
  50. if type(o) == 'table' then
  51. local s = '{ '
  52. for k,v in pairs(o) do
  53. if type(k) ~= 'number' then k = '"'..k..'"' end
  54. s = s .. '['..k..'] = ' .. dump(v) .. ','
  55. end
  56. return s .. '} '
  57. else
  58. return tostring(o)
  59. end
  60. end
  61.  
  62. essentiaChest = peripheral.wrap("ender_chest_3")
  63. pedestalChest = peripheral.wrap("ender_chest_2")
  64. infusionItemChest = peripheral.wrap("ender_chest_1")
  65.  
  66.  
  67. function revert()
  68. clearTo(essentiaChest, "up")
  69. clearTo(pedestalChest, "up")
  70. clearTo(infusionItemChest, "up")
  71. end
  72.  
  73. function requestItems(requester, itemArr, requestCrafts)
  74. complete = true
  75. jobs = {}
  76. for i,item in ipairs(itemArr) do
  77. success, exported = pcall(requester.exportItem, item, "down", item.qty)
  78. if not success or exported == nil then
  79. exported = {size = 0}
  80. end
  81. if exported.size < item.qty then
  82. if requestCrafts then
  83. amount = item.qty - exported.size
  84. print("Requesting crafting of "..amount.." "..item.display_name)
  85. requester.requestCrafting(item, amount)
  86. jobObj = {id = jobId(amount, item.id, item.dmg, item.nbtHash), done = false}
  87. table.insert(jobs, jobObj)
  88. table.insert(craftingJobs, jobObj)
  89. end
  90. complete = false
  91. end
  92. end
  93. return complete, jobs
  94. end
  95.  
  96. function clearTo(chest, dir)
  97. for slot,item in ipairs(chest.getAllStacks()) do
  98. chest.pushItem(dir, slot)
  99. end
  100. end
  101.  
  102. function tryToCraft(chest, recipe, requestCrafts)
  103. essentia, eJobs = requestItems(essentiaRequester, recipe.essentiaItems, requestCrafts)
  104. infusion, iJobs = requestItems(infusionRequester, recipe.infusionItems, requestCrafts)
  105. pedestal, pJobs = requestItems(pedestalRequester, recipe.pedestalItems, requestCrafts)
  106. fullRecipe = essentia and infusion and pedestal
  107. if not fullRecipe then
  108. revert()
  109. if requestCrafts then
  110. print("Adding recipe which infuses "..recipe.infusionItems[1].display_name.." to queue")
  111. table.insert(craftsWaiting, recipe)
  112. recipe.essentiaJobs = eJobs
  113. recipe.infusionJobs = iJobs
  114. recipe.pedestalJobs = pJobs
  115. end
  116. return false
  117. else
  118. return true
  119. end
  120. end
  121.  
  122. function jobsDone(jobs)
  123. for i,job in ipairs(jobs) do
  124. if not job.done then
  125. return false
  126. end
  127. end
  128. return true
  129. end
  130.  
  131. function isDoneCrafting(recipe)
  132. return jobsDone(recipe.essentiaJobs) and jobsDone(recipe.infusionJobs) and jobsDone(recipe.pedestalJobs)
  133. end
  134.  
  135.  
  136. function requestRecipeByTrigger(chest, recipe)
  137. stacks = chest.getAllStacks()
  138. for slot,item in ipairs(stacks) do
  139. if item.all().display_name == recipe.triggerItems[1].display_name then
  140. print("Found trigger "..item.all().display_name)
  141. crafted = tryToCraft(chest, recipe, true)
  142. chest.pushItem("north", slot)
  143. return crafted
  144. end
  145. end
  146. end
  147.  
  148.  
  149. function requestQueuedRecipe(chest, recipe, recipeSlot)
  150. if isDoneCrafting(recipe) then
  151. table.remove(craftsWaiting, recipeSlot)
  152. crafted = tryToCraft(chest, recipe, true)
  153. return crafted
  154. end
  155. end
  156.  
  157. inputChest = peripheral.wrap("ender_chest_4")
  158.  
  159. function jobId(qty,id,dmg,nbt)
  160. if nbt == nil then
  161. nbt = "nil"
  162. end
  163. return qty..":"..id..":"..dmg..":"..nbt
  164. end
  165.  
  166. function listenForCrafts()
  167. while true do
  168. key, stack, state = os.pullEvent("crafting_state")
  169. jobid = jobId(stack.size, stack.fingerprint.id, stack.fingerprint.dmg, stack.fingerprint.nbtHash)
  170. for i,job in ipairs(craftingJobs) do
  171. if job.id == jobid then
  172. table.remove(craftingJobs, i)
  173. sleep(3)
  174. job.done = true
  175. print("Job "..job.id.." is done")
  176. end
  177. end
  178. end
  179. end
  180.  
  181. function craftItems()
  182. revert()
  183. while true do
  184.  
  185. if not crafting and not isAltarFull() then
  186. done = false
  187. recipes = loadRecipes()
  188. for i,r in ipairs(recipes) do
  189. if not done and requestRecipeByTrigger(inputChest, r) then
  190. enableInfusion()
  191. done = true
  192. end
  193. end
  194. for i,r in ipairs(craftsWaiting) do
  195. if not done and requestQueuedRecipe(inputChest, r, i) then
  196. print("Processed recipe which infuses "..r.infusionItems[1].display_name.." from queue")
  197. enableInfusion()
  198. done = true
  199. end
  200. end
  201. end
  202.  
  203. if crafting and not isAltarFull() then
  204. error("Altar empty during craft, aborting")
  205. end
  206.  
  207. if not crafting and isAltarFull() then
  208. sleep(4)
  209. crafting = true
  210. currItem = altarItem()
  211. print("Infusing a " .. currItem)
  212. end
  213.  
  214. if crafting and altarItem() ~= currItem then
  215. print("Crafted a " .. altarItem())
  216. removeFromAltar()
  217. crafting = false
  218. disableInfusion()
  219. sleep(1)
  220. end
  221. sleep(2)
  222. end
  223. end
  224.  
  225. parallel.waitForAll(craftItems, listenForCrafts)
Advertisement
Add Comment
Please, Sign In to add comment