Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin get 3hBAMT1d UserInput
- -- === User Interface für Lagerabfrage ===
- local STORAGE_DISK_LABEL = "Data Storage"
- local USER_DISK_LABEL = "User Task"
- -- === Utility-Funktionen ===
- local function getDiskByLabel(label)
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "drive" then
- local disk = peripheral.wrap(side)
- if disk.getDiskLabel() == label then
- return disk, disk.getMountPath()
- end
- end
- end
- return nil, nil
- end
- local function loadStoredItems()
- local _, path = getDiskByLabel(STORAGE_DISK_LABEL)
- if not path then
- error("Storage Disk nicht gefunden.")
- end
- local filePath = fs.combine(path, "storedItems.txt")
- if not fs.exists(filePath) then
- error("Keine gespeicherten Items vorhanden.")
- end
- local file = fs.open(filePath, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- local function getTaskList(path)
- if not fs.exists(path) then return {} end
- local file = fs.open(path, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data or {}
- end
- local function saveTaskList(path, taskList)
- local file = fs.open(path, "w")
- file.write(textutils.serialize(taskList))
- file.close()
- end
- local function addTaskToList(path, task)
- local tasks = getTaskList(path)
- table.insert(tasks, task)
- saveTaskList(path, tasks)
- end
- local function saveTaskToUserDisk(task)
- local _, path = getDiskByLabel(USER_DISK_LABEL)
- if not path then
- error("User Disk nicht gefunden.")
- end
- local filePath = fs.combine(path, "task_list.txt")
- addTaskToList(filePath, task)
- end
- function isTaskSlotFree()
- local disk, path = getDiskByLabel(USER_DISK_LABEL)
- if not path then
- error("No disk detected under the computer!")
- return false
- end
- local fullPath = fs.combine(path, "task_list.txt")
- local taskList = getTaskList(fullPath)
- if #taskList == 0 then
- return true
- end
- return false
- end
- -- === Eingabebehandlung ===
- local function classifyInput(input)
- if input:find(":") then
- return "itemName"
- elseif input:match("^[A-Z]") then
- return "displayName"
- else
- return "bareItemName"
- end
- end
- -- === Hauptprogramm ===
- local function main()
- if not isTaskSlotFree() then
- sleep(0.5)
- return
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("Was möchtest du aus dem Lager holen?")
- io.write("> ")
- local userInput = read()
- local inputType = classifyInput(userInput)
- local storedItems = loadStoredItems()
- local matchedItem = nil
- for _, item in ipairs(storedItems) do
- if inputType == "displayName" and item.itemDisplayName == userInput then
- matchedItem = item
- break
- elseif inputType == "itemName" and item.itemName == userInput then
- matchedItem = item
- break
- elseif inputType == "bareItemName" and item.itemName == "minecraft:" .. userInput then
- matchedItem = item
- break
- end
- end
- if not matchedItem then
- print("Item nicht im Lager gefunden.")
- return
- end
- local available = matchedItem.itemCount - (matchedItem.itemOutgoingAmount or 0)
- print("Verfügbare Anzahl von \"" .. matchedItem.itemDisplayName .. "\": " .. available)
- io.write("Wie viele möchtest du?: ")
- local requested = tonumber(read())
- if not requested or requested <= 0 then
- print("Ungültige Anzahl.")
- return
- elseif requested > available then
- print("Nicht genügend im Lager vorhanden.")
- return
- end
- -- Task erzeugen
- local task = {
- taskType = "get_items",
- itemName = matchedItem.itemName,
- itemDisplayName = matchedItem.itemDisplayName,
- itemCount = requested,
- itemStackSize = matchedItem.stackSize
- }
- saveTaskToUserDisk(task)
- print("Task erfolgreich erstellt.")
- sleep(1)
- end
- while true do
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement