Advertisement
asweigart

shearsheep

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