Advertisement
2222Jonathan

Farm

Dec 6th, 2019
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. os.loadAPI("JAPI/JMove.lua")
  2.  
  3. -- /\ +y (FORWARD)
  4. -- | F F F
  5. -- | F F F
  6. -- | F F F
  7. -- | T F F F
  8. -- ---------> +x
  9.  
  10. function printUsage()
  11. textutils.pagedPrint("Usage: farm <width> <length> <farm dir up>")
  12. textutils.pagedPrint(" <farm dir up> can be a number or a string.")
  13. textutils.pagedPrint(" Number: 0 .. 3")
  14. textutils.pagedPrint(" String: FORWARD/POS_Y, LEFT/NEG_X, BACK/NEG_Y or RIGHT/POS_X")
  15. error()
  16. end
  17.  
  18. local tArgs = { ... }
  19. if #tArgs ~= 3 then
  20. printUsage()
  21. return
  22. end
  23.  
  24. local farmWidth = tArgs[1]
  25. local farmLength = tArgs[2]
  26. local farmDirUp = tArgs[3]
  27.  
  28. local success, data = JMove.isDir(farmDirUp)
  29. if success == false then
  30. printUsage()
  31. return
  32. end
  33.  
  34. local farmItemType = "minecraft:wheat"
  35. local farmItemSeedType = "minecraft:wheat_seeds"
  36. local waitTime = 2
  37.  
  38. function waitToBegin()
  39. JMove.turnTo(JMove.POS_X)
  40. local begin = false
  41. while begin == false do
  42. sleep(waitTime)
  43. local s, d = turtle.inspect()
  44. if s then
  45. print("Waiting...")
  46. if d.name == farmItemType then
  47. local age = d.state.age
  48. if age == 7 then
  49. print("Initiate farming")
  50. begin = true
  51. end
  52. end
  53. else
  54. print("Wheat at (0, 0, 0) is gone!")
  55. begin = true
  56. end
  57. end
  58. end
  59.  
  60. function dumpItems()
  61. JMove.turnTo(JMove.NEG_Y)
  62. local numSeeds = 0
  63. local shouldDumpSeeds = false
  64. for i=1, 16, 1 do
  65. turtle.select(i)
  66. local data = turtle.getItemDetail()
  67. if data then
  68. if data.name == farmItemSeedType then
  69. if shouldDumpSeeds == false then
  70. numSeeds = numSeeds + data.count
  71. local numFarmBlocks = farmWidth*farmLength
  72. if numSeeds > numFarmBlocks then
  73. local dropCount = numSeeds-numFarmBlocks
  74. shouldDumpSeeds = true
  75. turtle.drop(dropCount)
  76. end
  77. else
  78. turtle.drop()
  79. end
  80. elseif data.name == farmItemType then
  81. turtle.drop()
  82. end
  83. end
  84. end
  85. end
  86.  
  87. function reset()
  88. JMove.setPos(-1, 0, -1)
  89. JMove.setPOSY(farmDirUp)
  90.  
  91. dumpItems()
  92. waitToBegin()
  93. end
  94.  
  95. function toStart()
  96. reset()
  97. JMove.refuel()
  98. JMove.moveTo(-1, 0, 0)
  99. JMove.moveTo(0, 0, 0)
  100. end
  101.  
  102. function placeSeed()
  103. local seeds = JUtils.findItem(farmItemSeedType)
  104. if seeds then
  105. turtle.placeDown()
  106. local s, d = turtle.inspectDown()
  107. if s then
  108. if d.name ~= farmItemType then
  109. print("Warning: Tile at", JMove.posToStr(), "is blocked!")
  110. else
  111. print("Placed seed at", JMove.posToStr())
  112. end
  113. else
  114. -- Till the dirt block
  115. local couldTill= turtle.digDown()
  116. if couldTill == false then
  117. print("Warning: Missing dirt block at", JMove.posToStr(), "!")
  118. else
  119. turtle.placeDown()
  120. print("Tilled and placed seed at", JMove.posToStr(), "!")
  121. end
  122. end
  123. else
  124. print("No seeds was found, add seeds and press 'enter' when done!")
  125. local hasSeeds = false
  126. while hasSeeds == false do
  127. action, key = os.pullEventRaw()
  128. if action == "key" then
  129. if key == keys.enter then
  130. local seeds = JUtils.findItem(farmItemSeedType)
  131. if seeds then
  132. hasSeeds = true
  133. else
  134. print("That was not seeds, try again and press 'enter' when done!")
  135. end
  136. end
  137. end
  138. end
  139. placeSeed()
  140. end
  141. end
  142.  
  143. function farm()
  144. local success, data = turtle.inspectDown()
  145. if success then
  146. if data.name == farmItemType then
  147. local age = data.state.age
  148. -- Destroy is full grown
  149. if age == 7 then
  150. turtle.digDown()
  151. print("Farmed wheet at", JMove.posToStr(), "!")
  152. placeSeed()
  153. end
  154. end
  155. else
  156. placeSeed()
  157. end
  158. end
  159.  
  160. local fw = farmWidth-1
  161. local fh = farmLength-1
  162. while true do
  163. toStart()
  164. farm()
  165.  
  166. y = 0
  167. repeat
  168. local xStart = fw-1
  169. local xEnd = 0
  170. local xStep = -1
  171. if y%2 == 0 then
  172. xStart = 1
  173. xEnd = fw
  174. xStep = 1
  175. end
  176. if y ~= 0 then
  177. local i = xStart + 1
  178. if y%2 == 0 then i = 0 end
  179. JMove.moveTo(i, y, 0)
  180. farm()
  181. end
  182. for x=xStart,xEnd,xStep do
  183. JMove.moveTo(x, y, 0)
  184. farm()
  185. end
  186. y = y + 1
  187. until y > fh
  188.  
  189. -- Move back to starting position on the farm.
  190. JMove.moveTo(0, 0, 0)
  191. JMove.moveTo(-1, 0, 0)
  192. JMove.moveTo(-1, 0, -1)
  193. JMove.turnTo(farmDirUp)
  194. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement