Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shaka = require("API")
- fromInventories = shaka.readFile("inventories")
- resetTimer = 0
- disableMouse = false
- -- Connect peripherals
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for i, side in ipairs(sides) do
- local device = peripheral.getType(side)
- if device == "modem" then
- rednet.open(side)
- elseif device == "monitor" then
- monitor = peripheral.wrap(side)
- elseif device then
- local wrapper = peripheral.wrap(side)
- end
- end
- -- Load the wanted items from the file
- if fs.exists("wanteditems.lua") then
- wantedItems = dofile("wanteditems.lua")
- else
- wantedItems = {}
- local file = fs.open("wanteditems.lua", "w")
- file.write("return {}")
- file.close()
- end
- monitor.clear()
- monitor.setTextScale(0.5)
- window = shaka.centerWindow(30, 10, colors.gray, colors.yellow, monitor)
- shaka.centerText("Currently in menu, idling.", 5, window)
- -- Function to add an item to the wanted items table
- function addItem()
- sleep(0.2)
- resetScreen()
- term.setTextColor(colors.yellow)
- print("Add item to wanted items:")
- term.setTextColor(colors.white)
- print("Enter computer ID:")
- local id = tonumber(read())
- if not id or id < 1 then
- resetScreen()
- term.setTextColor(colors.red)
- print("Invalid computer ID.")
- term.setTextColor(colors.gray)
- print("\n\nPress any key to try again..")
- term.setTextColor(colors.white)
- os.pullEvent("key")
- return
- end
- print("Enter item name:")
- local itemName = read()
- if not itemName or itemName == "" then
- resetScreen()
- term.setTextColor(colors.red)
- print("Item name must not be empty.")
- term.setTextColor(colors.gray)
- print("\n\nPress any key to try again..")
- term.setTextColor(colors.white)
- os.pullEvent("key")
- return
- end
- print("Enter item count:")
- local itemCount = tonumber(read())
- if not itemCount or itemCount < 1 then
- resetScreen()
- term.setTextColor(colors.red)
- print("Invalid item count.")
- term.setTextColor(colors.gray)
- print("\n\nPress any key to try again..")
- term.setTextColor(colors.white)
- os.pullEvent("key")
- return
- end
- if not wantedItems[id] then
- wantedItems[id] = {}
- end
- -- Check if item already exists for this computer ID
- for _, v in ipairs(wantedItems[id]) do
- if v[1] == itemName then
- term.setTextColor(colors.red)
- textutils.slowPrint("Item "..itemName.. " already exists for this ID.")
- sleep(1)
- return false
- end
- end
- table.insert(wantedItems[id], { itemName, itemCount })
- saveWantedItems()
- term.setTextColor(colors.green)
- shaka.nextLine()
- textutils.slowPrint("Added "..itemName)
- sleep(1)
- end
- -- Function to save the wanted items
- function saveWantedItems()
- for id, items in pairs(wantedItems) do
- for _, item in ipairs(items) do
- if not id or not item[1] or not item[2] then
- resetScreen()
- term.setTextColor(colors.red)
- print("Computer ID, item name, and item count must not be empty.")
- term.setTextColor(colors.gray)
- print("\n\nPress any key to try again..")
- term.setTextColor(colors.white)
- event, key = os.pullEvent("key")
- os.reboot()
- end
- end
- end
- file = fs.open("wanteditems.lua", "w")
- file.write("return {\n")
- for id, items in pairs(wantedItems) do
- file.write((" [%d] = {\n"):format(id))
- for _, item in ipairs(items) do
- file.write((" {%q, %d},\n"):format(item[1], item[2]))
- end
- file.write(" },\n")
- end
- file.write("}\n")
- file.close()
- end
- function viewWantedItems()
- local sortedIDs = {}
- for id in pairs(wantedItems) do
- table.insert(sortedIDs, id)
- end
- table.sort(sortedIDs)
- for _, id in ipairs(sortedIDs) do
- local items = wantedItems[id]
- local screensize, width = term.getSize()
- screensize = width - 5
- local pages = math.ceil(#items / screensize)
- for page = 1, pages do
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- print("Wanted items:")
- term.setTextColor(colors.lightGray)
- print(("Computer ID: %d"):format(id))
- term.setTextColor(colors.white)
- for i = (page - 1) * screensize + 1, math.min(page * screensize, #items) do
- local item = items[i]
- print(("%d. %d x %s"):format(i, item[2], item[1]))
- end
- term.setTextColor(colors.gray)
- print("Enter item number to edit (space to skip):")
- term.setTextColor(colors.white)
- local event, key = os.pullEvent("key")
- if key ~= keys.space then
- local itemNum = tonumber(read())
- if itemNum == nil then
- term.setTextColor(colors.red)
- shaka.nextLine()
- textutils.slowPrint("Invalid input..")
- sleep(1)
- os.reboot()
- end
- if itemNum > 0 and itemNum <= #items then
- local item = items[itemNum]
- print("Enter new item count or 0 to delete:")
- local newCount = tonumber(read())
- if newCount == 0 then
- table.remove(items, itemNum)
- term.setTextColor(colors.red)
- shaka.nextLine()
- textutils.slowPrint("Removed " ..item[1])
- sleep(1)
- saveWantedItems()
- return
- else
- items[itemNum][2] = newCount
- term.setTextColor(colors.green)
- shaka.nextLine()
- textutils.slowPrint("Changed "..item[1].. " to "..newCount)
- sleep(1)
- saveWantedItems()
- return
- end
- else
- term.setTextColor(colors.red)
- shaka.nextLine()
- textutils.slowPrint("Invalid input..")
- sleep(1)
- os.reboot()
- end
- end
- end
- end
- end
- function displayLoadingBar(monitor, percent)
- monitor.setTextColor(colors.gray)
- monitor.setBackgroundColor(colors.black)
- local width = monitor.getSize()
- local barWidth = math.min(math.floor(width * percent / 100), width - 2)
- if percent >= 80 then
- monitor.setTextColor(colors.lime)
- elseif percent >= 70 then
- monitor.setTextColor(colors.green)
- elseif percent >= 40 then
- monitor.setTextColor(colors.yellow)
- elseif percent >= 15 then
- monitor.setTextColor(colors.orange)
- else
- monitor.setTextColor(colors.red)
- end
- monitor.setCursorPos(1,8)
- monitor.write("[")
- for i=1,barWidth do
- monitor.write("=")
- end
- for i=barWidth+1,width-2 do
- monitor.write(" ")
- end
- monitor.write("]")
- monitor.setCursorPos(1, 9)
- monitor.clearLine()
- monitor.write(tostring(math.floor(percent)) .. "%")
- end
- function defragmentInventories()
- for _, invName in ipairs(fromInventories) do
- term.clear()
- term.setCursorPos(1,1)
- local invWrap = peripheral.wrap(invName)
- local inventory = invWrap.list()
- shaka.changeColors(colors.green, colors.black)
- term.clearLine()
- print("Defragging inventory " .._.. " of " ..#fromInventories)
- monitor.setCursorPos(1,1)
- monitor.clear()
- -- monitor.setBackgroundColor(colors.green)
- shaka.changeColors(colors.green, colors.black, monitor)
- monitor.clearLine()
- monitor.write("Defragging inventory " .._.. " of " ..#fromInventories)
- for slot, item in pairs(inventory) do
- local percentage = (slot/#inventory) * 100
- displayLoadingBar(term, percentage)
- displayLoadingBar(monitor, percentage)
- prettyInv = shaka.prettyName(invName)
- prettyName = shaka.prettyName(item.name)
- for i = 1, #fromInventories do
- prettyTarget = shaka.prettyName(fromInventories[i])
- ---monitor
- monitor.setCursorPos(1, 3)
- shaka.changeColors(colors.lightBlue, colors.black, monitor)
- monitor.clearLine()
- monitor.write("Source inv: "..prettyInv)
- shaka.nextLine(monitor)
- shaka.changeColors(colors.yellow, colors.black, monitor)
- monitor.clearLine()
- monitor.write("Moving item: "..prettyName)
- monitor.setCursorPos(1, 5)
- shaka.changeColors(colors.orange, colors.black, monitor)
- monitor.clearLine()
- monitor.write("Target inv: "..prettyTarget)
- --terminal
- term.setCursorPos(1, 3)
- shaka.changeColors(colors.lightBlue, colors.black)
- term.clearLine()
- term.write("Source inv: "..prettyInv)
- shaka.nextLine()
- shaka.changeColors(colors.yellow, colors.black)
- term.clearLine()
- term.write("Moving item: "..prettyName)
- term.setCursorPos(1, 5)
- shaka.changeColors(colors.orange, colors.black)
- term.clearLine()
- term.write("Target inv: "..prettyTarget)
- moved = invWrap.pushItems(fromInventories[i], slot)
- if moved ~= 0 then
- break
- end
- end
- end
- shaka.changeColors(colors.black, colors.white)
- shaka.changeColors(colors.black, colors.white, monitor)
- end
- end
- function manualSortTable(t)
- local selected = 1
- local quit = false
- while not quit do
- -- clear screen and print current state of table
- term.clear()
- term.setCursorPos(1,1)
- shaka.changeColors(colors.gray, colors.white)
- term.clearLine()
- print("Choose inventory order: Deselect[Enter], Save['S']")
- shaka.changeColors(colors.black, colors.white)
- term.setCursorPos(1, 3)
- for i, v in ipairs(t) do
- if i == selected then
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- print(i.."> " .. v)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- else
- print(i.." " .. v)
- end
- end
- -- wait for user input
- local event, key = os.pullEvent("key")
- if key == keys.enter then
- if selected ~= nil then
- while selected do
- -- redraw the table with the current selection
- term.clear()
- term.setCursorPos(1, 1)
- shaka.changeColors(colors.gray, colors.white)
- term.clearLine()
- print("Choose inventory order: Select[Enter], Save['S']")
- shaka.changeColors(colors.black, colors.white)
- term.setCursorPos(1, 3)
- for i, v in ipairs(t) do
- if i == selected then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.yellow)
- print(i.."> " .. v)
- term.setTextColor(colors.white)
- else
- print(i.." " .. v)
- end
- end
- -- wait for user input to move the selection
- local event2, key2 = os.pullEvent("key")
- if key2 == keys.up then
- if selected > 1 then
- selected = selected - 1
- end
- elseif key2 == keys.down then
- if selected < #t then
- selected = selected + 1
- end
- elseif key2 == keys.enter then
- -- break out of the selection loop
- break
- elseif key2 == keys.s then
- return
- end
- end
- end
- elseif key == keys.up then
- -- move selection up
- if selected and selected > 1 then
- t[selected], t[selected - 1] = t[selected - 1], t[selected]
- selected = selected - 1
- end
- elseif key == keys.down then
- -- move selection down
- if selected and selected < #t then
- t[selected], t[selected + 1] = t[selected + 1], t[selected]
- selected = selected + 1
- end
- elseif key == keys.s then
- -- quit the function
- quit = true
- end
- end
- end
- -- User interface to add an item
- function menu(eventName)
- shaka.changeColors(colors.black, colors.white)
- term.clear()
- term.setTextColor(colors.white)
- local numberedOptions = 1
- for i = 5, 13, 2 do
- if i == 5 then numberedOptions = 1
- elseif i == 7 then numberedOptions = 2
- elseif i == 9 then numberedOptions = 3
- elseif i == 11 then numberedOptions = 4
- elseif i == 13 then numberedOptions = 5
- end
- term.setCursorPos(1, i)
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- term.write(numberedOptions..".")
- end
- term.setTextColor(colors.black)
- shaka.centerText("Manually add new item.", 5)
- shaka.centerText("Computer ID overview.", 7)
- shaka.centerText("Search for a specific item.", 9)
- shaka.centerText("Return to main menu.", 11)
- shaka.centerText("Defragment inventories.", 13)
- shaka.changeColors(colors.black, colors.gray)
- term.setCursorPos(1,1)
- print("\nClick your choice or enter number.")
- event, button, x, choice = os.pullEvent(eventName)
- if event == "key" then
- if button == keys.one then choice = "add"
- elseif button == keys.two then choice = "view"
- elseif button == keys.three then choice = "search"
- elseif button == keys.four then choice = "reboot"
- elseif button == keys.five then choice = "defrag"
- end
- end
- if event == "mouse_click" then
- if choice == 5 then choice = "add"
- elseif choice == 7 then choice = "view"
- elseif choice == 9 then choice = "search"
- elseif choice == 11 then choice = "reboot"
- elseif choice == 13 then choice = "defrag"
- end
- end
- if event == "mouse_click" or event == "key" then
- if choice == "add" then
- addItem()
- elseif choice == "view" then
- viewWantedItems()
- elseif choice == "search" then
- searchItem()
- elseif choice == "reboot" then
- os.reboot()
- elseif choice == "defrag" then
- shaka.clearScreen()
- shaka.changeColors(colors.gray, colors.white)
- term.clear()
- shaka.centerText("Please choose priority of inventories.", 5)
- shaka.centerText("Items will first try to go to 1, then 2, etc..", 7)
- shaka.centerText("Press any key to continue..", 10)
- os.pullEvent("key")
- term.setBackgroundColor(colors.black)
- manualSortTable(fromInventories)
- defragmentInventories()
- shaka.changeColors(colors.black,colors.green)
- print("\nDone!")
- shaka.writeFile("inventories", fromInventories)
- sleep(1)
- os.reboot()
- else
- if choice == nil then choice = button end
- print("Invalid selection("..choice..").")
- sleep(1)
- end
- end
- end
- function resetScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function searchItem()
- resetScreen()
- term.setTextColor(colors.yellow)
- print("Enter item name or item count:\n")
- term.setTextColor(colors.lime)
- sleep(0.1)
- local searchTerm = read()
- if searchTerm == "" then
- term.setTextColor(colors.red)
- print("Cancelling.")
- sleep(1)
- return
- end
- found = {}
- if tonumber(searchTerm) ~= nil then -- search by item count
- for i, wantedItem in pairs(wantedItems) do
- for k, v in pairs(wantedItem) do
- if v[2] == tonumber(searchTerm) then
- table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
- end
- end
- end
- else -- search by item name
- for i, wantedItem in pairs(wantedItems) do
- for k, v in pairs(wantedItem) do
- if string.find(string.lower(v[1]), string.lower(searchTerm)) then
- table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
- end
- end
- end
- end
- term.setTextColor(colors.white)
- if #found == 0 then
- term.setTextColor(colors.red)
- print("Item not in stockpile list.")
- sleep(1)
- elseif #found == 1 then
- local item = found[1]
- resetScreen()
- term.setTextColor(colors.white)
- print(("ID%d: %s x %d"):format(item.computerID, item.itemName, item.itemCount))
- term.setTextColor(colors.lightGray)
- print("\nEnter new item count or 0 to delete:\n")
- term.setTextColor(colors.white)
- local newCount = tonumber(read())
- if newCount == 0 then
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- table.remove(wantedItem, k)
- term.setTextColor(colors.red)
- shaka.nextLine()
- textutils.slowPrint("Removed "..item.itemName.. ".")
- sleep(1)
- break
- end
- end
- end
- end
- elseif newCount ~= nil then
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- wantedItem[k][2] = newCount
- term.setTextColor(colors.green)
- shaka.nextLine()
- textutils.slowPrint("Changed "..item.itemName.. " to "..newCount)
- sleep(1)
- break
- end
- end
- end
- end
- else
- term.setTextColor(colors.red)
- shaka.nextLine()
- textutils.slowPrint("Invalid input..")
- sleep(1)
- end
- saveWantedItems()
- else
- resetScreen()
- term.setTextColor(colors.yellow)
- print("Multiple results found. Please choose one:\n")
- term.setTextColor(colors.white)
- local pageSize = 15 -- number of items to display on each page
- local startIndex = 1 -- index of the first item to display
- local endIndex = math.min(#found, pageSize) -- index of the last item to display
- local function displayItems()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- print("Scroll and enter item #")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1, 3)
- for i = startIndex, endIndex do
- local item = found[i]
- local itemText = ("%d.ID%d: %s x %d"):format(i, item.computerID, item.itemName, item.itemCount)
- term.setTextColor(colors.yellow)
- io.write(i .. ".")
- term.setTextColor(colors.gray)
- io.write("ID" .. item.computerID .. ": ")
- term.setTextColor(colors.white)
- io.write(item.itemName .. " ")
- term.setTextColor(colors.lightGray)
- print("x " .. item.itemCount)
- term.setTextColor(colors.white)
- end
- end
- displayItems()
- while true do
- local event, param1, param2 = os.pullEvent()
- if event == "mouse_scroll" then
- -- Update the start and end indexes based on the scroll amount
- startIndex = startIndex + param1
- startIndex = math.max(startIndex, 1)
- endIndex = math.min(startIndex + pageSize - 1, #found)
- startIndex = math.max(startIndex, 1)
- displayItems()
- elseif event == "key" then
- if param1 == keys.down then
- startIndex = startIndex + 1
- startIndex = math.max(startIndex, 1)
- endIndex = math.min(startIndex + pageSize - 1, #found)
- startIndex = math.max(startIndex, 1)
- displayItems()
- elseif param1 == keys.up then
- startIndex = startIndex - 1
- startIndex = math.max(startIndex, 1)
- endIndex = math.min(startIndex + pageSize - 1, #found)
- startIndex = math.max(startIndex, 1)
- displayItems()
- else
- choice = tonumber(read())
- if choice == nil then
- term.setTextColor(colors.red)
- print("Invalid input. Please try again.")
- sleep(1)
- return
- end
- break
- end
- end
- end
- if choice >= 1 and choice <= #found then
- local item = found[choice]
- resetScreen()
- term.setTextColor(colors.white)
- print(("ID%d: %s x %d"):format(item.computerID, item.itemName, item.itemCount))
- term.setTextColor(colors.lightGray)
- print("\nEnter new item count or 0 to delete:\n")
- term.setTextColor(colors.white)
- local newCount = tonumber(read())
- if newCount ~= 0 and newCount ~= nil then
- -- If newCount is a number, update the item count
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- wantedItem[k][2] = newCount
- term.setTextColor(colors.green)
- shaka.nextLine()
- textutils.slowPrint("Changed "..item.itemName.. " to "..newCount)
- sleep(1)
- break
- end
- end
- end
- end
- elseif newCount == 0 then
- -- If newCount is "0", delete the item
- for i, wantedItem in pairs(wantedItems) do
- if i == item.computerID then
- for k, v in pairs(wantedItem) do
- if v[1] == item.itemName then
- table.remove(wantedItem, k)
- term.setTextColor(colors.red)
- shaka.nextLine()
- textutils.slowPrint("Removed "..item.itemName.. ".")
- sleep(1)
- break
- end
- end
- end
- end
- else
- -- If newCount is not a number or "0", display an error message and return
- term.setTextColor(colors.red)
- textutils.slowPrint("Invalid input.")
- sleep(1)
- return
- end
- -- Save the updated wantedItems table to file
- saveWantedItems()
- end
- end
- end
- while true do
- parallel.waitForAny(menu)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement