Advertisement
asweigart

Untitled

Jul 31st, 2016
222
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. -- al@inventwithpython.com
  4. -- Automatically farms carrots.
  5.  
  6. --[[
  7. IMPORTANT NOTE!!!
  8. Planted carrots in the game world are
  9. named 'minecraft:carrots' but carrots
  10. in your inventory are called
  11. 'minecraft:carrot'
  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: farmcarrots 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. plantCarrot()
  32. elseif block ~= nil and block['name'] == 'minecraft:carrots' and block['metadata'] == 7 then
  33. if turtle.digDown() then -- collect carrots
  34. print('Collected carrots.')
  35. plantCarrot()
  36. end
  37. end
  38. end
  39.  
  40.  
  41. function plantCarrot()
  42. if hare.selectItem('minecraft:carrot') == false then
  43. print('Warning: Low on carrots.')
  44. return false
  45. elseif turtle.placeDown() then -- plant a carrot
  46. print('Planted carrot.')
  47. return true
  48. else
  49. return false -- couldn't plant
  50. end
  51. end
  52.  
  53.  
  54. function storeCarrots()
  55. if not hare.findBlock('minecraft:chest') then -- face the chest
  56. print('Warning: Could not find chest.')
  57. return
  58. end
  59.  
  60. local numToKeep = hare.countItems('minecraft:carrot') - (rowsArg * columnsArg)
  61. local droppedOff = 0
  62.  
  63. -- drop off carrots
  64. while hare.selectItem('minecraft:carrot') do
  65. if numToKeep > droppedOff then
  66. local numToDropOff = math.min((total - rowsArg * columnsArg - droppedOff), turtle.getItemCount())
  67. print('Dropping off ' .. numToDropOff .. ' carrots...')
  68. turtle.drop(numToDropOff)
  69. droppedOff = droppedOff + numToDropOff
  70. if droppedOff == numToDropOff then
  71. break -- done dropping off carrots
  72. end
  73. end
  74. end
  75.  
  76. -- face field again
  77. turtle.turnLeft()
  78. turtle.turnLeft()
  79. end
  80.  
  81.  
  82. print('Hold Ctrl+T to stop.')
  83. if not hare.findBlock('minecraft:chest') then
  84. print('ERROR: Must start next to a chest!')
  85. end
  86.  
  87. -- face field
  88. turtle.turnLeft()
  89. turtle.turnLeft()
  90.  
  91. while true do
  92. -- check fuel
  93. if turtle.getFuelLevel() < (rowsArg * columnsArg) + rowsArg + columnsArg then
  94. print('ERROR: Not enough fuel.')
  95. return
  96. end
  97.  
  98. -- farm carrots
  99. print('Sweeping field...')
  100. hare.sweepField(rowsArg, columnsArg, checkCrop, storeCarrots)
  101.  
  102. print('Sleeping for 10 minutes...')
  103. os.sleep(600)
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement