Advertisement
jig487

TEST_invStartup

May 22nd, 2024 (edited)
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.19 KB | None | 0 0
  1. --pastebin get ZUHXDfFb startup
  2.  
  3. --Global menuColor table
  4. local menuColors = {
  5.     title = colors.lightBlue,
  6.     menu = colors.purple,
  7.     text = colors.white,
  8.     highlight = colors.pink,
  9.     --background = colors.black
  10. }
  11.  
  12. --##############################################
  13. --Function definitions
  14.  
  15. --Returns true if tbl contains "inventory", else false
  16. local function isInv(tbl)
  17.     for i = 1, #tbl do
  18.         if tbl[i] == "inventory" then
  19.             return true
  20.         end
  21.     end
  22.     return false
  23. end
  24.  
  25. --returns true if 'name' ~= "up","down","left","right","forward","back"
  26. local function isNotAdjacentOrDump(name,dumpName)
  27.     return not (name == dumpName or name == "up" or name == "down" or name == "left" or name == "right" or name == "forward" or name == "back")
  28. end
  29.  
  30. --returns a table of wrapped inventories
  31. --takes in a table of peripheral names
  32. local function sortForInv(nameList,dumpName)
  33.     local wrapList = {}
  34.     for i = 1, #nameList do
  35.         local curName = nameList[i]
  36.         if isNotAdjacentOrDump(curName,dumpName) then
  37.             local typeList = {peripheral.getType(curName)} -- = tbl of types
  38.             if isInv(typeList) then
  39.                 wrapList[#wrapList+1] = peripheral.wrap(curName)
  40.             end
  41.         end
  42.     end
  43.     return wrapList
  44. end
  45.  
  46. --returns table of all items in wrapped inventories
  47. --takes in a table of wrapped inventories
  48. local function getItemList(wrapList)
  49.     local itemList = { name = "Item List"}
  50.     for i = 1, #wrapList do
  51.         local curInv = wrapList[i].list()
  52.  
  53.         for slot,item in pairs(curInv) do
  54.             item.chestIndex = i
  55.             item.slot = slot
  56.             item.index = #itemList+1
  57.             itemList[#itemList+1] = item
  58.         end
  59.     end
  60.  
  61.     return itemList
  62. end
  63.  
  64. --takes in a numbered table of items, structured like this: tbl = {  [1] = {name,count,slot,chetsID}, [..] = {..}, ..  }
  65. --Displays a arrow key controlled menu using a provided menuPage
  66. --returns: int cursor, string menuChoice (either "search", "exit", or "select")
  67. local function openMenu(tbl,cursor)
  68.     local cursor = cursor or 1
  69.  
  70.     local _,screenHeight = term.getSize()
  71.     local pageLength = screenHeight - 3
  72.  
  73.     local maxPage = math.ceil(#tbl/(pageLength))
  74.     local page = math.min( math.ceil( cursor / pageLength ) ,maxPage)
  75.  
  76.     term.clear()
  77.     while true do
  78.         local displayStartIndex = math.max( pageLength*(page-1)+1, 1 )
  79.         local displayEndIndex = math.min( pageLength*(page), #tbl )
  80.         local col = term.getTextColor()
  81.  
  82.         --clamp cursor to range of 1-displayEndIndex
  83.         cursor = math.min(cursor,displayEndIndex)
  84.         cursor = math.max(cursor,1)
  85.  
  86.         term.setCursorPos(1,1)
  87.         if tbl.name then
  88.             term.setTextColor(menuColors.title)
  89.             print("<<< "..tbl.name.." >>>")
  90.         end
  91.         term.setTextColor(menuColors.text)
  92.  
  93.         --Display item options
  94.         for i = displayStartIndex, displayEndIndex do
  95.  
  96.             local displayStr = ""
  97.             local displayCount = ""
  98.             if tbl[i] then
  99.                 displayStr = tbl[i].name
  100.  
  101.                 --remove the prefix for item names
  102.                 local colonIndex = string.find(displayStr,":")
  103.                 if colonIndex then
  104.                     displayStr = displayStr:sub(colonIndex+1)
  105.                 end
  106.                 --capitalize the first character
  107.                 displayStr = displayStr:gsub("^%l", string.upper)
  108.  
  109.                 --remove underscores
  110.                 displayStr = string.gsub(displayStr,"_"," ")
  111.  
  112.                 if tbl[i].count then
  113.                     if tbl[i].count ~= 1 then
  114.                         displayCount = " : "..tostring(tbl[i].count)
  115.                     end
  116.                 end
  117.             end
  118.  
  119.             if i == cursor then
  120.                 if tbl[i].color then
  121.                     term.setTextColor(tbl[i].color)
  122.                 else
  123.                     term.setTextColor(menuColors.highlight)
  124.                 end
  125.                 print(">"..(displayStr)..displayCount.."<")
  126.                 term.setTextColor(menuColors.text)
  127.             else
  128.                 if tbl[i].color then
  129.                     term.setTextColor(tbl[i].color)
  130.                 end
  131.                 print(" "..(displayStr)..displayCount.." ")
  132.                 term.setTextColor(menuColors.text)
  133.             end
  134.         end
  135.  
  136.         term.setTextColor(menuColors.menu)
  137.         print("Page "..page.." / "..maxPage)
  138.         local _, key = os.pullEvent("key")
  139.         if key == keys.up then
  140.             --Move cursor up if it's not at first option
  141.             if cursor > displayStartIndex then
  142.                 cursor = cursor - 1
  143.             end
  144.         elseif key == keys.down then
  145.             --move cursor down if it's not at last option
  146.             if cursor < displayEndIndex then
  147.                 cursor = cursor + 1
  148.             end
  149.         elseif key == keys.left then
  150.             --move to previous page if not at first page
  151.             if page > 1 then
  152.                 page = page - 1
  153.                 cursor = cursor - pageLength
  154.                 term.clear()
  155.             end
  156.         elseif key == keys.right then
  157.             --move to next page if not at last page
  158.             if page < maxPage then
  159.                 page = page + 1
  160.                 cursor = cursor + pageLength
  161.                 term.clear()
  162.             end
  163.         elseif key == keys.enter then
  164.             --return the menu choice
  165.             return cursor,"select"
  166.         elseif key == keys.backspace then
  167.             --exit the menu
  168.             return cursor,"exit"
  169.         else
  170.             --player wants to search for a string
  171.             return cursor,"search"
  172.         end
  173.     end
  174. end
  175.  
  176. --iterates over itemList and updates the .index value of each index in the table
  177. local function updateIndex(itemList)
  178.     for key,val in pairs(itemList) do
  179.         if key ~= "name" then
  180.             itemList[key].index = key
  181.         end
  182.     end
  183. end
  184.  
  185. --takes in(String searchTarget, tbl itemList)
  186. --iterates over itemList and checks to see if itemList[i].name == searchTarget
  187. --returns table of all found items that match
  188. local function search(searchTarget,itemList)
  189.     searchTarget = string.lower(searchTarget)
  190.     local foundItems = {name = "Search: "..searchTarget}
  191.  
  192.     --Look through each item
  193.     for i = 1, #itemList do
  194.         --Check to see if current item matches item we are searching for
  195.         if string.find( itemList[i].name, searchTarget ) then
  196.             --found an item that matches search. Add it to foundItems table and update total found item count
  197.             foundItems[#foundItems+1] = itemList[i]
  198.         end
  199.     end
  200.  
  201.     return foundItems
  202. end
  203.  
  204. --Takes everything in a specific chest and tries to push it into other peripheral inventories
  205. --returns true if dumped all items, false if failed to dump an item
  206. local function dump(outWrap,wrapList)
  207.     term.clear()
  208.     term.setTextColor(menuColors.title)
  209.     term.setCursorPos(1,1)
  210.     print("Dumping items...")
  211.  
  212.     local badName = peripheral.getName(outWrap)
  213.     local itemList = outWrap.list()
  214.     local namesList = {}
  215.     local itemCount = 0
  216.     for _,_ in pairs(itemList) do
  217.         itemCount = itemCount + 1
  218.     end
  219.     for index,wrap in pairs(wrapList) do
  220.         namesList[index] = peripheral.getName(wrap)
  221.     end
  222.  
  223.     --CHANGE:
  224.     --Make this smart. Deposit items in the first chest that contains a similar item. Go to next chest if full
  225.     for i = 1, #namesList do
  226.         local name = namesList[i]
  227.  
  228.  
  229.         if name ~= badname then
  230.             while itemCount > 0 do
  231.                 local full = false
  232.                 for slot,_ in pairs(itemList) do
  233.                     --.pushItems returns the amount of items moved
  234.                     if outWrap.pushItems(name, slot) == 0 then
  235.                         --curInv is full. go to next
  236.                         full = true
  237.                         break
  238.                     end
  239.                 end
  240.  
  241.                 itemList = outWrap.list()
  242.                 itemCount = 0
  243.                 for _,_ in pairs(itemList) do
  244.                     itemCount = itemCount + 1
  245.                 end
  246.                 if itemCount == 0 then
  247.                     return true
  248.                 elseif full then
  249.                     --curInv is full. Go to next
  250.                     break
  251.                 end
  252.             end
  253.         end
  254.     end
  255.  
  256.     if itemCount > 0 then
  257.         return false
  258.     end
  259.     return true
  260. end
  261.  
  262. local function dumpChestCheck(dumpName)
  263.     --error checking to make sure dump chest exists
  264.     local curNameList = peripheral.getNames()
  265.     for i = 1, #curNameList do
  266.         if dumpName == curNameList[i] then
  267.             return dumpName
  268.         end
  269.     end
  270.  
  271.     local sortedNames = {name = "Inventory List"}
  272.     for i = 1, #curNameList do
  273.         local curName = curNameList[i]
  274.         if isNotAdjacent(curName) then
  275.             local typeList = {peripheral.getType(curName)} -- = tbl of types
  276.             if isInv(typeList) then
  277.                 sortedNames[#sortedNames+1] = {name = curNameList[i]}
  278.             end
  279.         end
  280.     end
  281.  
  282.     while true do
  283.         term.clear()
  284.         term.setCursorPos(1,1)
  285.         term.setTextColor(menuColors.title)
  286.         print("Please define a valid dump chest")
  287.         print("Or type 'list' to pick from a list of inventories")
  288.         term.setTextColor(menuColors.menu)
  289.         dumpName = read()
  290.         term.setTextColor(menuColors.text)
  291.  
  292.         if dumpName == "list" then
  293.             local cursor,choice
  294.             while choice ~= "exit" do
  295.                 cursor,choice = openMenu(sortedNames)
  296.                 if choice == "select" then
  297.                     return sortedNames[cursor].name
  298.                 end
  299.             end
  300.         else
  301.             for i = 1, #sortedNames do
  302.                 if dumpName == sortedNames[i].name then
  303.                     return dumpName
  304.                 end
  305.             end
  306.         end
  307.     end
  308. end
  309.  
  310. --###################################################################
  311. --###################################################################
  312. --Main function
  313. local menuTbl = {
  314.     name = "Main Menu",
  315.     {name = "Items"},
  316.     {name = "Dump"},
  317.     {name = "Update Item/Chest List"},
  318.     {name = "Settings"}
  319. }
  320.  
  321. local colorOptions = {
  322.     name = "Color List",
  323.     {name = "White", color = 1},
  324.     {name = "Orange", color = 2},
  325.     {name = "Magenta", color = 4},
  326.     {name = "Light Blue", color = 8},
  327.     {name = "Yellow", color = 16},
  328.     {name = "Lime", color = 32},
  329.     {name = "Pink", color = 64},
  330.     {name = "Gray", color = 128},
  331.     {name = "Light Gray", color = 256},
  332.     {name = "Cyan", color = 512},
  333.     {name = "Purple", color = 1024},
  334.     {name = "Blue", color = 2048},
  335.     {name = "Brown", color = 4096},
  336.     {name = "Green", color = 8192},
  337.     {name = "Red", color = 16384},
  338.     {name = "Black"}
  339. }
  340.  
  341. local defaultSettings = {
  342.     settingsMenu = {
  343.         name = "Settings",
  344.         {name = "Title Color",      count = "Light Blue", menu = "colorOptions"},
  345.         {name = "Menu Color",       count = "Purple",     menu = "colorOptions"},
  346.         {name = "Text Color",       count = "White",      menu = "colorOptions"},
  347.         {name = "Highlight Color",  count = "Pink",       menu = "colorOptions"},
  348.         --{name = "Background Color", count = "Black",      menu = "colorOptions"}
  349.         {name = "Dump Chest",       count = "minecraft:chest_0"},
  350.         --{name = "Reset Settings To Default"},
  351.     },
  352.     menuColors = {
  353.         title = colors.lightBlue,
  354.         menu = colors.purple,
  355.         text = colors.white,
  356.         highlight = colors.pink,
  357.         --background = colors.black
  358.     }
  359. }
  360.  
  361. local settingsMenu = {
  362.     name = "Settings",
  363.     {name = "Title Color",      count = "Light Blue", menu = "colorOptions"},
  364.     {name = "Menu Color",       count = "Purple",     menu = "colorOptions"},
  365.     {name = "Text Color",       count = "White",      menu = "colorOptions"},
  366.     {name = "Highlight Color",  count = "Pink",       menu = "colorOptions"},
  367.     --{name = "Background Color", count = "Black",      menu = "colorOptions"}
  368.     {name = "Dump Chest", count = "minecraft:chest_0"},
  369.     --{name = "Reset Settings To Default"},
  370. }
  371.  
  372. --Load values and check if was interrupted in previous task
  373. local data = {}
  374. if( fs.exists("inventorySettings.txt") ) then
  375.     local file = fs.open("inventorySettings.txt","r")
  376.     data = textutils.unserialise(file.readAll())
  377.     file.close()
  378.     settingsMenu = data.settingsMenu
  379.     menuColors = data.menuColors
  380. else
  381.     data = {
  382.         settingsMenu = settingsMenu,
  383.         menuColors = menuColors
  384.     }
  385.     local file = fs.open("inventorySettings.txt","w")
  386.     file.write(textutils.serialise(data))
  387.     file.close()
  388. end
  389.  
  390. --initialize variables from settings
  391. --error checking to make sure dumpChest exists
  392. --Get dump chest name from settings
  393. local test = dumpChestCheck(settingsMenu[5].count)
  394. if test ~= settingsMenu[5].count then
  395.     --update save file
  396.     settingsMenu[5].count = test
  397.     data = {
  398.         settingsMenu = settingsMenu,
  399.         menuColors = menuColors
  400.     }
  401.     local file = fs.open("inventorySettings.txt","w")
  402.     file.write(textutils.serialise(data))
  403.     file.close()
  404. end
  405.  
  406. local outChest = test
  407. local outWrap = peripheral.wrap(outChest)
  408.  
  409. --remove dumpChest from inventory list
  410. local nameList = peripheral.getNames()
  411. for i = 1, #nameList do
  412.     if nameList[i] == outChest then
  413.         table.remove(nameList,i)
  414.         break
  415.     end
  416. end
  417.  
  418. --finish setting up variables
  419. local wrapList = sortForInv(nameList,outChest)
  420. local itemList
  421. local needToUpdate = true
  422.  
  423.  
  424. while true do
  425.     --outer menu
  426.     term.clear()
  427.     local cursor,menuChoice = openMenu(menuTbl)
  428.  
  429.     local mainChoice = menuTbl[cursor].name
  430.     term.clear()
  431.  
  432.         if mainChoice == "Items" or menuChoice == "search" then
  433.  
  434.             local activeInv = itemList
  435.             local activeSearch = false
  436.             local searchKey = ""
  437.  
  438.             --check if search is coming from main menu
  439.             if menuChoice == "search" then
  440.                 term.clear()
  441.                 term.setCursorPos(1,1)
  442.                 print("Enter search target:")
  443.                 searchKey = read()
  444.                 activeSearch = true
  445.                 if needToUpdate then
  446.                     term.setCursorPos(1,1)
  447.                     print("Getting item list...")
  448.                     itemList = getItemList(wrapList)
  449.                     needToUpdate = false
  450.                     term.clear()
  451.                 end
  452.                 activeInv = search(searchKey,itemList)
  453.             elseif needToUpdate then
  454.                 term.setCursorPos(1,1)
  455.                 print("Getting item list...")
  456.                 itemList = getItemList(wrapList)
  457.                 activeInv = itemList
  458.                 needToUpdate = false
  459.                 term.clear()
  460.             end
  461.            
  462.             --inside item menu
  463.             while menuChoice ~= "exit" do
  464.                 cursor,menuChoice = openMenu(activeInv,cursor)
  465.                
  466.                 if menuChoice == "select" then
  467.                     --pullItem(cursor,itemList)
  468.                     if activeInv[cursor] then
  469.                         local item = activeInv[cursor]
  470.                         local slot = item.slot
  471.                         local count = item.count
  472.  
  473.                         local wrapFuncs = wrapList[item.chestIndex]
  474.  
  475.                         --try to push 64 items. If items pushed ~= 0 then go into statement
  476.                         if wrapFuncs.pushItems(outChest,slot,64) ~= 0 then
  477.                             --needToUpdate = true
  478.                             --remove it from the list if stack less than 64 because we moved the whole stack, then update list
  479.                             if count <= 64 then
  480.                                 table.remove(itemList, item.index)
  481.                                 updateIndex(itemList)
  482.                                 if activeSearch then
  483.                                     --CHANGE:
  484.                                     --make this more efficient by using an index table to point to outer itemList instead of creating a new itemList
  485.                                     --HAVE to use search() method instead of updateIndex method because index has to be based on itemList, not activeInv
  486.                                    
  487.                                     table.remove(activeInv,cursor)
  488.                                     --activeInv = search(searchKey,itemList)
  489.                                 end
  490.                             else
  491.                                 --items still remaining, update count
  492.                                 itemList[item.index].count = count - 64
  493.                                 if activeSearch then
  494.                                     activeInv[cursor].count = count - 64
  495.                                 end
  496.                             end
  497.                         end
  498.                     end
  499.  
  500.                 elseif menuChoice == "search" then
  501.                     activeSearch = true
  502.                     term.clear()
  503.                     term.setCursorPos(1,1)
  504.                     print("Enter search target:")
  505.                     searchKey = read()
  506.                     activeInv = search(searchKey,itemList)
  507.                     term.clear()
  508.                     term.setCursorPos(1,1)
  509.                     print("Searching...")
  510.  
  511.                 elseif menuChoice == "exit" then
  512.                     --if activeSearch = true then only go back to invList, not main menu
  513.                     if activeSearch then
  514.                         activeSearch = false
  515.                         activeInv = itemList
  516.                         menuChoice = ""
  517.                     end
  518.                 end
  519.             end
  520.  
  521.         elseif mainChoice == "Dump" then
  522.             needToUpdate = true
  523.  
  524.             --error checking to make sure dump chest exists
  525.             local newDumpChest = dumpChestCheck(outChest)
  526.             if newDumpChest ~= outChest then
  527.                 outChest = newDumpChest
  528.                 outWrap = peripheral.wrap(outChest)
  529.  
  530.                 --update save file
  531.                 settingsMenu[cursor].count = outChest
  532.                 data = {
  533.                     settingsMenu = settingsMenu,
  534.                     menuColors = menuColors
  535.                 }
  536.                 local file = fs.open("inventorySettings.txt","w")
  537.                 file.write(textutils.serialise(data))
  538.                 file.close()
  539.             end
  540.  
  541.             if not dump(outWrap,wrapList) then
  542.                 term.setCursorPos(1,1)
  543.                 print("Failed to dump all items!")
  544.                 print("Storage likely full")
  545.                 print("Press 'enter' to continue")
  546.                 read()
  547.                 nameList = peripheral.getNames()
  548.                 wrapList = sortForInv(nameList,outChest)
  549.             end
  550.  
  551.         elseif mainChoice == "Update Item/Chest List" then
  552.             nameList = peripheral.getNames()
  553.             wrapList = sortForInv(nameList,outChest)
  554.             term.setCursorPos(1,1)
  555.             print("Getting item list...")
  556.             itemList = getItemList(wrapList)
  557.             needToUpdate = false
  558.    
  559.         elseif mainChoice == "Settings" then
  560.             while menuChoice ~= "exit" do
  561.                 cursor,menuChoice = openMenu(settingsMenu)
  562.  
  563.                 if menuChoice == "select" then
  564.                     --check to see if current option has .menu element
  565.                     if settingsMenu[cursor].menu then
  566.                         if settingsMenu[cursor].menu == "colorOptions" then
  567.                             --inside a specific color
  568.                             local toChange = cursor
  569.                             cursor,menuChoice = openMenu(colorOptions)
  570.                             if menuChoice == "select" then
  571.                                 --[[
  572.                                     local menuColors = {
  573.                                         title = colors.lightBlue,
  574.                                         menu = colors.purple,
  575.                                         text = colors.white,
  576.                                         highlight = colors.pink,
  577.                                         background = colors.black
  578.                                     }
  579.                                 ]]
  580.                                 settingsMenu[toChange].count = colorOptions[cursor].name
  581.                                 print("settingsMenu["..toChange.."].count = "..colorOptions[cursor].name)
  582.                                 local colorVal = 2^(cursor-1)
  583.                                 if toChange == 1 then
  584.                                     menuColors.title = colorVal
  585.                                     print("title = "..colorVal)
  586.                                 elseif toChange == 2 then
  587.                                     menuColors.menu = colorVal
  588.                                     print("menu = "..colorVal)
  589.                                 elseif toChange == 3 then
  590.                                     menuColors.text = colorVal
  591.                                     print("text = "..colorVal)
  592.                                 elseif toChange == 4 then
  593.                                     menuColors.highlight = colorVal
  594.                                     print("highlight = "..colorVal)
  595.                                 --elseif cursor == 5 then
  596.                                 --    menuColors.background = colorVal
  597.                                 end
  598.  
  599.                                 --update saveFile
  600.                                 data = {
  601.                                     settingsMenu = settingsMenu,
  602.                                     menuColors = menuColors
  603.                                 }
  604.                                 local file = fs.open("inventorySettings.txt","w")
  605.                                 file.write(textutils.serialise(data))
  606.                                 file.close()
  607.                             end
  608.                         end
  609.                     --[[
  610.                     elseif settingsMenu[cursor].name == "Reset Settings To Default" then
  611.                         print("Old settings:")
  612.                         print("Settings: "..textutils.serialize(settingsMenu))
  613.                         read()
  614.                         print("menu colors: "..textutils.serialize(menuColors))
  615.                         read()
  616.  
  617.                         settingsMenu = defaultSettings.settingsMenu
  618.                         menuColors = defaultSettings.menuColors
  619.  
  620.                         local file = fs.open("inventorySettings.txt","w")
  621.                         file.write(textutils.serialise(defaultSettings))
  622.                         file.close()
  623.  
  624.                         print("Reset settings:")
  625.                         print("Settings: "..textutils.serialize(settingsMenu))
  626.                         read()
  627.                         print("menu colors: "..textutils.serialize(menuColors))
  628.                         read()
  629.                     ]]
  630.  
  631.                     elseif settingsMenu[cursor].name == "Dump Chest" then
  632.                         local lineLen = 15--15 = #">Dump Chest< : "
  633.                         term.setCursorPos(lineLen,cursor+1)
  634.                         term.setTextColor(menuColors.menu)
  635.                         --update dump chest
  636.                         local newDumpChest = read(nil,nil,nil,tostring(settingsMenu[cursor].count))
  637.  
  638.                         --error checking to make sure dump chest exists
  639.                         if newDumpChest ~= outChest then
  640.                             newDumpChest = dumpChestCheck(newDumpChest)
  641.                         end
  642.  
  643.                         outChest = newDumpChest
  644.                         outWrap = peripheral.wrap(outChest)
  645.                    
  646.                         --update save file
  647.                         settingsMenu[cursor].count = outChest
  648.                         data = {
  649.                             settingsMenu = settingsMenu,
  650.                             menuColors = menuColors
  651.                         }
  652.                         local file = fs.open("inventorySettings.txt","w")
  653.                         file.write(textutils.serialise(data))
  654.                         file.close()
  655.                     end
  656.                 end
  657.             end
  658.         end
  659. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement