Reskatron

beetfarm

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