Advertisement
asweigart

farm sugar cane

Aug 3rd, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. -- Farm Sugar Cane program
  2. -- By Al Sweigart
  3. -- turtleappstore.com/users/AlSweigart
  4. -- Automatically farms sugar cane.
  5.  
  6. -- NOTE: Sugar cane has the name minecraft:reeds
  7.  
  8. os.loadAPI('hare')
  9.  
  10. -- handle command line arguments
  11. local cliArgs = {...}
  12. local rowsArg = tonumber(cliArgs[1])
  13. local columnsArg = tonumber(cliArgs[2])
  14.  
  15. -- display "usage" info
  16. if columnsArg == nil or cliArgs[1] == '?' then
  17. print('Usage: farmsugar <forward> <right>')
  18. return
  19. end
  20.  
  21.  
  22. function checkCrop()
  23. local result, block = turtle.inspectDown()
  24.  
  25. if block ~= nil and block['name'] == 'minecraft:reeds' then
  26. if turtle.digDown() then -- collect sugar cane
  27. print('Collected sugar cane.')
  28. end
  29. end
  30. end
  31.  
  32.  
  33. function storeSugarCane()
  34. if not hare.findBlock('minecraft:chest') then -- face the chest
  35. print('Warning: Could not find chest.')
  36. return
  37. end
  38.  
  39. -- drop off sugar cane
  40. local numToSave = rowsArg * columnsArg
  41. while hare.countItems('minecraft:reeds') > numToSave do
  42. hare.selectItem('minecraft:reeds')
  43. local numToDropOff = math.min((hare.countItems('minecraft:reeds') - numToSave), turtle.getItemCount())
  44. print('Dropping off ' .. numToDropOff .. ' reeds...')
  45. if not turtle.drop(numToDropOff) then
  46. print('Sugar cane chest is full!')
  47. print('Waiting for chest to be emptied...')
  48. while not turtle.drop(numToDropOff) do
  49. os.sleep(10)
  50. end
  51. end
  52. end
  53.  
  54. -- face field again
  55. turtle.turnLeft()
  56. turtle.turnLeft()
  57. end
  58.  
  59.  
  60. print('Hold Ctrl+T to stop.')
  61. if not hare.findBlock('minecraft:chest') then
  62. print('ERROR: Must start next to a chest!')
  63. end
  64.  
  65. -- face field
  66. turtle.turnLeft()
  67. turtle.turnLeft()
  68.  
  69. while true do
  70. -- check fuel
  71. if turtle.getFuelLevel() < (rowsArg * columnsArg) + rowsArg + columnsArg then
  72. print('ERROR: Not enough fuel.')
  73. return
  74. end
  75.  
  76. -- farm sugar cane
  77. print('Sweeping field...')
  78. hare.sweepField(rowsArg, columnsArg, checkCrop)
  79. storeSugarCane()
  80.  
  81. print('Sleeping for 10 minutes...')
  82. os.sleep(600)
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement