Advertisement
drpepper240

turtle shearer

Jun 11th, 2022 (edited)
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. local delay = 180
  2.  
  3. local function itemBelow()
  4.     local ok, item = turtle.inspectDown()
  5.     if ok then
  6.         if item["name"] == "minecraft:chest" or item["name"] == "ironchest:iron_chest" then
  7.             return "chest"
  8.         else
  9.             return "other"
  10.         end
  11.     else
  12.         return "none"
  13.     end
  14. end
  15.  
  16. local function tryFindChest(maxDistance)
  17.     local dx = 0
  18.     for i=1,maxDistance do
  19.         if itemBelow() == "chest" then
  20.             return true
  21.         end
  22.         if turtle.forward() then
  23.             dx = dx + 1
  24.         else
  25.             break
  26.         end
  27.     end
  28.     -- no luck, return back
  29.     turtle.turnRight()
  30.     turtle.turnRight()
  31.     for i=1,dx do
  32.         while not turtle.forward() do
  33.             sleep(0.5)
  34.         end
  35.     end
  36.     return false
  37. end
  38.  
  39. local function unload()
  40.     local total = 0
  41.     for i=1,16 do
  42.         if turtle.getItemCount(i) > 0 then
  43.             total = total + turtle.getItemCount(i)
  44.             turtle.select(i)
  45.             if not turtle.dropDown() then
  46.                 print("No space in the chest")
  47.                 fs.delete("startup")
  48.                 return false
  49.             end
  50.             sleep(0.5)
  51.         end
  52.     end
  53.     print("Time: "..os.time().." items: "..total.." fuel: "..turtle.getFuelLevel())
  54.     return true
  55. end
  56.  
  57. -- enable autorun
  58. local s = "shell.run(\"shear\")"
  59. local f = fs.open("/startup", "w")
  60. f.write(s)
  61. f.close()
  62.  
  63. -- find chest
  64. if itemBelow() ~= "chest" then
  65.     print("Chest not found")
  66.     return
  67. end
  68.  
  69. if not unload() then
  70.     return
  71. end
  72.  
  73. while true do
  74.     -- check fuel
  75.     if turtle.getFuelLevel() < 100 then
  76.         print("Low fuel")
  77.         return
  78.     end
  79.  
  80.     -- sanity check
  81.     if itemBelow() ~= "chest" then
  82.         print("Chest not found")
  83.         return
  84.     end
  85.  
  86.     -- harvest
  87.     local dx = 0
  88.     while true do
  89.         if turtle.forward() then
  90.             dx = dx + 1
  91.         else
  92.             break
  93.         end
  94.        
  95.         local item = itemBelow()
  96.         if item == "none" then
  97.             repeat
  98.                 local sheared = turtle.attackDown()
  99.             until not sheared
  100.         elseif item ~= "none" then
  101.             break
  102.         end
  103.     end
  104.  
  105.     -- return back
  106.     turtle.turnRight()
  107.     turtle.turnRight()
  108.     for i=1,dx do
  109.         while not turtle.forward() do
  110.             sleep(0.5)
  111.         end
  112.     end
  113.     turtle.turnRight()
  114.     turtle.turnRight()
  115.    
  116.     -- unload
  117.     if not unload() then
  118.         return
  119.     end
  120.  
  121.     sleep(delay)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement