Advertisement
asweigart

farmpotaotes

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