Advertisement
asweigart

milker

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