asweigart

farmpotaotes

Jul 31st, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. -- Farm Potatoes program
  2. -- By Al Sweigart
  3. -- Automatically farms potatoes.
  4.  
  5. --[[
  6. IMPORTANT NOTE!!!
  7. Planted potatoes in the game world are
  8. named 'minecraft:potatoes' but potatoes
  9. in your inventory are called
  10. 'minecraft:potato'
  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: farmpotatoes 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. plantPotato()
  31. elseif block ~= nil and block['name'] == 'minecraft:potatoes' and block['metadata'] == 7 then
  32. if turtle.digDown() then -- collect potatoes
  33. print('Collected potatoes.')
  34. plantPotato()
  35. end
  36. end
  37. end
  38.  
  39.  
  40. function plantPotato()
  41. if hare.selectItem('minecraft:potato') == false then
  42. print('Warning: Low on potatoes.')
  43. return false
  44. elseif turtle.placeDown() then -- plant a potato
  45. print('Planted potato.')
  46. return true
  47. else
  48. return false -- couldn't plant
  49. end
  50. end
  51.  
  52.  
  53. function storePotatoes()
  54. if not hare.findBlock('minecraft:chest') then -- face the chest
  55. print('Warning: Could not find chest.')
  56. return
  57. end
  58.  
  59. -- drop off potatoes
  60. local numToSave = rowsArg * columnsArg
  61. while hare.countItems('minecraft:potato') > numToSave do
  62. hare.selectItem('minecraft:potato')
  63. local numToDropOff = math.min((hare.countItems('minecraft:potato') - numToSave), turtle.getItemCount())
  64. print('Dropping off ' .. numToDropOff .. ' potatoes...')
  65. turtle.drop(numToDropOff)
  66. end
  67.  
  68. -- face field again
  69. turtle.turnLeft()
  70. turtle.turnLeft()
  71. end
  72.  
  73.  
  74. print('Hold Ctrl+T to stop.')
  75. if not hare.findBlock('minecraft:chest') then
  76. print('ERROR: Must start next to a chest!')
  77. end
  78.  
  79. -- face field
  80. turtle.turnLeft()
  81. turtle.turnLeft()
  82.  
  83. while true do
  84. -- check fuel
  85. if turtle.getFuelLevel() < (rowsArg * columnsArg) + rowsArg + columnsArg then
  86. print('ERROR: Not enough fuel.')
  87. return
  88. end
  89.  
  90. -- farm potatoes
  91. print('Sweeping field...')
  92. hare.sweepField(rowsArg, columnsArg, checkCrop, storePotatoes)
  93.  
  94. print('Sleeping for 10 minutes...')
  95. os.sleep(600)
  96. end
Advertisement
Add Comment
Please, Sign In to add comment