Advertisement
Inksaver

lavaRefuel.lua (refuel or drain lava) 2021/08/15

Apr 28th, 2020 (edited)
1,934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. version = 20230308.1930
  2. --[[
  3.     pastebin get kFZsXu99 lavaRefuel.lua
  4.     Last edited: see version YYYYMMDD.HHMM
  5.     Will auto-download clsTurtle.lua
  6.     Used to refuel a turtle on a lava lake or drain lava
  7.     Use: 'lavaRefuel' in turtle terminal
  8. ]]
  9.  
  10. function clear()
  11.     term.clear()
  12.     term.setCursorPos(1, 1)
  13. end
  14.  
  15. function checkLibs(libDir, filename)
  16.     local fileExists = false
  17.     if fs.exists(libDir) then
  18.         if not fs.isDir(libDir) then
  19.             fs.move(libDir, libDir.."Renamed")
  20.             fs.makeDir(libDir)
  21.         end
  22.     else
  23.         fs.makeDir(libDir)
  24.     end
  25.     if fs.exists(fs.combine(libDir, filename)) or fs.exists(fs.combine(libDir, filename..".lua")) then
  26.         fileExists = true
  27.     end
  28.     return fileExists
  29. end
  30.  
  31. function getLavaStrip()
  32.     local block, blockType = T:isWaterOrLava("down") -- will automatically fill bucket with lava and refuel
  33.     local start = true
  34.     local length = 0
  35.     local lavaPresent = false
  36.     -- while lava below, just started or moved forward < 3
  37.     while block == "minecraft:lava" or block == "minecraft:flowing_lava" or start or length < 3 do --will automatically fill bucket with lava
  38.         start = false
  39.        
  40.         if T:forward(1) then
  41.             length = length + 1
  42.         end
  43.         block, blockType = T:isWaterOrLava("down")
  44.        
  45.         print("Block below: "..tostring(block))
  46.         if block == "minecraft:lava" or block == "minecraft:flowing_lava" then
  47.             lavaPresent = true
  48.         end
  49.     end
  50.     T:go("L2F"..length + 1)
  51.     block, blockType = T:isWaterOrLava("down")
  52.     while block == "minecraft:lava" or block == "minecraft:flowing_lava" do
  53.         T:forward(1)
  54.         block, blockType = T:isWaterOrLava("down")
  55.     end
  56.     turtle.back()
  57.     T:go("L2")
  58.     return lavaPresent
  59. end
  60.  
  61. function main()
  62.     local side = ''
  63.     while side == '' do
  64.         clear()
  65.         print("Place me in front of lava")
  66.         print("Which side has more lava? (L C R)")
  67.         print("Choose C for a single strip")
  68.         side = read()
  69.         if side == 'l' or side =='L' then
  70.             side = 'L'
  71.         elseif side == 'r' or side =='R' then
  72.             side = 'R'
  73.         else
  74.             side = 'C'
  75.         end
  76.     end
  77.     local doContinue = true
  78.     if not checkLibs("lib", "clsTurtle") then
  79.         -- use pastebin get to download clsTurtle to libs folder
  80.         print("Missing clsTurtle.lua in libs directory")
  81.         print("Attempting to obtain from Pastebin...")
  82.         if shell.run("pastebin","get","tvfj90gK","lib/clsTurtle.lua") then
  83.             print("clsTurtle.lua installed from Pastebin")
  84.         else
  85.             print("failed to install clsTurtle.lua from Pastebin")
  86.             doContinue = false
  87.         end
  88.     end
  89.     sleep(2)
  90.     if doContinue then
  91.         clear()
  92.         print("Current fuel: "..turtle.getFuelLevel().." / "..turtle.getFuelLimit())
  93.         T = require("lib.clsTurtle"):new()
  94.         local lavaSlot = T:getItemSlot("minecraft:lava_bucket", -1)
  95.         if lavaSlot > 0 then
  96.             turtle.select(lavaSlot)
  97.             T:refuel(0) -- 0=force refuel
  98.         end
  99.         T:checkInventoryForItem({"minecraft:bucket", "minecraft:lava_bucket"}, {1,1}, true)
  100.         T:refuel(0) -- 0=force refuel
  101.         local width = 0
  102.         local lavaPresent = getLavaStrip()
  103.         if side ~= 'C' then -- not a single strip
  104.             while lavaPresent do
  105.                 width = width + 1
  106.                 if side == 'R' then -- do strip on the right
  107.                     T:go("R1F1L1")
  108.                 else
  109.                     T:go("L1F1R1")
  110.                 end
  111.                 lavaPresent = getLavaStrip()
  112.                 if turtle.getFuelLevel() >= turtle.getFuelLimit() then
  113.                     lavaPresent = false
  114.                     print("Max fuel "..turtle.getFuelLimit() .. " achieved")
  115.                 end
  116.             end
  117.             if width <= 0 then
  118.                 width = 1
  119.             end
  120.            
  121.             if side == 'R' then
  122.                 T:go("L1F"..width.."R1")
  123.             else
  124.                 T:go("R1F"..width.."L1")
  125.             end
  126.         end
  127.     else
  128.         print("Add missing files and restart")
  129.     end
  130. end
  131.  
  132. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement