Advertisement
Fews

Craft

Jul 31st, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. local menu=0
  2.  
  3. while true do
  4. term.clear()
  5. term.setCursorPos(1,1)
  6. print("===========================")
  7. print(" Custom Crafting - by fews ")
  8. print("===========================")
  9. print("Select Menu")
  10. print("1. See Recipe")
  11. print("2. Craft")
  12. print("3. Exit")
  13. io.write("Menu : ") local menu=read()
  14. if tonumber(menu) == 1 then
  15. term.clear()
  16. term.setCursorPos(1,1)
  17. local title = {}
  18. if fs.exists("recipe/listrecipe/listrecipe") then
  19. --get list recipe
  20. local fileObject = fs.open("recipe/listrecipe/listrecipe","r")
  21. local index=1;
  22. while true do
  23. local a = fileObject.readLine()
  24. if a == nil then
  25. break
  26. else
  27. title[index] = a
  28. index = index+1
  29. end
  30. end
  31.  
  32. print("Available Recipe")
  33. for i=1,index do
  34. print(title[i])
  35. end
  36. else
  37. print("No Recipe Available")
  38. end
  39. print("Press Enter to continue")
  40. local a = read()
  41. elseif tonumber(menu) == 2 then
  42. term.clear()
  43. term.setCursorPos(1,1)
  44. print("What you want to craft : ") local title = read()
  45. print(fs.exists("recipe/"..title))
  46. local recipe = {}
  47. local damage = {}
  48.  
  49. local itemneeded = {}
  50. local temp=0
  51. --read recipe
  52. if fs.exists("recipe/"..title) then
  53. local fileObject = fs.open("recipe/"..title, "r")
  54. for i=1,9 do
  55. local n = fileObject.readLine()
  56. recipe[i] = n
  57. if temp == 0 and recipe[i] ~="nil" then
  58. temp = temp + 1
  59. itemneeded[temp] = {}
  60. itemneeded[temp][1] = recipe[i]
  61. itemneeded[temp][2] = 1
  62. elseif recipe[i] ~="nil" then
  63. local noted = false
  64. for j=1 ,temp do
  65. if itemneeded[j][1] == recipe[i] then
  66. itemneeded[j][2] = itemneeded[j][2] + 1
  67. noted = true
  68. end
  69. end
  70. if noted == false then
  71. temp = temp + 1
  72. itemneeded[temp] = {}
  73. itemneeded[temp][1] = recipe[i]
  74. itemneeded[temp][2] = 1
  75. end
  76. end
  77. end
  78. for i=1,9 do
  79. local n = fileObject.readLine()
  80. damage[i] = n
  81. end
  82.  
  83.  
  84. print("Total "..title.." you want to craft : ") local total = read()
  85. for i=1,temp do
  86. itemneeded[i][2] = itemneeded[i][2] * total
  87. end
  88.  
  89.  
  90. --take material
  91. turtle.select(1)
  92. local totalitem = 0
  93. while true do
  94. local materialcomplete = false
  95. if turtle.suckUp() == true then
  96. totalitem = totalitem+1
  97. else
  98. -- move material needed or not
  99. for i=1,tonumber(totalitem)%16 do
  100. local neededitem=false
  101. turtle.select(i)
  102. for j=1,9 do
  103. if turtle.getItemDetail().name == recipe[j] and turtle.getItemDetail().damage == tonumber(damage[j]) then
  104. neededitem=true
  105. print("needed")
  106. for k=1,temp do
  107. if itemneeded[k][1] ~=nil then
  108. print("item "..itemneeded[k][1] .." need "..itemneeded[k][2])
  109. if turtle.getItemDetail().name == itemneeded[k][1] then
  110. local totalmaterial = tonumber(turtle.getItemCount())
  111. itemneeded[k][2] = itemneeded[k][2] - totalmaterial
  112. print("item "..itemneeded[k][1] .." need "..itemneeded[k][2])
  113. if itemneeded[k][2] <= 0 then
  114. itemneeded[k][1] = nil
  115. end
  116. end
  117. end
  118. end
  119. materialcomplete = true
  120. for k=1, temp do
  121. if itemneeded[k][1] ~= nil then
  122. materialcomplete = false
  123. end
  124. end
  125. turtle.dropDown()
  126. break
  127. end
  128. end
  129. if neededitem == false then
  130. turtle.drop()
  131. end
  132. end
  133. break
  134. end
  135.  
  136. -- if 16 slot full in turtle
  137. if totalitem % 16 == 0 then
  138. -- move material needed or not
  139. for i=1,16 do
  140. local neededitem=false
  141. turtle.select(i)
  142. for j=1,9 do
  143. if turtle.getItemDetail().name == recipe[j] and turtle.getItemDetail().damage == tonumber(damage[j]) then
  144. neededitem=true
  145. print("needed")
  146.  
  147. for k=1,temp do
  148. if itemneeded[k][1] ~=nil then
  149. print("item "..itemneeded[k][1] .." need "..itemneeded[k][2])
  150. if turtle.getItemDetail().name == itemneeded[k][1] then
  151. local totalmaterial = tonumber(turtle.getItemCount())
  152. itemneeded[k][2] = itemneeded[k][2] - totalmaterial
  153. print("item "..itemneeded[k][1] .." need "..itemneeded[k][2])
  154. if itemneeded[k][2] <= 0 then
  155. itemneeded[k][1] = nil
  156. end
  157. end
  158. end
  159. end
  160. materialcomplete = true
  161. for k=1, temp do
  162. if itemneeded[k][1] ~=nil then
  163. materialcomplete = false
  164. end
  165. end
  166. turtle.dropDown()
  167. break
  168. end
  169. end
  170. if neededitem == false then
  171. turtle.drop()
  172. end
  173. end
  174. turtle.select(1)
  175. if materialcomplete == true then
  176. print("Completed Needed Material")
  177. break
  178. end
  179. end
  180. end
  181.  
  182. print("total Item : "..totalitem)
  183.  
  184. print("Returning Item to Main Chest")
  185. --return item to main chest
  186. redstone.setOutput("right",false)
  187. sleep(1)
  188. redstone.setOutput("right",true)
  189.  
  190.  
  191. --take needed item
  192. for i=1,7 do
  193. local selected = 1
  194. if i<=3 then
  195. selected = i*4
  196. else
  197. selected = i+9
  198. end
  199. turtle.select(selected)
  200. if turtle.suckDown() == true then
  201. else
  202. break
  203. end
  204. end
  205.  
  206. --crafting
  207. local craftable = true;
  208. for i=1,9 do
  209. if recipe[i] ~= "nil" then
  210. local craft = i
  211. if i>3 and i<=6 then
  212. craft = craft + 1
  213. elseif i>6 then
  214. craft = craft + 2
  215. end
  216. local foundItem=false
  217. for j=1,7 do
  218. local selected = 1
  219. if j<=3 then
  220. selected = j*4
  221. else
  222. selected = j+9
  223. end
  224. turtle.select(selected)
  225. if turtle.getItemDetail() ~= nil then
  226. if turtle.getItemDetail().name == recipe[i] and tonumber(turtle.getItemCount()) >= tonumber(total) and turtle.getItemDetail().damage == tonumber(damage[i]) then
  227. turtle.transferTo(tonumber(craft),tonumber(total))
  228. foundItem = true
  229. break
  230. end
  231. end
  232. if j==7 and foundItem == false then
  233. print("Item "..recipe[i].." Not Enough")
  234. end
  235. end
  236. if foundItem == false then
  237. craftable=false;
  238. break;
  239. end
  240. end
  241. end
  242.  
  243.  
  244. -- move item back to storage
  245. for i=1,7 do
  246. local selected = 1
  247. if i<=3 then
  248. selected = i*4
  249. else
  250. selected = i+9
  251. end
  252. turtle.select(selected)
  253. if turtle.getItemDetail() ~= nil then
  254. turtle.drop()
  255. end
  256. end
  257.  
  258. --craft
  259. if craftable == true then
  260. turtle.craft()
  261. else
  262. for i=1,16 do
  263. turtle.select(i)
  264. turtle.drop()
  265. end
  266. end
  267. else
  268. print("Recipe not exists")
  269. end
  270.  
  271. print("Press Enter to continue")
  272. local a = read()
  273.  
  274. elseif tonumber(menu) == 3 then
  275. break
  276. end
  277.  
  278.  
  279.  
  280. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement