gravitowl

SimpleFarmingScript

Mar 20th, 2021 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. --[[
  2.  
  3. Script Created by CEEBS
  4. YouTube: https://www.youtube.com/channel/UCQvB8QBknoS1jYgwpYGQrQg
  5. Twitter: https://twitter.com/OnlyCeebs
  6.  
  7. Really appreciate all the support you have given me, so thank you!
  8.  
  9. ]]--
  10.  
  11. local INVENTORY_SIZE = 16
  12. local MAIN_LOOP_INTERVAL = 420
  13. local FUEL_CROP_CHEST_INTERVAL = 5
  14. local length = 0
  15. local rows = 0
  16.  
  17. -- Receive arguments and validate user input
  18. if #arg == 2 then
  19. length = tonumber(arg[1])
  20. rows = tonumber(arg[2])
  21.  
  22. if length <= 1 or rows <= 1 then
  23. print("Please enter values bigger than 1")
  24. return
  25. end
  26. else
  27. print("Please enter both the length and with of the farm area. (e.g. scriptName 10 10)")
  28. return
  29. end
  30.  
  31. -- List of accepted fuels
  32. local ACCEPTED_FUELS = {
  33. "minecraft:coal_block",
  34. "minecraft:coal",
  35. "druidcraft:fiery_glass"
  36. }
  37.  
  38. -- List of accepted seeds
  39. local SEEDS = {
  40. "minecraft:carrot",
  41. "minecraft:potato",
  42. "minecraft:wheat_seeds",
  43. "mysticalworld:aubergine_seeds",
  44. "farmersdelight:onion"
  45. }
  46.  
  47. -- List of mature crops
  48. local CROPS = {
  49. "minecraft:carrots",
  50. "minecraft:potatoes",
  51. "minecraft:wheat",
  52. "farmersdelight:onions",
  53. "mysticalworld:aubergine_crop"
  54. }
  55.  
  56. -- Refuel using the found fuel
  57. function refuel(slot_number)
  58. turtle.select(slot_number)
  59. turtle.refuel()
  60. end
  61.  
  62. -- Check the current fuel level
  63. function checkFuelLevel()
  64.  
  65. local currentFuelLevel = turtle.getFuelLevel()
  66.  
  67. if currentFuelLevel <= 0 then
  68. print("Out of fuel, waiting for fuel to be added. Checking for fuel every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  69. while currentFuelLevel <= 0 do
  70. for i = 1, INVENTORY_SIZE do
  71. local currentItem = turtle.getItemDetail(i)
  72. if currentItem ~= nil then
  73. for x = 1, #ACCEPTED_FUELS do
  74. if currentItem.name == ACCEPTED_FUELS[x] then
  75. refuel(i)
  76. end
  77. end
  78. end
  79. end
  80. sleep(FUEL_CROP_CHEST_INTERVAL)
  81. currentFuelLevel = turtle.getFuelLevel()
  82. end
  83. print("Fuel added successfully, continuing...")
  84. end
  85. end
  86.  
  87. -- Get the amount of seeds
  88. function getSeedsCount()
  89. local seedsCount = 0
  90.  
  91. for i = 1, INVENTORY_SIZE do
  92. local currentItem = turtle.getItemDetail(i)
  93. if currentItem ~= nil then
  94. for x = 1, #SEEDS do
  95. if currentItem.name == SEEDS[x] then
  96. seedsCount = seedsCount + turtle.getItemCount(i)
  97. end
  98. end
  99. end
  100. end
  101.  
  102. return seedsCount
  103. end
  104.  
  105. -- Checking for seeds
  106. function checkSeedsCount()
  107.  
  108. local seedsCount = getSeedsCount()
  109.  
  110. if seedsCount <= 0 then
  111. print("Out of seeds, waiting for seeds to be added. Checking for seeds every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  112. while seedsCount <= 0 do
  113. seedsCount = getSeedsCount()
  114. sleep(FUEL_CROP_CHEST_INTERVAL)
  115. end
  116. print("Seeds added successfully, continuing...")
  117. end
  118.  
  119. end
  120.  
  121. -- Movement helper functions
  122. function moveForward(times)
  123.  
  124. checkFuelLevel()
  125. checkSeedsCount()
  126.  
  127. if times then
  128. for i = 1, times do
  129. turtle.forward()
  130. turtle.suckDown()
  131. end
  132. else
  133. turtle.forward()
  134. turtle.suckDown()
  135. end
  136. end
  137.  
  138. function turnLeft(times)
  139. if times then
  140. for i = 1, times do
  141. turtle.turnLeft()
  142. end
  143. else
  144. turtle.turnLeft()
  145. end
  146. end
  147.  
  148. function turnRight(times)
  149. if times then
  150. for i = 1, times do
  151. turtle.turnRight()
  152. end
  153. else
  154. turtle.turnRight()
  155. end
  156. end
  157.  
  158. -- Plant our crops!
  159. function plantCrops()
  160. for i = 1, INVENTORY_SIZE do
  161. local currentItem = turtle.getItemDetail(i)
  162. if currentItem ~= nil then
  163. for x = 1, #SEEDS do
  164. if currentItem.name == SEEDS[x] then
  165. turtle.select(i)
  166. turtle.placeDown()
  167. break
  168. end
  169. end
  170. end
  171. end
  172. end
  173.  
  174. -- Harvest our crops!
  175. function harvestCrops()
  176. local isBlock, block = turtle.inspectDown()
  177.  
  178. if isBlock ~= false then
  179. local isCrop = false
  180.  
  181. for x = 1, #CROPS do
  182. if block.name == CROPS[x] then
  183. isCrop = true
  184. end
  185. end
  186.  
  187. if isCrop then
  188. if block.state.age == 7 then
  189. turtle.digDown()
  190. turtle.suckDown()
  191. plantCrops()
  192. end
  193. else
  194. if block.name == "minecraft:grass" then
  195. turtle.digDown()
  196. turtle.digDown()
  197. turtle.suckDown()
  198. plantCrops()
  199. end
  200. end
  201.  
  202. else
  203. turtle.digDown()
  204. turtle.suckDown()
  205. plantCrops()
  206. end
  207. end
  208.  
  209. -- Inventory sorting
  210. function inventorySort()
  211. for j = 1, INVENTORY_SIZE do
  212. local currentItem = turtle.getItemDetail(j)
  213.  
  214. if currentItem ~= nil then
  215. turtle.select(j)
  216. for k = j, INVENTORY_SIZE do
  217. if turtle.compareTo(k) then
  218. turtle.select(k)
  219. turtle.transferTo(j)
  220. turtle.select(j)
  221. end
  222. end
  223. end
  224. end
  225. end
  226.  
  227. -- Dump inventory
  228. function dumpInventory()
  229.  
  230. local cropCount = 0
  231. local requiredSeedsCount = math.ceil(length * rows)
  232.  
  233. for i = 1, INVENTORY_SIZE do
  234.  
  235. local currentItem = turtle.getItemDetail(i)
  236.  
  237. if currentItem ~= nil then
  238.  
  239. local isFuel = false
  240. local isCrop = false
  241.  
  242. for x = 1, #ACCEPTED_FUELS do
  243. if currentItem.name == ACCEPTED_FUELS[x] then
  244. isFuel = true
  245. break
  246. end
  247. end
  248.  
  249. if not isFuel then
  250. for x = 1, #SEEDS do
  251. if currentItem.name == SEEDS[x] then
  252. if cropCount < requiredSeedsCount then
  253. cropCount = cropCount + currentItem.count
  254. isCrop = true
  255. else
  256. isCrop = false
  257. end
  258. end
  259. end
  260. end
  261.  
  262. if not isCrop and not isFuel then
  263. turtle.select(i)
  264. while turtle.dropDown() == false do
  265. print("No room in target inventory, please clear some space. Checking target inventory every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  266. sleep(FUEL_CROP_CHEST_INTERVAL)
  267. end
  268. end
  269. end
  270. end
  271.  
  272. inventorySort()
  273. end
  274.  
  275. -- Farming loop
  276. function farm()
  277.  
  278. print("Starting farm loop.")
  279.  
  280. for i = 1, rows do
  281.  
  282. if i ~= 1 then
  283. for j = 1, length - 1 do
  284. moveForward()
  285. harvestCrops()
  286. end
  287. else
  288. for j = 1, length do
  289. moveForward()
  290. harvestCrops()
  291. end
  292. end
  293.  
  294. if i == rows then
  295. if i % 2 == 0 then
  296. moveForward()
  297. turnRight()
  298. moveForward(rows - 1)
  299. turnRight()
  300. else
  301. turnRight()
  302. moveForward()
  303. turnRight()
  304. moveForward(length)
  305. turnRight()
  306. moveForward(rows)
  307. turnRight()
  308. end
  309. else
  310. if i % 2 == 0 then
  311. turnLeft()
  312. moveForward()
  313. harvestCrops()
  314. turnLeft()
  315. else
  316. turnRight()
  317. moveForward()
  318. harvestCrops()
  319. turnRight()
  320. end
  321. end
  322.  
  323. end
  324.  
  325. dumpInventory()
  326. end
  327.  
  328. while true do
  329. farm()
  330. print("Completed loop, waiting " .. MAIN_LOOP_INTERVAL .. " seconds to start next loop.")
  331. sleep(MAIN_LOOP_INTERVAL)
  332. end
Add Comment
Please, Sign In to add comment