druidbruce

standard trigger crafting (3 categories)/startup

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