druidbruce

witchery cauldron triggre craft(2 category)/startup

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