hhhzzzsss

sugarcane.lua

Aug 22nd, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.56 KB | None | 0 0
  1. local function fastFail(success)
  2.     if not success then
  3.         print("An unexpected error has occured! Aborting.")
  4.         exit()
  5.     end
  6. end
  7.  
  8. local function refuel(amount)
  9.     local fuelLevel = turtle.getFuelLevel()
  10.     if fuelLevel == "unlimited" then
  11.         return true
  12.     end
  13.  
  14.     turtle.select(16)
  15.     while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
  16.         if not turtle.refuel(1) then
  17.             return false
  18.         end
  19.     end
  20.  
  21.     if turtle.getFuelLevel() < amount then
  22.         return false
  23.     else
  24.         return true
  25.     end
  26. end
  27.  
  28. local function waitForFuel(amount)
  29.     if not refuel(amount) then
  30.         print("Please put fuel in slot 16")
  31.         while not refuel(amount) do
  32.             sleep(0.5)
  33.         end
  34.         print("Received sufficient fuel. Continuing...")
  35.     end
  36. end
  37.  
  38. local function selectItem(name)
  39.     for i=15, 1, -1 do
  40.         itemDetail = turtle.getItemDetail(i)
  41.         if itemDetail and itemDetail.name == name then
  42.             turtle.select(i)
  43.             return true
  44.         end
  45.     end
  46.     return false
  47. end
  48.  
  49. local function countItem(name)
  50.     num = 0
  51.     for i=1, 15 do
  52.         itemDetail = turtle.getItemDetail(i)
  53.         if itemDetail and itemDetail.name == name then
  54.             num = num + itemDetail.count
  55.         end
  56.     end
  57.     return num
  58. end
  59.  
  60. local function waitForItem(name, amount)
  61.     if countItem(name) < amount then
  62.         print("Please provide more " .. name)
  63.         while countItem(name) < amount do
  64.             sleep(0.5)
  65.         end
  66.         print("Received sufficient " .. name .. ". Continuing...")
  67.     end
  68.     selectItem(name)
  69. end
  70.  
  71. local function waitForDrop()
  72.     if turtle.getItemCount() > 0 and not turtle.drop() then
  73.         print("Please make room for turtle to drop")
  74.         while turtle.getItemCount() > 0 and not turtle.drop() do
  75.             sleep(0.5)
  76.         end
  77.         print("Turtle successfully dropped. Continuing...")
  78.     end
  79. end
  80.  
  81. local function waitForDropDown()
  82.     if turtle.getItemCount() > 0 and not turtle.dropDown() then
  83.         print("Please make room for turtle to drop")
  84.         while turtle.getItemCount() > 0 and not turtle.dropDown() do
  85.             sleep(0.5)
  86.         end
  87.         print("Turtle successfully dropped. Continuing...")
  88.     end
  89. end
  90.  
  91. local function waitForDropUp()
  92.     if turtle.getItemCount() > 0 and not turtle.dropUp() then
  93.         print("Please make room for turtle to drop")
  94.         while turtle.getItemCount() > 0 and not turtle.dropUp() do
  95.             sleep(0.5)
  96.         end
  97.         print("Turtle successfully dropped. Continuing...")
  98.     end
  99. end
  100.  
  101. local function containerRefuel(amount)
  102.     turtle.select(16)
  103.     waitForDropUp() -- Edit this line to when changing refuel direction
  104.     turtle.suckUp() -- Edit this line to when changing refuel direction
  105.     if not refuel(amount) then
  106.         print("Waiting for fuel...")
  107.         sleep(0.5)
  108.         while not refuel(amount) do
  109.             sleep(0.5)
  110.             waitForDropUp() -- Edit this line to when changing refuel direction
  111.             turtle.suckUp() -- Edit this line to when changing refuel direction
  112.         end
  113.     end
  114. end
  115.  
  116. local function getBlock()
  117.     local hasBlock, data = turtle.inspect()
  118.     if hasBlock then
  119.         return data.name
  120.     else
  121.         return "minecraft:air"
  122.     end
  123. end
  124.  
  125. local function getBlockDown()
  126.     local hasBlock, data = turtle.inspectDown()
  127.     if hasBlock then
  128.         return data.name
  129.     else
  130.         return "minecraft:air"
  131.     end
  132. end
  133.  
  134. local function getBlockUp()
  135.     local hasBlock, data = turtle.inspectUp()
  136.     if hasBlock then
  137.         return data.name
  138.     else
  139.         return "minecraft:air"
  140.     end
  141. end
  142.  
  143. local function tryForward()
  144.     waitForFuel(1)
  145.     while not turtle.forward() do
  146.         if getBlock() == "minecraft:glass_pane" then
  147.             return false
  148.         end
  149.         if turtle.detect() then
  150.             if not turtle.dig() then
  151.                 return false
  152.             end
  153.         elseif not turtle.attack() then
  154.             sleep(0.5)
  155.         end
  156.     end
  157.  
  158.     if getBlockDown() == "minecraft:sugar_cane" then
  159.         turtle.digDown()
  160.     end
  161.  
  162.     return true
  163. end
  164.  
  165. local function repeatForward(amount)
  166.     for i=1, amount do
  167.         fastFail(tryForward())
  168.     end
  169. end
  170.  
  171. local function recenter()
  172.     while getBlockUp() ~= "minecraft:barrel" do
  173.         if not tryForward() then
  174.             turtle.turnLeft()
  175.         end
  176.     end
  177.     while turtle.detect() and getBlock() ~= "minecraft:sugar_cane" do
  178.         turtle.turnLeft()
  179.     end
  180. end
  181.  
  182. local function deposit()
  183.     for i=1, 15 do
  184.         if turtle.getItemCount(i) > 0 then
  185.             turtle.select(i)
  186.             waitForDropDown() -- Edit this line to when changing deposit direction
  187.         end
  188.     end
  189.     turtle.select(16)
  190.     if not turtle.refuel(0) then
  191.         waitForDropDown() -- Edit this line to when changing deposit direction
  192.     end
  193. end
  194.  
  195. while true do
  196.     recenter()
  197.     deposit()
  198.  
  199.     for i=1, 20*60 do
  200.         if getBlock() ~= "minecraft:sugar_cane" then
  201.             sleep(1)
  202.         else
  203.             break
  204.         end
  205.     end
  206.  
  207.     containerRefuel(1024)
  208.  
  209.     for i=0, 14 do
  210.         if i%3 == 0 then
  211.             fastFail(tryForward())
  212.             turtle.turnRight()
  213.             repeatForward(15)
  214.             turtle.turnLeft()
  215.         elseif i % 3 == 1 then
  216.             fastFail(tryForward())
  217.         else
  218.             fastFail(tryForward())
  219.             turtle.turnLeft()
  220.             repeatForward(15)
  221.             turtle.turnRight()
  222.         end
  223.     end
  224. end
Advertisement
Add Comment
Please, Sign In to add comment