Advertisement
Blackhome

StorageUserInput

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