Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- File to store the list of items
- local filename = "items_list.lua"
- local turtleName = "turtle_2"
- local shaka = require("API")
- local fromInventories = shaka.readFile("inventories") or {}
- -- Save list to file
- local function saveList(list)
- local file = fs.open(filename, "w")
- if file then
- file.write(textutils.serialize(list))
- file.close()
- else
- error("Failed to save list.")
- end
- end
- -- Read list from file
- local function readList()
- if not fs.exists(filename) then return {} end
- local file = fs.open(filename, "r")
- local content = file.readAll()
- file.close()
- local success, result = pcall(load("return " .. content))
- return success and result or {}
- end
- -- Display the list with minimal UI
- local function displayList(list, startIndex, selectedIndex)
- term.clear()
- term.setCursorPos(1, 1)
- print("W/S to scroll, [e]: edit, [i]: update from turtle, [q]: quit:")
- local endIndex = math.min(startIndex + 9, #list)
- for i = startIndex, endIndex do
- local item = list[i]
- if i == selectedIndex then
- term.setTextColor(colors.yellow)
- print("> " .. item.name .. " (x" .. item.amount .. ")")
- else
- term.setTextColor(colors.white)
- print(" " .. item.name .. " (x" .. item.amount .. ")")
- end
- end
- -- Display scrolling indicators
- term.setTextColor(colors.gray)
- if startIndex > 1 then
- print("\n(Scroll up for more...)")
- end
- if endIndex < #list then
- print("\n(Scroll down for more...)")
- end
- term.setTextColor(colors.white)
- end
- -- Scan turtle inventory and update the list
- local function scanTurtleInventory(list)
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item then
- local found = false
- for _, entry in ipairs(list) do
- if entry.name == item.name then
- entry.amount = entry.amount + item.count
- found = true
- break
- end
- end
- if not found then
- table.insert(list, {name = item.name, amount = item.count})
- end
- end
- end
- saveList(list) -- Save updated list
- end
- -- Main program logic
- local function main()
- local list = readList()
- local startIndex = 1
- local selectedIndex = 1
- while true do
- displayList(list, startIndex, selectedIndex)
- local event, key = os.pullEvent("key")
- if key == keys.q then
- os.reboot()
- break -- Quit the program
- elseif key == keys.w or key == keys.up then
- if selectedIndex > 1 then
- selectedIndex = selectedIndex - 1
- if selectedIndex < startIndex then
- startIndex = startIndex - 1
- end
- end
- elseif key == keys.s or key == keys.down then
- if selectedIndex < #list then
- selectedIndex = selectedIndex + 1
- if selectedIndex > startIndex + 9 then
- startIndex = startIndex + 1
- end
- end
- elseif key == keys.i then
- term.clear()
- term.setCursorPos(1, 1)
- print("Scanning turtle inventory and updating list...")
- scanTurtleInventory(list)
- print("Inventory updated.")
- sleep(1)
- elseif key == keys.e then
- term.clear()
- term.setCursorPos(1, 1)
- local item = list[selectedIndex]
- shaka.changeColors(colors.gray, colors.black)
- term.clearLine()
- print("Editing item: ")
- shaka.changeColors(colors.black, colors.yellow)
- print(shaka.prettyName(item.name))
- shaka.changeColors(colors.black, colors.white)
- shaka.nextLine()
- write("Enter new amount: ")
- shaka.changeColors(colors.black, colors.yellow)
- sleep(0.3)
- local newAmount = tonumber(read())
- if newAmount and newAmount > 0 then
- item.amount = newAmount
- saveList(list)
- shaka.changeColors(colors.black, colors.green)
- print("Item updated.")
- else
- shaka.changeColors(colors.black, colors.red)
- print("Invalid amount.")
- end
- sleep(1)
- end
- end
- end
- -- Run the program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement