Enderlein

Turtle Harvest Cactus

Jul 12th, 2022 (edited)
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. LENGTH = 8
  2. -- for harvesting from a square cactus farm
  3. -- turtle should be on top of a corner cactus, on "left" edge of square, facing the inside of the square
  4.  
  5. function checkFuel()
  6.     if turtle.getFuelLevel() < (LENGTH * LENGTH) then -- basic check, not precise
  7.         shell.run("refuel")
  8.     end
  9. end
  10.  
  11. function seesCactus()
  12.     not_air, block = turtle.inspectDown()
  13.  
  14.     if not_air then
  15.         if block.name == "minecraft:cactus" then
  16.             return true
  17.         end
  18.     end
  19.  
  20.     return false
  21.  
  22. end
  23.  
  24. function selectItem(query)
  25.     for slot = 1, 16, 1 do
  26.         turtle.select(slot)
  27.         if turtle.getItemDetail() then
  28.             if turtle.getItemDetail().name == query then
  29.                 return true
  30.             end
  31.  
  32.             if slot == 16 then
  33.                 return false
  34.             end
  35.         end
  36.     end
  37. end
  38.  
  39. function breakCactus()
  40.     turtle.digDown()
  41.     turtle.down()
  42.     turtle.digDown()
  43.     turtle.up()
  44. end
  45.  
  46. function turnSwitch(t)
  47.     if t % 2 == 0 then
  48.         turtle.turnLeft()
  49.     else
  50.         turtle.turnRight()
  51.     end
  52. end
  53.  
  54. function homeFromEnd()
  55.     turtle.turnRight()
  56.  
  57.     for k = 1, LENGTH - 1, 1 do
  58.         turtle.forward()
  59.     end
  60.  
  61.     turtle.turnRight()
  62. end
  63.  
  64. function dropNoFuel()
  65.     item = turtle.getItemDetail()
  66.  
  67.     if item then
  68.         if item.name ~= "minecraft:coal" then
  69.             turtle.dropDown()
  70.         end
  71.     end
  72. end
  73.  
  74. function dumpInventory()
  75.     turtle.back()
  76.     turtle.back()
  77.     for k = 1, 16, 1 do
  78.         turtle.select(k)
  79.         dropNoFuel()
  80.     end
  81.  
  82.     turtle.forward()
  83.     turtle.forward()
  84. end
  85.  
  86. -------------------------------------------------------------
  87.  
  88. for i = 1, LENGTH, 1 do
  89.     checkFuel()
  90.  
  91.     for j = 1, LENGTH, 1 do
  92.         if seesCactus() == true then
  93.             breakCactus()
  94.         end
  95.  
  96.         if j < LENGTH then
  97.             turtle.forward()
  98.         end
  99.     end
  100.  
  101.     if i < LENGTH then
  102.         turnSwitch(i)
  103.         turtle.forward()
  104.         turnSwitch(i)
  105.     else
  106.         homeFromEnd()
  107.         dumpInventory()
  108.     end
  109. end
  110.  
  111. -- enderlein
Add Comment
Please, Sign In to add comment