Advertisement
Blackhome

Settings Manager

May 2nd, 2025 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.96 KB | Gaming | 0 0
  1. -- Settings Writer for Storage System (mit Taskliste)
  2. -- Platziert über dem Disk Drive mit der "Settings"-Disk
  3.  
  4. local SETTINGS_LABEL = "Settings"
  5. local COORD_FILE = "initial_coords.txt"
  6. local TASK_FILE = "task_list.txt" -- zentrale Taskliste auf der Settings-Disk
  7.  
  8. -- Funktion: Hole Disk-Laufwerk mit Label "Settings"
  9. function getSettingsDisk()
  10.     for _, side in ipairs(peripheral.getNames()) do
  11.         if peripheral.getType(side) == "drive" then
  12.             local disk = peripheral.wrap(side)
  13.             if disk.getDiskLabel() == SETTINGS_LABEL then
  14.                 return disk, disk.getMountPath()
  15.             end
  16.         end
  17.     end
  18.     return nil, nil
  19. end
  20.  
  21. -- Funktion: Überprüft, ob initial_coords.txt bereits existiert
  22. function coordsFileExists()
  23.     local _, path = getSettingsDisk()
  24.     if not path then return false end
  25.     return fs.exists(fs.combine(path, COORD_FILE))
  26. end
  27.  
  28. -- Funktion: Fragt den Nutzer nach einer Koordinate (in Form "x y z")
  29. function askForCoords(promptText)
  30.     while true do
  31.         print(promptText)
  32.         local input = read()
  33.         local x, y, z = input:match("(-?%d+)%s+(-?%d+)%s+(-?%d+)")
  34.         if x and y and z then
  35.             return tonumber(x), tonumber(y), tonumber(z)
  36.         else
  37.             print("Ungültiges Format. Bitte erneut eingeben als: x y z")
  38.         end
  39.     end
  40. end
  41.  
  42. -- Funktion: Speichert die beiden Koordinaten in initial_coords.txt
  43. function saveInitialCoords(x1, y1, z1, x2, y2, z2)
  44.     local _, path = getSettingsDisk()
  45.     if not path then error("Settings-Disk nicht gefunden!") end
  46.  
  47.     local fullPath = fs.combine(path, COORD_FILE)
  48.     local file = fs.open(fullPath, "w")
  49.     file.writeLine(string.format("%d,%d,%d", x1, y1, z1))
  50.     file.writeLine(string.format("%d,%d,%d", x2, y2, z2))
  51.     file.close()
  52.     print("Koordinaten erfolgreich gespeichert.")
  53. end
  54.  
  55. -- Funktion: Lädt Taskliste von Disk
  56. function loadTaskList(filePath)
  57.     if not fs.exists(filePath) then return {} end
  58.     local file = fs.open(filePath, "r")
  59.     local content = file.readAll()
  60.     file.close()
  61.     local data = textutils.unserialize(content)
  62.     return data or {}
  63. end
  64.  
  65. -- Funktion: Fügt Task zur Liste hinzu und speichert sie wieder
  66. function appendTaskToList(filePath, taskData)
  67.     local tasks = loadTaskList(filePath)
  68.     table.insert(tasks, taskData)
  69.     local file = fs.open(filePath, "w")
  70.     file.write(textutils.serialize(tasks))
  71.     file.close()
  72.     print("Neuer Task wurde erfolgreich gespeichert.")
  73. end
  74.  
  75. -- Funktion: Wiederholt Menü für Task-Erstellung
  76. function showTaskMenu(filePath)
  77.     while true do
  78.         print()
  79.         print("Was möchtest du tun?")
  80.         print("[1] Kisten per Task erstellen lassen")
  81.         print("[2] Bereits vorhandene Kisten initialisieren")
  82.         print("[3] Bestandsaufnahme durchführen")
  83.         print("[4] Beenden")
  84.  
  85.         write("Auswahl: ")
  86.         local choice = read()
  87.  
  88.         if choice == "1" or choice == "2" then
  89.             local x, y, z = askForCoords("Position der ersten Kiste eingeben (x y z):")
  90.             local taskType = (choice == "1") and "create_chests" or "initialize_chests"
  91.             local task = {
  92.                 taskType = taskType,
  93.                 x = x,
  94.                 y = y,
  95.                 z = z
  96.             }
  97.             appendTaskToList(filePath, task)
  98.  
  99.         elseif choice == "3" then
  100.             local task = {
  101.                 taskType = "check_inventory"
  102.             }
  103.             appendTaskToList(filePath, task)
  104.  
  105.         elseif choice == "4" then
  106.             print("Beende das Programm.")
  107.             break
  108.  
  109.         else
  110.             print("Ungültige Eingabe. Bitte wähle 1-4.")
  111.         end
  112.     end
  113. end
  114.  
  115. -- === Hauptprogramm ===
  116. print("Starte Settings-Setup...")
  117.  
  118. local _, path = getSettingsDisk()
  119. while not path do
  120.     print("Warte auf eingelegte Settings-Disk...")
  121.     sleep(1)
  122.     _, path = getSettingsDisk()
  123. end
  124.  
  125. local taskFilePath = fs.combine(path, TASK_FILE)
  126.  
  127. if coordsFileExists() then
  128.     print("initial_coords.txt existiert bereits.")
  129. else
  130.     print("Keine initial_coords.txt gefunden. Bitte Koordinaten eingeben.")
  131.     local x1, y1, z1 = askForCoords("Start-Koordinate eingeben (x y z):")
  132.     local x2, y2, z2 = askForCoords("End-Koordinate eingeben (x y z):")
  133.  
  134.     print()
  135.     print("Sollen die Kisten jetzt manuell erstellt werden oder per Task?")
  136.     print("[1] Kisten existieren bereits")
  137.     print("[2] Kisten sollen per Task erstellt werden")
  138.     write("Auswahl (1 oder 2): ")
  139.     local choice = read()
  140.  
  141.     if choice == "2" then
  142.         local task = {
  143.             taskType = "create_chests",
  144.             x = x1,
  145.             y = y1,
  146.             z = z1
  147.         }
  148.         appendTaskToList(taskFilePath, task)
  149.     else
  150.         print("Kein Task erstellt. Es wird davon ausgegangen, dass die Kisten vorhanden sind.")
  151.     end
  152.  
  153.     saveInitialCoords(x1, y1, z1, x2, y2, z2)
  154. end
  155.  
  156. -- Starte interaktives Menü zur Erstellung weiterer Tasks
  157. showTaskMenu(taskFilePath)
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement