hhhzzzsss

obsidian.lua

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