asweigart

farm cactus

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