asweigart

Untitled

Jul 31st, 2016
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. -- Farm Carrots program
  2. -- By Al Sweigart
  3. -- Automatically farms carrots.
  4.  
  5. --[[
  6. IMPORTANT NOTE!!!
  7. Planted carrots in the game world are
  8. named 'minecraft:carrots' but carrots
  9. in your inventory are called
  10. 'minecraft:carrot'
  11. ]]
  12.  
  13. os.loadAPI('hare')
  14.  
  15. local cliArgs = {...}
  16. local rowsArg = tonumber(cliArgs[1])
  17. local columnsArg = tonumber(cliArgs[2])
  18.  
  19. if columnsArg == nil then
  20. print('Usage: farmcarrots rows columns')
  21. return
  22. end
  23.  
  24.  
  25. function checkCrop()
  26. local result, block = turtle.inspectDown()
  27.  
  28. if result == false then
  29. turtle.digDown() -- till the soil
  30. plantCarrot()
  31. elseif block ~= nil and block['name'] == 'minecraft:carrots' and block['metadata'] == 7 then
  32. if turtle.digDown() then -- collect carrots
  33. print('Collected carrots.')
  34. plantCarrot()
  35. end
  36. end
  37. end
  38.  
  39.  
  40. function plantCarrot()
  41. if hare.selectItem('minecraft:carrot') == false then
  42. print('Warning: Low on carrots.')
  43. return false
  44. elseif turtle.placeDown() then -- plant a carrot
  45. print('Planted carrot.')
  46. return true
  47. else
  48. return false -- couldn't plant
  49. end
  50. end
  51.  
  52.  
  53. function storeCarrots()
  54. if not hare.findBlock('minecraft:chest') then -- face the chest
  55. print('Warning: Could not find chest.')
  56. return
  57. end
  58.  
  59. local numToKeep = hare.countItems('minecraft:carrot') - (rowsArg * columnsArg)
  60. local droppedOff = 0
  61.  
  62. -- drop off carrots
  63. while hare.selectItem('minecraft:carrot') do
  64. if numToKeep > droppedOff then
  65. local numToDropOff = math.min((total - rowsArg * columnsArg - droppedOff), turtle.getItemCount())
  66. print('Dropping off ' .. numToDropOff .. ' carrots...')
  67. turtle.drop(numToDropOff)
  68. droppedOff = droppedOff + numToDropOff
  69. if droppedOff == numToDropOff then
  70. break -- done dropping off carrots
  71. end
  72. end
  73. end
  74.  
  75. -- face field again
  76. turtle.turnLeft()
  77. turtle.turnLeft()
  78. end
  79.  
  80.  
  81. print('Hold Ctrl+T to stop.')
  82. if not hare.findBlock('minecraft:chest') then
  83. print('ERROR: Must start next to a chest!')
  84. end
  85.  
  86. -- face field
  87. turtle.turnLeft()
  88. turtle.turnLeft()
  89.  
  90. while true do
  91. -- check fuel
  92. if turtle.getFuelLevel() < (rowsArg * columnsArg) + rowsArg + columnsArg then
  93. print('ERROR: Not enough fuel.')
  94. return
  95. end
  96.  
  97. -- farm carrots
  98. print('Sweeping field...')
  99. hare.sweepField(rowsArg, columnsArg, checkCrop, storeCarrots)
  100.  
  101. print('Sleeping for 10 minutes...')
  102. os.sleep(600)
  103. end
Advertisement
Add Comment
Please, Sign In to add comment