Blackhome

BucketLavaCollector

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