Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. 1. --[[Wheat Farming program by Al Sweigart
  2. 2. Plants and harvests wheat.
  3. 3. Assumes a field is in front and
  4. 4. to the right of the turtle,
  5. 5. with a chest behind it.]]
  6. 6.
  7. 7. os.loadAPI('hare')
  8. 8.
  9. 9. -- handle command line arguments
  10. 10. local cliArgs = {...}
  11. 11. local length = tonumber(cliArgs[1])
  12. 12. local width = tonumber(cliArgs[2])
  13. 13.
  14. 14. -- display "usage" info
  15. 15. if length == nil or width == nil or cliArgs[1] == '?' then
  16. 16. print('Usage: farmwheat <length> <width>')
  17. 17. return
  18. 18. end
  19. 19.
  20. 20. print('Hold Ctrl-T to stop.')
  21. 21.
  22. 22. -- check that chest is there
  23. 23. if not hare.findBlock('minecraft:chest') then
  24. 24. error('Must start next to a chest.')
  25. 25. end
  26. 26.
  27. 27. -- face field
  28. 28. turtle.turnLeft()
  29. 29. turtle.turnLeft()
  30. 30.
  31. 31.
  32. 32. -- checkWheatCrop() harvests mature wheat
  33. 33. -- and plants seeds
  34. 34. function checkWheatCrop()
  35. 35. local result, block = turtle.inspectDown()
  36. 36.
  37. 37. if not result then
  38. 38. turtle.digDown() -- till the soil
  39. 39. plantWheatSeed()
  40. 40. elseif block ~= nil and block['name'] == 'minecraft:wheat' and
  41. block['metadata'] == 7 then
  42. 41. -- collect wheat and replant
  43. 42. turtle.digDown()
  44. 43. print('Collected wheat.')
  45. 44. plantWheatSeed()
  46. 45. end
  47. 46. end
  48. 47.
  49. 48.
  50. 49. -- plantWheatSeed() attempts to plant
  51. 50. -- a wheat seed below the turtle
  52. 51. function plantWheatSeed()
  53. 52. if not hare.selectItem('minecraft:wheat_seeds') then
  54. 53. print('Warning: Low on seeds.')
  55. 54. else
  56. 55. turtle.placeDown() -- plant a seed
  57. 56. print('Planted seed.')
  58. 57. end
  59. 58. end
  60. 59.
  61. 60.
  62. 61. -- storeWheat() puts all wheat into an
  63. 62. -- adjacent chest
  64. 63. function storeWheat()
  65. 64. -- face the chest
  66. 65. if not hare.findBlock('minecraft:chest') then
  67. 66. error('Could not find chest.')
  68. 67. end
  69. 68.
  70. 69. -- store wheat in chest
  71. 70. while hare.selectItem('minecraft:wheat') do
  72. 71. print('Dropping off ' .. turtle.getItemCount() .. ' wheat...')
  73. 72. if not turtle.drop() then
  74. 73. error('Wheat chest is full!')
  75. 74. end
  76. 75. end
  77. 76.
  78. 77. -- face field again
  79. 78. turtle.turnLeft()
  80. 79. turtle.turnLeft()
  81. 80. end
  82. 81.
  83. 82.
  84. 83. -- begin farming
  85. 84. while true do
  86. 85. -- check fuel
  87. 86. if turtle.getFuelLevel() < (length * width + length + width) then
  88. 87. error('Turtle needs more fuel!')
  89. 88. end
  90. 89.
  91. 90. -- farm wheat
  92. 91. print('Sweeping field...')
  93. 92. hare.sweepField(length, width, checkWheatCrop)
  94. 93. storeWheat()
  95. 94.
  96. 95. print('Sleeping for 10 minutes...')
  97. 96. os.sleep(600)
  98. 97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement