hhhzzzsss

treefarm.lua

Aug 16th, 2023 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.38 KB | None | 0 0
  1. local xpos = -1
  2. local ypos = -1
  3. local zpos = -1
  4. local dir = -1
  5.  
  6. local function fastFail(success)
  7.     if not success then
  8.         print("An unexpected error has occured! Aborting.")
  9.         exit()
  10.     end
  11. end
  12.  
  13. local function refuel(amount)
  14.     local fuelLevel = turtle.getFuelLevel()
  15.     if fuelLevel == "unlimited" then
  16.         return true
  17.     end
  18.  
  19.     turtle.select(16)
  20.     while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
  21.         if not turtle.refuel(1) then
  22.             return false
  23.         end
  24.     end
  25.  
  26.     if turtle.getFuelLevel() < amount then
  27.         return false
  28.     else
  29.         return true
  30.     end
  31. end
  32.  
  33. local function waitForFuel(amount)
  34.     if not refuel(amount) then
  35.         print("Please put fuel in slot 16")
  36.         while not refuel(amount) do
  37.             sleep(0.5)
  38.         end
  39.         print("Received sufficient fuel. Continuing...")
  40.     end
  41. end
  42.  
  43. local function selectSapling()
  44.     for i=1, 15 do
  45.         itemDetail = turtle.getItemDetail(i)
  46.         if itemDetail and itemDetail.name == "terrestria:hemlock_sapling" then
  47.             turtle.select(i)
  48.             return true
  49.         end
  50.     end
  51.     return false
  52. end
  53.  
  54. local function countSaplings()
  55.     num = 0
  56.     for i=1, 15 do
  57.         itemDetail = turtle.getItemDetail(i)
  58.         if itemDetail and itemDetail.name == "terrestria:hemlock_sapling" then
  59.             num = num + itemDetail.count
  60.         end
  61.     end
  62.     return num
  63. end
  64.  
  65. local function waitForSapling(amount)
  66.     if countSaplings() < amount then
  67.         print("Please provide more hemlock saplings")
  68.         while countSaplings() < amount do
  69.             sleep(0.5)
  70.         end
  71.         print("Received saplings. Continuing...")
  72.     end
  73.     selectSapling()
  74. end
  75.  
  76. local function waitForDrop()
  77.     if turtle.getItemCount() > 0 and not turtle.drop() then
  78.         print("Please make room for turtle to drop")
  79.         while turtle.getItemCount() > 0 and not turtle.drop() do
  80.             sleep(0.5)
  81.         end
  82.         print("Turtle successfully dropped. Continuing...")
  83.     end
  84. end
  85.  
  86. local function waitForDropDown()
  87.     if turtle.getItemCount() > 0 and not turtle.dropDown() then
  88.         print("Please make room for turtle to drop")
  89.         while turtle.getItemCount() > 0 and not turtle.dropDown() do
  90.             sleep(0.5)
  91.         end
  92.         print("Turtle successfully dropped. Continuing...")
  93.     end
  94. end
  95.  
  96. local function getBlock()
  97.     hasBlock, data = turtle.inspect()
  98.     if hasBlock then
  99.         return data.name
  100.     else
  101.         return "minecraft:air"
  102.     end
  103. end
  104.  
  105. local function getBlockDown()
  106.     hasBlock, data = turtle.inspectDown()
  107.     if hasBlock then
  108.         return data.name
  109.     else
  110.         return "minecraft:air"
  111.     end
  112. end
  113.  
  114. local function getBlockUp()
  115.     hasBlock, data = turtle.inspectUp()
  116.     if hasBlock then
  117.         return data.name
  118.     else
  119.         return "minecraft:air"
  120.     end
  121. end
  122.  
  123. local function isImpermeable(name)
  124.     if name == "minecraft:stone_bricks" or name == "minecraft:dirt" or name == "minecraft:chest" or name == "minecraft:barrel" then
  125.         return true
  126.     else
  127.         return false
  128.     end
  129. end
  130.  
  131. local function tryForwards()
  132.     waitForFuel(1)
  133.     -- if not turtle.detect() then
  134.     --     turtle.suck()
  135.     -- end
  136.     while not turtle.forward() do
  137.         if isImpermeable(getBlock()) then
  138.             return false
  139.         end
  140.         if turtle.detect() then
  141.             if not turtle.dig() then
  142.                 return false
  143.             end
  144.         elseif not turtle.attack() then
  145.             sleep(0.5)
  146.         end
  147.     end
  148.  
  149.     if dir == 0 then
  150.         xpos = xpos + 1
  151.     elseif dir == 1 then
  152.         zpos = zpos + 1
  153.     elseif dir == 2 then
  154.         xpos = xpos - 1
  155.     elseif dir == 3 then
  156.         zpos = zpos - 1
  157.     end
  158.  
  159.     return true
  160. end
  161.  
  162. local function tryDown()
  163.     -- if not turtle.detectDown() then
  164.     --     turtle.suckDown()
  165.     -- end
  166.     waitForFuel(1)
  167.     while not turtle.down() do
  168.         if isImpermeable(getBlockDown()) then
  169.             return false
  170.         end
  171.         if turtle.detectDown() then
  172.             if not turtle.digDown() then
  173.                 return false
  174.             end
  175.         elseif not turtle.attackDown() then
  176.             sleep(0.5)
  177.         end
  178.     end
  179.  
  180.     if dir >= 0 then
  181.         ypos = ypos - 1
  182.     end
  183.  
  184.     return true
  185. end
  186.  
  187. local function tryUp()
  188.     -- if not turtle.detectUp() then
  189.     --     turtle.suckUp()
  190.     -- end
  191.     waitForFuel(1)
  192.     while not turtle.up() do
  193.         if isImpermeable(getBlockUp()) then
  194.             return false
  195.         end
  196.         if turtle.detectUp() then
  197.             if not turtle.digUp() then
  198.                 return false
  199.             end
  200.         elseif not turtle.attackUp() then
  201.             sleep(0.5)
  202.         end
  203.     end
  204.  
  205.     if dir >= 0 then
  206.         ypos = ypos + 1
  207.     end
  208.  
  209.     return true
  210. end
  211.  
  212. local function turnLeft()
  213.     -- if not turtle.detect() then
  214.     --     turtle.suck()
  215.     -- end
  216.     turtle.turnLeft()
  217.     dir = (dir + 1) % 4
  218. end
  219.  
  220. local function turnRight()
  221.     -- if not turtle.detect() then
  222.     --     turtle.suck()
  223.     -- end
  224.     turtle.turnRight()
  225.     dir = (dir + 3) % 4
  226. end
  227.  
  228. local function recenter()
  229.     waitForFuel(128)
  230.  
  231.     while not isImpermeable(getBlockDown()) do
  232.         tryDown()
  233.     end
  234.  
  235.     while getBlockDown() ~= "minecraft:chest" do
  236.         if not tryForwards() then
  237.             turtle.turnLeft()
  238.         end
  239.     end
  240.  
  241.     while getBlock() ~= "minecraft:barrel" do
  242.         turtle.turnLeft()
  243.     end
  244.  
  245.     xpos = 2
  246.     zpos = 0
  247.     ypos = 0
  248.     dir = 0
  249. end
  250.  
  251. recenter()
  252.  
  253. while true do
  254.     -- Refuel
  255.     turtle.select(16)
  256.     waitForDrop()
  257.     turtle.suck()
  258.     if not refuel(1024) then
  259.         print("Waiting for fuel...")
  260.         sleep(0.5)
  261.         while not refuel(1024) do
  262.             sleep(0.5)
  263.             waitForDrop()
  264.             turtle.suck()
  265.         end
  266.     end
  267.  
  268.     turnLeft()
  269.    
  270.     while getBlock() == "terrestria:hemlock_sapling" do
  271.         sleep(0.5)
  272.     end
  273.  
  274.     fastFail(tryForwards())
  275.     turnRight()
  276.  
  277.     for i=0, 63 do
  278.         if turtle.detectUp() then
  279.             for j=0, 3 do
  280.                 turtle.dig()
  281.                 turnRight()
  282.                 turtle.dig()
  283.                 turnRight()
  284.                 turnRight()
  285.                 fastFail(tryForwards())
  286.             end
  287.         end
  288.         if i ~= 63 then
  289.             fastFail(tryUp())
  290.         end
  291.     end
  292.  
  293.     for i=0, 61 do
  294.         fastFail(tryDown())
  295.     end
  296.        
  297.     waitForSapling(4)
  298.     for j=0, 3 do
  299.         turnLeft()
  300.         fastFail(tryForwards())
  301.         waitForSapling(1)
  302.         turtle.placeDown()
  303.     end
  304.     turnRight()
  305.     fastFail(tryForwards())
  306.     turnLeft()
  307.     fastFail(tryDown())
  308.  
  309.     for i=1, 15 do
  310.         itemDetail = turtle.getItemDetail(i)
  311.         if itemDetail and itemDetail.name ~= "terrestria:hemlock_sapling" then
  312.             turtle.select(i)
  313.             waitForDropDown()
  314.         end
  315.     end
  316.  
  317.     for i=1, 12 do
  318.         itemDetail = turtle.getItemDetail(i)
  319.         if itemDetail and itemDetail.name == "terrestria:hemlock_sapling" then
  320.             turtle.select(i)
  321.             turtle.transferTo(13)
  322.             turtle.transferTo(14)
  323.             turtle.transferTo(15)
  324.             waitForDropDown()
  325.         end
  326.     end
  327.  
  328.     turtle.select(16)
  329.     if not turtle.refuel(0) then
  330.         waitForDropDown()
  331.     end
  332. end
Advertisement
Add Comment
Please, Sign In to add comment