Advertisement
Blackhome

StorageUserInput

Jun 19th, 2025 (edited)
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | Gaming | 0 0
  1. -- pastebin get 3hBAMT1d UserInput
  2.  
  3. -- === User Interface für Lagerabfrage ===
  4. local STORAGE_DISK_LABEL = "Data Storage"
  5. local USER_DISK_LABEL = "User Task"
  6.  
  7. -- === Utility-Funktionen ===
  8. local function getDiskByLabel(label)
  9.     for _, side in ipairs(peripheral.getNames()) do
  10.         if peripheral.getType(side) == "drive" then
  11.             local disk = peripheral.wrap(side)
  12.             if disk.getDiskLabel() == label then
  13.                 return disk, disk.getMountPath()
  14.             end
  15.         end
  16.     end
  17.     return nil, nil
  18. end
  19.  
  20.  
  21. local function loadStoredItems()
  22.     local _, path = getDiskByLabel(STORAGE_DISK_LABEL)
  23.     if not path then
  24.         error("Storage Disk nicht gefunden.")
  25.     end
  26.     local filePath = fs.combine(path, "storedItems.txt")
  27.     if not fs.exists(filePath) then
  28.         error("Keine gespeicherten Items vorhanden.")
  29.     end
  30.     local file = fs.open(filePath, "r")
  31.     local data = textutils.unserialize(file.readAll())
  32.     file.close()
  33.     return data or {}
  34. end
  35.  
  36. local function getTaskList(path)
  37.     if not fs.exists(path) then return {} end
  38.     local file = fs.open(path, "r")
  39.     local data = textutils.unserialize(file.readAll())
  40.     file.close()
  41.     return data or {}
  42. end
  43.  
  44. local function saveTaskList(path, taskList)
  45.     local file = fs.open(path, "w")
  46.     file.write(textutils.serialize(taskList))
  47.     file.close()
  48. end
  49.  
  50. local function addTaskToList(path, task)
  51.     local tasks = getTaskList(path)
  52.     table.insert(tasks, task)
  53.     saveTaskList(path, tasks)
  54. end
  55.  
  56. local function saveTaskToUserDisk(task)
  57.     local _, path = getDiskByLabel(USER_DISK_LABEL)
  58.     if not path then
  59.         error("User Disk nicht gefunden.")
  60.     end
  61.     local filePath = fs.combine(path, "task_list.txt")
  62.  
  63.     addTaskToList(filePath, task)
  64. end
  65.  
  66. function isTaskSlotFree()
  67.     local disk, path = getDiskByLabel(USER_DISK_LABEL)
  68.  
  69.     if not path then
  70.         error("No disk detected under the computer!")
  71.         return false
  72.     end
  73.  
  74.     local fullPath = fs.combine(path, "task_list.txt")
  75.     local taskList = getTaskList(fullPath)
  76.  
  77.     if #taskList == 0 then
  78.         return true
  79.     end
  80.     return false
  81. end
  82.  
  83. -- === Eingabebehandlung ===
  84. local function classifyInput(input)
  85.     if input:find(":") then
  86.         return "itemName"
  87.     elseif input:match("^[A-Z]") then
  88.         return "displayName"
  89.     else
  90.         return "bareItemName"
  91.     end
  92. end
  93.  
  94. -- === Hauptprogramm ===
  95. local function main()
  96.     if not isTaskSlotFree() then
  97.         sleep(0.5)
  98.         return
  99.     end
  100.     term.clear()
  101.     term.setCursorPos(1, 1)
  102.     print("Was möchtest du aus dem Lager holen?")
  103.     io.write("> ")
  104.     local userInput = read()
  105.  
  106.     local inputType = classifyInput(userInput)
  107.     local storedItems = loadStoredItems()
  108.  
  109.     local matchedItem = nil
  110.  
  111.     for _, item in ipairs(storedItems) do
  112.         if inputType == "displayName" and item.itemDisplayName == userInput then
  113.             matchedItem = item
  114.             break
  115.         elseif inputType == "itemName" and item.itemName == userInput then
  116.             matchedItem = item
  117.             break
  118.         elseif inputType == "bareItemName" and item.itemName == "minecraft:" .. userInput then
  119.             matchedItem = item
  120.             break
  121.         end
  122.     end
  123.  
  124.     if not matchedItem then
  125.         print("Item nicht im Lager gefunden.")
  126.         return
  127.     end
  128.  
  129.     local available = matchedItem.itemCount - (matchedItem.itemOutgoingAmount or 0)
  130.     print("Verfügbare Anzahl von \"" .. matchedItem.itemDisplayName .. "\": " .. available)
  131.  
  132.     io.write("Wie viele möchtest du?: ")
  133.     local requested = tonumber(read())
  134.  
  135.     if not requested or requested <= 0 then
  136.         print("Ungültige Anzahl.")
  137.         return
  138.     elseif requested > available then
  139.         print("Nicht genügend im Lager vorhanden.")
  140.         return
  141.     end
  142.  
  143.     -- Task erzeugen
  144.     local task = {
  145.         taskType = "get_items",
  146.         itemName = matchedItem.itemName,
  147.         itemDisplayName = matchedItem.itemDisplayName,
  148.         itemCount = requested,
  149.         itemStackSize = matchedItem.stackSize
  150.     }
  151.  
  152.     saveTaskToUserDisk(task)
  153.  
  154.     print("Task erfolgreich erstellt.")
  155.     sleep(1)
  156. end
  157.  
  158. while true do
  159.     main()
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement