Advertisement
asweigart

milker

Jul 31st, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. -- Milker program
  2. -- By Al Sweigart
  3. -- al@inventwithpython.com
  4. -- Milks cows in a field.
  5.  
  6.  
  7. os.loadAPI('hare')
  8.  
  9. local cliArgs = {...}
  10. local rowsArg = tonumber(cliArgs[1])
  11. local columnsArg = tonumber(cliArgs[2])
  12.  
  13. if columnsArg == nil then
  14. print('Usage: milker rows columns')
  15. return
  16. end
  17.  
  18.  
  19. function milkCow()
  20. -- keep milking the cow as long as
  21. -- there are empty buckets and a cow
  22. while hare.selectItem('minecraft:bucket') and turtle.placeDown() do
  23. print('Milked the cow.')
  24. end
  25. end
  26.  
  27.  
  28. function storeMilkBuckets()
  29. -- face the milk bucket chest
  30. if not hare.findBlock('minecraft:chest') then
  31. print('ERROR: Cannot find milk bucket chest!')
  32. return
  33. end
  34.  
  35. -- drop any item that isn't an empty bucket
  36. print('Dropping off milk buckets...')
  37. local slot
  38. for slot=1,16 do
  39. local item = turtle.getItemDetail(slot)
  40. if item ~= nil and item['name'] ~= 'minecraft:bucket' then
  41. turtle.select(slot)
  42. while not turtle.drop() do
  43. print('Milk bucket chest is full!')
  44. print('Waiting for the chest to be emptied...')
  45. os.sleep(10)
  46. end
  47. end
  48. end
  49. end
  50.  
  51. print('Hold Ctrl+T to stop.')
  52.  
  53.  
  54. -- start by facing the milk bucket chest
  55. if not hare.findBlock('minecraft:chest') then
  56. print('ERROR: Must start next to a chest!')
  57. end
  58.  
  59. storeMilkBuckets() -- drop off anything that isn't an empty bucket
  60. print('SHOULD HAVE ONLY EMPTY BUCKETS NOW')
  61. os.sleep(5)
  62. while true do
  63. -- check fuel
  64. if turtle.getFuelLevel() < (rowsArg * columnsArg) + rowsArg + columnsArg + 2 then
  65. print('ERROR: Not enough fuel.')
  66. return
  67. end
  68.  
  69. -- grab up to 16 empty buckets
  70. turtle.up()
  71. while hare.countItems('minecraft:bucket') < 16 do
  72. if not turtle.suck(1) then
  73. if hare.countItems('minecraft:bucket') == 0 then
  74. -- More empty buckets are needed in the empty bucket chest
  75. print('Need empty buckets to continue...')
  76. os.sleep(10)
  77. else
  78. break -- we can just use the buckets we have
  79. end
  80. end
  81. end
  82. turtle.down()
  83.  
  84. -- put all empty buckets into one slot
  85.  
  86. print('SHOULD NOW HAVE UP TO 16 BUCKETS')
  87. os.sleep(5)
  88.  
  89. -- face the field
  90. turtle.turnLeft()
  91. turtle.turnLeft()
  92.  
  93. -- find and milk cows
  94. print('Sweeping field...')
  95. hare.sweepField(rowsArg, columnsArg, milkCow, storeMilkBuckets)
  96. print('MILK BUCKETS NOW DROPPED OFF')
  97. os.sleep(5)
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement