asweigart

shearsheep

Jul 31st, 2016
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. -- Sheep Shearer program
  2. -- By Al Sweigart
  3. -- Shears sheep 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: shearer rows columns')
  14. return
  15. end
  16.  
  17.  
  18. function shearSheep()
  19. -- keep shearing sheep as long as
  20. -- there are empty buckets and sheep
  21. local preShearAmount = hare.countItems('minecraft:wool')
  22. while turtle.placeDown() do
  23. if hare.countItems('minecraft:wool') == preShearAmount then
  24. break -- "sheared" an already sheared sheep, so break
  25. end
  26. print('Sheared a sheep.')
  27. preShearAmount = hare.countItems('minecraft:wool')
  28. end
  29. end
  30.  
  31.  
  32. function storeWool()
  33. -- face the chest
  34. if not hare.findBlock('minecraft:chest') then
  35. error('ERROR: Cannot find the wool chest!')
  36. end
  37.  
  38. -- drop any item that isn't an empty bucket
  39. print('Dropping off wool...')
  40. local slot
  41. for slot=1,16 do
  42. local item = turtle.getItemDetail(slot)
  43. if item ~= nil and item['name'] ~= 'minecraft:shears' then
  44. turtle.select(slot)
  45. turtle.drop()
  46. end
  47. end
  48.  
  49. -- face the field again
  50. turtle.turnLeft()
  51. turtle.turnLeft()
  52. end
  53.  
  54.  
  55. print('Hold Ctrl+T to stop.')
  56.  
  57. -- start by facing the chest
  58. if not hare.findBlock('minecraft:chest') then
  59. print('ERROR: Must start next to a chest!')
  60. return
  61. end
  62.  
  63. -- face the field
  64. turtle.turnLeft()
  65. turtle.turnLeft()
  66.  
  67. while true do
  68. -- check fuel
  69. if turtle.getFuelLevel() < (rowsArg * columnsArg) + rowsArg + columnsArg then
  70. print('ERROR: Not enough fuel.')
  71. return
  72. end
  73.  
  74. -- select the shears
  75. if not hare.selectItem('minecraft:shears') then
  76. print('ERROR: Need shears in inventory.')
  77. return
  78. end
  79.  
  80. -- find and shear sheep
  81. print('Sweeping field...')
  82. hare.sweepField(rowsArg, columnsArg, shearSheep, storeWool)
  83.  
  84. print('Sleeping for 5 minutes...')
  85. os.sleep(300)
  86. end
Advertisement
Add Comment
Please, Sign In to add comment