Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --pastebin get hre4rgH7 startup
- --Global menuColor table
- local menuColors = {
- title = colors.lightBlue,
- menu = colors.purple,
- text = colors.white,
- highlight = colors.pink,
- --background = colors.black
- }
- --##############################################
- --Function definitions
- --Returns true if tbl contains "inventory", else false
- local function isInv(tbl)
- for i = 1, #tbl do
- if tbl[i] == "inventory" then
- return true
- end
- end
- return false
- end
- --returns true if 'name' ~= "up","down","left","right","forward","back"
- local function isNotAdjacentOrDump(name,dumpName)
- return not (name == dumpName or name == "up" or name == "down" or name == "left" or name == "right" or name == "forward" or name == "back")
- end
- --returns a table of wrapped inventories
- --takes in a table of peripheral names
- local function sortForInv(nameList,dumpName)
- local wrapList = {}
- for i = 1, #nameList do
- local curName = nameList[i]
- if isNotAdjacentOrDump(curName,dumpName) then
- local typeList = {peripheral.getType(curName)} -- = tbl of types
- if isInv(typeList) then
- wrapList[#wrapList+1] = peripheral.wrap(curName)
- end
- end
- end
- return wrapList
- end
- --returns table of all items in wrapped inventories
- --takes in a table of wrapped inventories
- local function getItemList(wrapList)
- local itemList = { name = "Item List"}
- for i = 1, #wrapList do
- local curInv = wrapList[i].list()
- for slot,item in pairs(curInv) do
- item.chestIndex = i
- item.slot = slot
- item.index = #itemList+1
- itemList[#itemList+1] = item
- end
- end
- return itemList
- end
- --takes in a numbered table of items, structured like this: tbl = { [1] = {name,count,slot,chetsID}, [..] = {..}, .. }
- --Displays a arrow key controlled menu using a provided menuPage
- --returns: int cursor, string menuChoice (either "search", "exit", or "select")
- local function openMenu(tbl,cursor)
- local cursor = cursor or 1
- local _,screenHeight = term.getSize()
- local pageLength = screenHeight - 3
- local maxPage = math.ceil(#tbl/(pageLength))
- local page = math.min( math.ceil( cursor / pageLength ) ,maxPage)
- term.clear()
- while true do
- local displayStartIndex = math.max( pageLength*(page-1)+1, 1 )
- local displayEndIndex = math.min( pageLength*(page), #tbl )
- local col = term.getTextColor()
- --clamp cursor to range of 1-displayEndIndex
- cursor = math.min(cursor,displayEndIndex)
- cursor = math.max(cursor,1)
- term.setCursorPos(1,1)
- if tbl.name then
- term.setTextColor(menuColors.title)
- print("<<< "..tbl.name.." >>>")
- end
- term.setTextColor(menuColors.text)
- --Display item options
- for i = displayStartIndex, displayEndIndex do
- local displayStr = ""
- local displayCount = ""
- if tbl[i] then
- displayStr = tbl[i].name
- --remove the prefix for item names
- local colonIndex = string.find(displayStr,":")
- if colonIndex then
- displayStr = displayStr:sub(colonIndex+1)
- end
- --capitalize the first character
- displayStr = displayStr:gsub("^%l", string.upper)
- --remove underscores
- displayStr = string.gsub(displayStr,"_"," ")
- if tbl[i].count then
- if tbl[i].count ~= 1 then
- displayCount = " : "..tostring(tbl[i].count)
- end
- end
- end
- if i == cursor then
- if tbl[i].color then
- term.setTextColor(tbl[i].color)
- else
- term.setTextColor(menuColors.highlight)
- end
- print(">"..(displayStr)..displayCount.."<")
- term.setTextColor(menuColors.text)
- else
- if tbl[i].color then
- term.setTextColor(tbl[i].color)
- end
- print(" "..(displayStr)..displayCount.." ")
- term.setTextColor(menuColors.text)
- end
- end
- term.setTextColor(menuColors.menu)
- print("Page "..page.." / "..maxPage)
- local _, key = os.pullEvent("key")
- if key == keys.up then
- --Move cursor up if it's not at first option
- if cursor > displayStartIndex then
- cursor = cursor - 1
- end
- elseif key == keys.down then
- --move cursor down if it's not at last option
- if cursor < displayEndIndex then
- cursor = cursor + 1
- end
- elseif key == keys.left then
- --move to previous page if not at first page
- if page > 1 then
- page = page - 1
- cursor = cursor - pageLength
- term.clear()
- end
- elseif key == keys.right then
- --move to next page if not at last page
- if page < maxPage then
- page = page + 1
- cursor = cursor + pageLength
- term.clear()
- end
- elseif key == keys.enter then
- --return the menu choice
- return cursor,"select"
- elseif key == keys.backspace then
- --exit the menu
- return cursor,"exit"
- else
- --player wants to search for a string
- return cursor,"search"
- end
- end
- end
- --iterates over itemList and updates the .index value of each index in the table
- local function updateIndex(itemList)
- for key,val in pairs(itemList) do
- if key ~= "name" then
- itemList[key].index = key
- end
- end
- end
- --takes in(String searchTarget, tbl itemList)
- --iterates over itemList and checks to see if itemList[i].name == searchTarget
- --returns table of all found items that match
- local function search(searchTarget,itemList)
- local formatedTarget = string.gsub(searchTarget," ","_")
- formatedTarget = string.lower(formatedTarget)
- local foundItems = {name = "Search: "..searchTarget}
- --Look through each item
- for i = 1, #itemList do
- --Check to see if current item matches item we are searching for
- if string.find( itemList[i].name, formatedTarget ) then
- --found an item that matches search. Add it to foundItems table and update total found item count
- foundItems[#foundItems+1] = itemList[i]
- end
- end
- return foundItems
- end
- --Takes everything in a specific chest and tries to push it into other peripheral inventories
- --returns true if dumped all items, false if failed to dump an item
- local function dump(outWrap,wrapList)
- term.clear()
- term.setTextColor(menuColors.title)
- term.setCursorPos(1,1)
- print("Dumping items...")
- local badName = peripheral.getName(outWrap)
- local itemList = outWrap.list()
- local namesList = {}
- local itemCount = 0
- for _,_ in pairs(itemList) do
- itemCount = itemCount + 1
- end
- for index,wrap in pairs(wrapList) do
- namesList[index] = peripheral.getName(wrap)
- end
- --CHANGE:
- --Make this smart. Deposit items in the first chest that contains a similar item. Go to next chest if full
- for i = 1, #namesList do
- local name = namesList[i]
- if name ~= badname then
- while itemCount > 0 do
- local full = false
- for slot,_ in pairs(itemList) do
- --.pushItems returns the amount of items moved
- if outWrap.pushItems(name, slot) == 0 then
- --curInv is full. go to next
- full = true
- break
- end
- end
- itemList = outWrap.list()
- itemCount = 0
- for _,_ in pairs(itemList) do
- itemCount = itemCount + 1
- end
- if itemCount == 0 then
- return true
- elseif full then
- --curInv is full. Go to next
- break
- end
- end
- end
- end
- if itemCount > 0 then
- return false
- end
- return true
- end
- local function dumpChestCheck(dumpName)
- --error checking to make sure dump chest exists
- local curNameList = peripheral.getNames()
- for i = 1, #curNameList do
- if dumpName == curNameList[i] then
- return dumpName
- end
- end
- local sortedNames = {name = "Inventory List"}
- for i = 1, #curNameList do
- local curName = curNameList[i]
- if isNotAdjacentOrDump(curName,dumpName) then
- local typeList = {peripheral.getType(curName)} -- = tbl of types
- if isInv(typeList) then
- sortedNames[#sortedNames+1] = {name = curNameList[i]}
- end
- end
- end
- while true do
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(menuColors.title)
- print("Please define a valid dump chest")
- print("Or type 'list' to pick from a list of inventories")
- term.setTextColor(menuColors.menu)
- dumpName = read()
- term.setTextColor(menuColors.text)
- if dumpName == "list" then
- local cursor,choice
- while choice ~= "exit" do
- cursor,choice = openMenu(sortedNames)
- if choice == "select" then
- return sortedNames[cursor].name
- end
- end
- else
- for i = 1, #sortedNames do
- if dumpName == sortedNames[i].name then
- return dumpName
- end
- end
- end
- end
- end
- --###################################################################
- --###################################################################
- --Main function
- local menuTbl = {
- name = "Main Menu",
- {name = "Items"},
- {name = "Dump"},
- {name = "Update Item/Chest List"},
- {name = "Settings"}
- }
- local colorOptions = {
- name = "Color List",
- {name = "White", color = 1},
- {name = "Orange", color = 2},
- {name = "Magenta", color = 4},
- {name = "Light Blue", color = 8},
- {name = "Yellow", color = 16},
- {name = "Lime", color = 32},
- {name = "Pink", color = 64},
- {name = "Gray", color = 128},
- {name = "Light Gray", color = 256},
- {name = "Cyan", color = 512},
- {name = "Purple", color = 1024},
- {name = "Blue", color = 2048},
- {name = "Brown", color = 4096},
- {name = "Green", color = 8192},
- {name = "Red", color = 16384},
- {name = "Black"}
- }
- local defaultSettings = {
- settingsMenu = {
- name = "Settings",
- {name = "Title Color", count = "Light Blue", menu = "colorOptions"},
- {name = "Menu Color", count = "Purple", menu = "colorOptions"},
- {name = "Text Color", count = "White", menu = "colorOptions"},
- {name = "Highlight Color", count = "Pink", menu = "colorOptions"},
- --{name = "Background Color", count = "Black", menu = "colorOptions"}
- {name = "Dump Chest", count = "minecraft:chest_0"},
- --{name = "Reset Settings To Default"},
- },
- menuColors = {
- title = colors.lightBlue,
- menu = colors.purple,
- text = colors.white,
- highlight = colors.pink,
- --background = colors.black
- }
- }
- local settingsMenu = {
- name = "Settings",
- {name = "Title Color", count = "Light Blue", menu = "colorOptions"},
- {name = "Menu Color", count = "Purple", menu = "colorOptions"},
- {name = "Text Color", count = "White", menu = "colorOptions"},
- {name = "Highlight Color", count = "Pink", menu = "colorOptions"},
- --{name = "Background Color", count = "Black", menu = "colorOptions"}
- {name = "Dump Chest", count = "minecraft:chest_0"},
- --{name = "Reset Settings To Default"},
- }
- --Load values and check if was interrupted in previous task
- local data = {}
- if( fs.exists("inventorySettings.txt") ) then
- local file = fs.open("inventorySettings.txt","r")
- data = textutils.unserialise(file.readAll())
- file.close()
- settingsMenu = data.settingsMenu
- menuColors = data.menuColors
- else
- data = {
- settingsMenu = settingsMenu,
- menuColors = menuColors
- }
- local file = fs.open("inventorySettings.txt","w")
- file.write(textutils.serialise(data))
- file.close()
- end
- --initialize variables from settings
- --error checking to make sure dumpChest exists
- --Get dump chest name from settings
- local test = dumpChestCheck(settingsMenu[5].count)
- if test ~= settingsMenu[5].count then
- --update save file
- settingsMenu[5].count = test
- data = {
- settingsMenu = settingsMenu,
- menuColors = menuColors
- }
- local file = fs.open("inventorySettings.txt","w")
- file.write(textutils.serialise(data))
- file.close()
- end
- local outChest = test
- local outWrap = peripheral.wrap(outChest)
- --remove dumpChest from inventory list
- local nameList = peripheral.getNames()
- for i = 1, #nameList do
- if nameList[i] == outChest then
- table.remove(nameList,i)
- break
- end
- end
- --finish setting up variables
- local wrapList = sortForInv(nameList,outChest)
- local itemList
- local needToUpdate = true
- while true do
- --outer menu
- term.clear()
- local cursor,menuChoice = openMenu(menuTbl)
- local mainChoice = menuTbl[cursor].name
- term.clear()
- if mainChoice == "Items" or menuChoice == "search" then
- local activeInv = itemList
- local activeSearch = false
- local searchKey = ""
- --check if search is coming from main menu
- if menuChoice == "search" then
- term.clear()
- term.setCursorPos(1,1)
- print("Enter search target:")
- searchKey = read()
- activeSearch = true
- if needToUpdate then
- term.setCursorPos(1,1)
- print("Getting item list...")
- itemList = getItemList(wrapList)
- needToUpdate = false
- term.clear()
- end
- activeInv = search(searchKey,itemList)
- elseif needToUpdate then
- term.setCursorPos(1,1)
- print("Getting item list...")
- itemList = getItemList(wrapList)
- activeInv = itemList
- needToUpdate = false
- term.clear()
- end
- --inside item menu
- while menuChoice ~= "exit" do
- cursor,menuChoice = openMenu(activeInv,cursor)
- if menuChoice == "select" then
- --pullItem(cursor,itemList)
- if activeInv[cursor] then
- local item = activeInv[cursor]
- local slot = item.slot
- local count = item.count
- local wrapFuncs = wrapList[item.chestIndex]
- --try to push 64 items. If items pushed ~= 0 then go into statement
- if wrapFuncs.pushItems(outChest,slot,64) ~= 0 then
- --needToUpdate = true
- --remove it from the list if stack less than 64 because we moved the whole stack, then update list
- if count <= 64 then
- table.remove(itemList, item.index)
- updateIndex(itemList)
- if activeSearch then
- --CHANGE:
- --make this more efficient by using an index table to point to outer itemList instead of creating a new itemList
- --HAVE to use search() method instead of updateIndex method because index has to be based on itemList, not activeInv
- table.remove(activeInv,cursor)
- --activeInv = search(searchKey,itemList)
- end
- else
- --items still remaining, update count
- itemList[item.index].count = count - 64
- if activeSearch then
- activeInv[cursor].count = count - 64
- end
- end
- end
- end
- elseif menuChoice == "search" then
- activeSearch = true
- term.clear()
- term.setCursorPos(1,1)
- print("Enter search target:")
- searchKey = read()
- activeInv = search(searchKey,itemList)
- term.clear()
- term.setCursorPos(1,1)
- print("Searching...")
- elseif menuChoice == "exit" then
- --if activeSearch = true then only go back to invList, not main menu
- if activeSearch then
- activeSearch = false
- activeInv = itemList
- menuChoice = ""
- end
- end
- end
- elseif mainChoice == "Dump" then
- needToUpdate = true
- --error checking to make sure dump chest exists
- local newDumpChest = dumpChestCheck(outChest)
- if newDumpChest ~= outChest then
- outChest = newDumpChest
- outWrap = peripheral.wrap(outChest)
- --update save file
- settingsMenu[cursor].count = outChest
- data = {
- settingsMenu = settingsMenu,
- menuColors = menuColors
- }
- local file = fs.open("inventorySettings.txt","w")
- file.write(textutils.serialise(data))
- file.close()
- end
- if not dump(outWrap,wrapList) then
- term.setCursorPos(1,1)
- print("Failed to dump all items!")
- print("Storage likely full")
- print("Press 'enter' to continue")
- read()
- nameList = peripheral.getNames()
- wrapList = sortForInv(nameList,outChest)
- end
- elseif mainChoice == "Update Item/Chest List" then
- nameList = peripheral.getNames()
- wrapList = sortForInv(nameList,outChest)
- term.setCursorPos(1,1)
- print("Getting item list...")
- itemList = getItemList(wrapList)
- needToUpdate = false
- elseif mainChoice == "Settings" then
- while menuChoice ~= "exit" do
- cursor,menuChoice = openMenu(settingsMenu)
- if menuChoice == "select" then
- --check to see if current option has .menu element
- if settingsMenu[cursor].menu then
- if settingsMenu[cursor].menu == "colorOptions" then
- --inside a specific color
- local toChange = cursor
- cursor,menuChoice = openMenu(colorOptions)
- if menuChoice == "select" then
- --[[
- local menuColors = {
- title = colors.lightBlue,
- menu = colors.purple,
- text = colors.white,
- highlight = colors.pink,
- background = colors.black
- }
- ]]
- settingsMenu[toChange].count = colorOptions[cursor].name
- print("settingsMenu["..toChange.."].count = "..colorOptions[cursor].name)
- local colorVal = 2^(cursor-1)
- if toChange == 1 then
- menuColors.title = colorVal
- print("title = "..colorVal)
- elseif toChange == 2 then
- menuColors.menu = colorVal
- print("menu = "..colorVal)
- elseif toChange == 3 then
- menuColors.text = colorVal
- print("text = "..colorVal)
- elseif toChange == 4 then
- menuColors.highlight = colorVal
- print("highlight = "..colorVal)
- --elseif cursor == 5 then
- -- menuColors.background = colorVal
- end
- --update saveFile
- data = {
- settingsMenu = settingsMenu,
- menuColors = menuColors
- }
- local file = fs.open("inventorySettings.txt","w")
- file.write(textutils.serialise(data))
- file.close()
- end
- end
- --[[
- elseif settingsMenu[cursor].name == "Reset Settings To Default" then
- print("Old settings:")
- print("Settings: "..textutils.serialize(settingsMenu))
- read()
- print("menu colors: "..textutils.serialize(menuColors))
- read()
- settingsMenu = defaultSettings.settingsMenu
- menuColors = defaultSettings.menuColors
- local file = fs.open("inventorySettings.txt","w")
- file.write(textutils.serialise(defaultSettings))
- file.close()
- print("Reset settings:")
- print("Settings: "..textutils.serialize(settingsMenu))
- read()
- print("menu colors: "..textutils.serialize(menuColors))
- read()
- ]]
- elseif settingsMenu[cursor].name == "Dump Chest" then
- local lineLen = 15--15 = #">Dump Chest< : "
- term.setCursorPos(lineLen,cursor+1)
- term.setTextColor(menuColors.menu)
- --update dump chest
- local newDumpChest = read(nil,nil,nil,tostring(settingsMenu[cursor].count))
- --error checking to make sure dump chest exists
- if newDumpChest ~= outChest then
- newDumpChest = dumpChestCheck(newDumpChest)
- end
- outChest = newDumpChest
- outWrap = peripheral.wrap(outChest)
- --update save file
- settingsMenu[cursor].count = outChest
- data = {
- settingsMenu = settingsMenu,
- menuColors = menuColors
- }
- local file = fs.open("inventorySettings.txt","w")
- file.write(textutils.serialise(data))
- file.close()
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement