druidbruce

witchery kettle trigger craft(3 category)/startup

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