Advertisement
Blackhome

BucketLavaCollector

Jan 10th, 2025 (edited)
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | Gaming | 0 0
  1.  
  2. local arg1, arg2 = ...
  3. local move = require("move1")
  4. local inspect = require("inspect1")
  5. local collectLava = require("collectLava1")
  6.  
  7. local maxDistance = 35
  8. if arg2 then
  9.     maxDistance = tonumber(arg2)
  10. end
  11.  
  12. local lineEndMove = 1
  13. if arg1 then
  14.     if tonumber(arg1) == 1 then
  15.         lineEndMove = 1
  16.     elseif tonumber(arg1) == 2 then
  17.         lineEndMove = 2
  18.     else
  19.         lineEndMove = 3
  20.     end
  21. end
  22.  
  23. function saveData(fileName, data)
  24.     local file = fs.open(fileName, "w") -- Datei im Schreibmodus öffnen
  25.     if file then
  26.         file.write(textutils.serialize(data)) -- Tabelle in die Datei schreiben
  27.         file.close()
  28.         print("Daten gespeichert in:", fileName)
  29.     else
  30.         print("Fehler beim Speichern der Daten.")
  31.     end
  32. end
  33.  
  34. function loadData(fileName)
  35.     if fs.exists(fileName) then
  36.         local file = fs.open(fileName, "r") -- Datei im Lesemodus öffnen
  37.         if file then
  38.             local content = file.readAll()
  39.             file.close()
  40.             return textutils.unserialize(content) -- String in Tabelle umwandeln
  41.         end
  42.     else
  43.         print("Datei nicht gefunden:", fileName)
  44.     end
  45.     return nil -- Standardwert, wenn Datei nicht existiert
  46. end
  47.  
  48. local function selectBucket()
  49.     for i=1, 16, 1 do
  50.         local count = turtle.getItemCount(i)
  51.         if count > 0 then
  52.             turtle.select(i)
  53.             local itemDetail = turtle.getItemDetail()
  54.             if itemDetail then
  55.                 if itemDetail.name == "minecraft:bucket" then
  56.                     return true
  57.                 end
  58.             end
  59.         end
  60.     end
  61.     return false
  62. end
  63.  
  64. local function moreEmpty()
  65.     for i=1, 16, 1 do
  66.         local count = turtle.getItemCount(i)
  67.         if count == 0 then
  68.             return true
  69.         end
  70.     end
  71.     return false
  72. end
  73.  
  74. local function tryOffloadItems()
  75.     local bChest = inspect.Forward({"minecraft:chest"})
  76.     if not bChest then
  77.         return
  78.     end
  79.     for i=1, 16, 1 do
  80.         local numItems = turtle.getItemCount(i)
  81.         if numItems > 0 then
  82.             turtle.select(i)
  83.             local itemDetail = turtle.getItemDetail()
  84.  
  85.             if itemDetail then
  86.                 if not (itemDetail.name == "minecraft:bucket") then
  87.                     turtle.drop()
  88.                 end
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. function LavaTilBottom()
  95.     local cnt = 0
  96.     local bFull = false
  97.     while not turtle.detectDown() do
  98.         if not moreEmpty() then
  99.             if selectBucket() then
  100.                 if not (turtle.getItemCount(turtle.getSelectedSlot()) == 1) then
  101.                     bFull = true
  102.                     break
  103.                 end
  104.             else
  105.                 bFull = true
  106.                 break
  107.             end
  108.         end
  109.  
  110.         if selectBucket() then
  111.             turtle.placeDown()
  112.             move.Down()
  113.             cnt = cnt + 1
  114.         else
  115.             bFull = true
  116.             break
  117.         end
  118.     end
  119.  
  120.     while cnt > 0 do
  121.         move.Up()
  122.         cnt = cnt - 1
  123.     end
  124.     return bFull
  125. end
  126.  
  127. local cnt = 0
  128. local loadedData = loadData("lastPosition.txt")
  129. if loadedData then
  130.     while cnt < tonumber(loadedData.lastPosition) do
  131.         turtle.forward()
  132.         cnt = cnt + 1
  133.     end
  134. end
  135. local bFinishedLine = false
  136.  
  137. while (not LavaTilBottom()) and maxDistance > cnt do
  138.     if turtle.forward() then
  139.         cnt = cnt + 1
  140.     else
  141.         local dataToSave = { lastPosition = 0 }
  142.         bFinishedLine = true
  143.         saveData("lastPosition.txt", dataToSave)
  144.         print("No more Lava to collect")
  145.         break
  146.     end
  147. end
  148. if maxDistance == cnt then
  149.     local dataToSave = { lastPosition = 0 }
  150.     bFinishedLine = true
  151.     saveData("lastPosition.txt", dataToSave)
  152.     print("Collected all the Lava along the way")
  153. end
  154. if not bFinishedLine then
  155.     local dataToSave = { lastPosition = cnt }
  156.     saveData("lastPosition.txt", dataToSave)
  157. end
  158.  
  159. turtle.turnLeft()
  160. turtle.turnLeft()
  161.  
  162. while cnt > 0 do
  163.     turtle.forward()
  164.     cnt = cnt - 1
  165. end
  166.  
  167. tryOffloadItems()
  168. turtle.turnLeft()
  169. turtle.turnLeft()
  170.  
  171. if bFinishedLine then
  172.     if lineEndMove == 1 then
  173.         turtle.turnRight()
  174.         turtle.forward()
  175.         turtle.turnLeft()
  176.     elseif lineEndMove == 2 then
  177.         turtle.turnLeft()
  178.         turtle.forward()
  179.         turtle.turnRight()
  180.     end
  181. end
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement