Advertisement
jig487

startup

Jun 28th, 2023 (edited)
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.27 KB | None | 0 0
  1. --pastebin get hre4rgH7 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.     local formatedTarget = string.gsub(searchTarget," ","_")
  190.     formatedTarget = string.lower(formatedTarget)
  191.     local foundItems = {name = "Search: "..searchTarget}
  192.  
  193.     --Look through each item
  194.     for i = 1, #itemList do
  195.         --Check to see if current item matches item we are searching for
  196.         if string.find( itemList[i].name, formatedTarget ) then
  197.             --found an item that matches search. Add it to foundItems table and update total found item count
  198.             foundItems[#foundItems+1] = itemList[i]
  199.         end
  200.     end
  201.  
  202.     return foundItems
  203. end
  204.  
  205. --Takes everything in a specific chest and tries to push it into other peripheral inventories
  206. --returns true if dumped all items, false if failed to dump an item
  207. local function dump(outWrap,wrapList)
  208.     term.clear()
  209.     term.setTextColor(menuColors.title)
  210.     term.setCursorPos(1,1)
  211.     print("Dumping items...")
  212.  
  213.     local badName = peripheral.getName(outWrap)
  214.     local itemList = outWrap.list()
  215.     local namesList = {}
  216.     local itemCount = 0
  217.     for _,_ in pairs(itemList) do
  218.         itemCount = itemCount + 1
  219.     end
  220.     for index,wrap in pairs(wrapList) do
  221.         namesList[index] = peripheral.getName(wrap)
  222.     end
  223.  
  224.     --CHANGE:
  225.     --Make this smart. Deposit items in the first chest that contains a similar item. Go to next chest if full
  226.     for i = 1, #namesList do
  227.         local name = namesList[i]
  228.  
  229.  
  230.         if name ~= badname then
  231.             while itemCount > 0 do
  232.                 local full = false
  233.                 for slot,_ in pairs(itemList) do
  234.                     --.pushItems returns the amount of items moved
  235.                     if outWrap.pushItems(name, slot) == 0 then
  236.                         --curInv is full. go to next
  237.                         full = true
  238.                         break
  239.                     end
  240.                 end
  241.  
  242.                 itemList = outWrap.list()
  243.                 itemCount = 0
  244.                 for _,_ in pairs(itemList) do
  245.                     itemCount = itemCount + 1
  246.                 end
  247.                 if itemCount == 0 then
  248.                     return true
  249.                 elseif full then
  250.                     --curInv is full. Go to next
  251.                     break
  252.                 end
  253.             end
  254.         end
  255.     end
  256.  
  257.     if itemCount > 0 then
  258.         return false
  259.     end
  260.     return true
  261. end
  262.  
  263. local function dumpChestCheck(dumpName)
  264.     --error checking to make sure dump chest exists
  265.     local curNameList = peripheral.getNames()
  266.     for i = 1, #curNameList do
  267.         if dumpName == curNameList[i] then
  268.             return dumpName
  269.         end
  270.     end
  271.  
  272.     local sortedNames = {name = "Inventory List"}
  273.     for i = 1, #curNameList do
  274.         local curName = curNameList[i]
  275.         if isNotAdjacentOrDump(curName,dumpName) then
  276.             local typeList = {peripheral.getType(curName)} -- = tbl of types
  277.             if isInv(typeList) then
  278.                 sortedNames[#sortedNames+1] = {name = curNameList[i]}
  279.             end
  280.         end
  281.     end
  282.  
  283.     while true do
  284.         term.clear()
  285.         term.setCursorPos(1,1)
  286.         term.setTextColor(menuColors.title)
  287.         print("Please define a valid dump chest")
  288.         print("Or type 'list' to pick from a list of inventories")
  289.         term.setTextColor(menuColors.menu)
  290.         dumpName = read()
  291.         term.setTextColor(menuColors.text)
  292.  
  293.         if dumpName == "list" then
  294.             local cursor,choice
  295.             while choice ~= "exit" do
  296.                 cursor,choice = openMenu(sortedNames)
  297.                 if choice == "select" then
  298.                     return sortedNames[cursor].name
  299.                 end
  300.             end
  301.         else
  302.             for i = 1, #sortedNames do
  303.                 if dumpName == sortedNames[i].name then
  304.                     return dumpName
  305.                 end
  306.             end
  307.         end
  308.     end
  309. end
  310.  
  311. --###################################################################
  312. --###################################################################
  313. --Main function
  314. local menuTbl = {
  315.     name = "Main Menu",
  316.     {name = "Items"},
  317.     {name = "Dump"},
  318.     {name = "Update Item/Chest List"},
  319.     {name = "Settings"}
  320. }
  321.  
  322. local colorOptions = {
  323.     name = "Color List",
  324.     {name = "White", color = 1},
  325.     {name = "Orange", color = 2},
  326.     {name = "Magenta", color = 4},
  327.     {name = "Light Blue", color = 8},
  328.     {name = "Yellow", color = 16},
  329.     {name = "Lime", color = 32},
  330.     {name = "Pink", color = 64},
  331.     {name = "Gray", color = 128},
  332.     {name = "Light Gray", color = 256},
  333.     {name = "Cyan", color = 512},
  334.     {name = "Purple", color = 1024},
  335.     {name = "Blue", color = 2048},
  336.     {name = "Brown", color = 4096},
  337.     {name = "Green", color = 8192},
  338.     {name = "Red", color = 16384},
  339.     {name = "Black"}
  340. }
  341.  
  342. local defaultSettings = {
  343.     settingsMenu = {
  344.         name = "Settings",
  345.         {name = "Title Color",      count = "Light Blue", menu = "colorOptions"},
  346.         {name = "Menu Color",       count = "Purple",     menu = "colorOptions"},
  347.         {name = "Text Color",       count = "White",      menu = "colorOptions"},
  348.         {name = "Highlight Color",  count = "Pink",       menu = "colorOptions"},
  349.         --{name = "Background Color", count = "Black",      menu = "colorOptions"}
  350.         {name = "Dump Chest",       count = "minecraft:chest_0"},
  351.         --{name = "Reset Settings To Default"},
  352.     },
  353.     menuColors = {
  354.         title = colors.lightBlue,
  355.         menu = colors.purple,
  356.         text = colors.white,
  357.         highlight = colors.pink,
  358.         --background = colors.black
  359.     }
  360. }
  361.  
  362. local settingsMenu = {
  363.     name = "Settings",
  364.     {name = "Title Color",      count = "Light Blue", menu = "colorOptions"},
  365.     {name = "Menu Color",       count = "Purple",     menu = "colorOptions"},
  366.     {name = "Text Color",       count = "White",      menu = "colorOptions"},
  367.     {name = "Highlight Color",  count = "Pink",       menu = "colorOptions"},
  368.     --{name = "Background Color", count = "Black",      menu = "colorOptions"}
  369.     {name = "Dump Chest", count = "minecraft:chest_0"},
  370.     --{name = "Reset Settings To Default"},
  371. }
  372.  
  373. --Load values and check if was interrupted in previous task
  374. local data = {}
  375. if( fs.exists("inventorySettings.txt") ) then
  376.     local file = fs.open("inventorySettings.txt","r")
  377.     data = textutils.unserialise(file.readAll())
  378.     file.close()
  379.     settingsMenu = data.settingsMenu
  380.     menuColors = data.menuColors
  381. else
  382.     data = {
  383.         settingsMenu = settingsMenu,
  384.         menuColors = menuColors
  385.     }
  386.     local file = fs.open("inventorySettings.txt","w")
  387.     file.write(textutils.serialise(data))
  388.     file.close()
  389. end
  390.  
  391. --initialize variables from settings
  392. --error checking to make sure dumpChest exists
  393. --Get dump chest name from settings
  394. local test = dumpChestCheck(settingsMenu[5].count)
  395. if test ~= settingsMenu[5].count then
  396.     --update save file
  397.     settingsMenu[5].count = test
  398.     data = {
  399.         settingsMenu = settingsMenu,
  400.         menuColors = menuColors
  401.     }
  402.     local file = fs.open("inventorySettings.txt","w")
  403.     file.write(textutils.serialise(data))
  404.     file.close()
  405. end
  406.  
  407. local outChest = test
  408. local outWrap = peripheral.wrap(outChest)
  409.  
  410. --remove dumpChest from inventory list
  411. local nameList = peripheral.getNames()
  412. for i = 1, #nameList do
  413.     if nameList[i] == outChest then
  414.         table.remove(nameList,i)
  415.         break
  416.     end
  417. end
  418.  
  419. --finish setting up variables
  420. local wrapList = sortForInv(nameList,outChest)
  421. local itemList
  422. local needToUpdate = true
  423.  
  424.  
  425. while true do
  426.     --outer menu
  427.     term.clear()
  428.     local cursor,menuChoice = openMenu(menuTbl)
  429.  
  430.     local mainChoice = menuTbl[cursor].name
  431.     term.clear()
  432.  
  433.         if mainChoice == "Items" or menuChoice == "search" then
  434.  
  435.             local activeInv = itemList
  436.             local activeSearch = false
  437.             local searchKey = ""
  438.  
  439.             --check if search is coming from main menu
  440.             if menuChoice == "search" then
  441.                 term.clear()
  442.                 term.setCursorPos(1,1)
  443.                 print("Enter search target:")
  444.                 searchKey = read()
  445.                 activeSearch = true
  446.                 if needToUpdate then
  447.                     term.setCursorPos(1,1)
  448.                     print("Getting item list...")
  449.                     itemList = getItemList(wrapList)
  450.                     needToUpdate = false
  451.                     term.clear()
  452.                 end
  453.                 activeInv = search(searchKey,itemList)
  454.             elseif needToUpdate then
  455.                 term.setCursorPos(1,1)
  456.                 print("Getting item list...")
  457.                 itemList = getItemList(wrapList)
  458.                 activeInv = itemList
  459.                 needToUpdate = false
  460.                 term.clear()
  461.             end
  462.            
  463.             --inside item menu
  464.             while menuChoice ~= "exit" do
  465.                 cursor,menuChoice = openMenu(activeInv,cursor)
  466.                
  467.                 if menuChoice == "select" then
  468.                     --pullItem(cursor,itemList)
  469.                     if activeInv[cursor] then
  470.                         local item = activeInv[cursor]
  471.                         local slot = item.slot
  472.                         local count = item.count
  473.  
  474.                         local wrapFuncs = wrapList[item.chestIndex]
  475.  
  476.                         --try to push 64 items. If items pushed ~= 0 then go into statement
  477.                         if wrapFuncs.pushItems(outChest,slot,64) ~= 0 then
  478.                             --needToUpdate = true
  479.                             --remove it from the list if stack less than 64 because we moved the whole stack, then update list
  480.                             if count <= 64 then
  481.                                 table.remove(itemList, item.index)
  482.                                 updateIndex(itemList)
  483.                                 if activeSearch then
  484.                                     --CHANGE:
  485.                                     --make this more efficient by using an index table to point to outer itemList instead of creating a new itemList
  486.                                     --HAVE to use search() method instead of updateIndex method because index has to be based on itemList, not activeInv
  487.                                    
  488.                                     table.remove(activeInv,cursor)
  489.                                     --activeInv = search(searchKey,itemList)
  490.                                 end
  491.                             else
  492.                                 --items still remaining, update count
  493.                                 itemList[item.index].count = count - 64
  494.                                 if activeSearch then
  495.                                     activeInv[cursor].count = count - 64
  496.                                 end
  497.                             end
  498.                         end
  499.                     end
  500.  
  501.                 elseif menuChoice == "search" then
  502.                     activeSearch = true
  503.                     term.clear()
  504.                     term.setCursorPos(1,1)
  505.                     print("Enter search target:")
  506.                     searchKey = read()
  507.                     activeInv = search(searchKey,itemList)
  508.                     term.clear()
  509.                     term.setCursorPos(1,1)
  510.                     print("Searching...")
  511.  
  512.                 elseif menuChoice == "exit" then
  513.                     --if activeSearch = true then only go back to invList, not main menu
  514.                     if activeSearch then
  515.                         activeSearch = false
  516.                         activeInv = itemList
  517.                         menuChoice = ""
  518.                     end
  519.                 end
  520.             end
  521.  
  522.         elseif mainChoice == "Dump" then
  523.             needToUpdate = true
  524.  
  525.             --error checking to make sure dump chest exists
  526.             local newDumpChest = dumpChestCheck(outChest)
  527.             if newDumpChest ~= outChest then
  528.                 outChest = newDumpChest
  529.                 outWrap = peripheral.wrap(outChest)
  530.  
  531.                 --update save file
  532.                 settingsMenu[cursor].count = outChest
  533.                 data = {
  534.                     settingsMenu = settingsMenu,
  535.                     menuColors = menuColors
  536.                 }
  537.                 local file = fs.open("inventorySettings.txt","w")
  538.                 file.write(textutils.serialise(data))
  539.                 file.close()
  540.             end
  541.  
  542.             if not dump(outWrap,wrapList) then
  543.                 term.setCursorPos(1,1)
  544.                 print("Failed to dump all items!")
  545.                 print("Storage likely full")
  546.                 print("Press 'enter' to continue")
  547.                 read()
  548.                 nameList = peripheral.getNames()
  549.                 wrapList = sortForInv(nameList,outChest)
  550.             end
  551.  
  552.         elseif mainChoice == "Update Item/Chest List" then
  553.             nameList = peripheral.getNames()
  554.             wrapList = sortForInv(nameList,outChest)
  555.             term.setCursorPos(1,1)
  556.             print("Getting item list...")
  557.             itemList = getItemList(wrapList)
  558.             needToUpdate = false
  559.    
  560.         elseif mainChoice == "Settings" then
  561.             while menuChoice ~= "exit" do
  562.                 cursor,menuChoice = openMenu(settingsMenu)
  563.  
  564.                 if menuChoice == "select" then
  565.                     --check to see if current option has .menu element
  566.                     if settingsMenu[cursor].menu then
  567.                         if settingsMenu[cursor].menu == "colorOptions" then
  568.                             --inside a specific color
  569.                             local toChange = cursor
  570.                             cursor,menuChoice = openMenu(colorOptions)
  571.                             if menuChoice == "select" then
  572.                                 --[[
  573.                                     local menuColors = {
  574.                                         title = colors.lightBlue,
  575.                                         menu = colors.purple,
  576.                                         text = colors.white,
  577.                                         highlight = colors.pink,
  578.                                         background = colors.black
  579.                                     }
  580.                                 ]]
  581.                                 settingsMenu[toChange].count = colorOptions[cursor].name
  582.                                 print("settingsMenu["..toChange.."].count = "..colorOptions[cursor].name)
  583.                                 local colorVal = 2^(cursor-1)
  584.                                 if toChange == 1 then
  585.                                     menuColors.title = colorVal
  586.                                     print("title = "..colorVal)
  587.                                 elseif toChange == 2 then
  588.                                     menuColors.menu = colorVal
  589.                                     print("menu = "..colorVal)
  590.                                 elseif toChange == 3 then
  591.                                     menuColors.text = colorVal
  592.                                     print("text = "..colorVal)
  593.                                 elseif toChange == 4 then
  594.                                     menuColors.highlight = colorVal
  595.                                     print("highlight = "..colorVal)
  596.                                 --elseif cursor == 5 then
  597.                                 --    menuColors.background = colorVal
  598.                                 end
  599.  
  600.                                 --update saveFile
  601.                                 data = {
  602.                                     settingsMenu = settingsMenu,
  603.                                     menuColors = menuColors
  604.                                 }
  605.                                 local file = fs.open("inventorySettings.txt","w")
  606.                                 file.write(textutils.serialise(data))
  607.                                 file.close()
  608.                             end
  609.                         end
  610.                     --[[
  611.                     elseif settingsMenu[cursor].name == "Reset Settings To Default" then
  612.                         print("Old settings:")
  613.                         print("Settings: "..textutils.serialize(settingsMenu))
  614.                         read()
  615.                         print("menu colors: "..textutils.serialize(menuColors))
  616.                         read()
  617.  
  618.                         settingsMenu = defaultSettings.settingsMenu
  619.                         menuColors = defaultSettings.menuColors
  620.  
  621.                         local file = fs.open("inventorySettings.txt","w")
  622.                         file.write(textutils.serialise(defaultSettings))
  623.                         file.close()
  624.  
  625.                         print("Reset settings:")
  626.                         print("Settings: "..textutils.serialize(settingsMenu))
  627.                         read()
  628.                         print("menu colors: "..textutils.serialize(menuColors))
  629.                         read()
  630.                     ]]
  631.  
  632.                     elseif settingsMenu[cursor].name == "Dump Chest" then
  633.                         local lineLen = 15--15 = #">Dump Chest< : "
  634.                         term.setCursorPos(lineLen,cursor+1)
  635.                         term.setTextColor(menuColors.menu)
  636.                         --update dump chest
  637.                         local newDumpChest = read(nil,nil,nil,tostring(settingsMenu[cursor].count))
  638.  
  639.                         --error checking to make sure dump chest exists
  640.                         if newDumpChest ~= outChest then
  641.                             newDumpChest = dumpChestCheck(newDumpChest)
  642.                         end
  643.  
  644.                         outChest = newDumpChest
  645.                         outWrap = peripheral.wrap(outChest)
  646.                    
  647.                         --update save file
  648.                         settingsMenu[cursor].count = outChest
  649.                         data = {
  650.                             settingsMenu = settingsMenu,
  651.                             menuColors = menuColors
  652.                         }
  653.                         local file = fs.open("inventorySettings.txt","w")
  654.                         file.write(textutils.serialise(data))
  655.                         file.close()
  656.                     end
  657.                 end
  658.             end
  659.         end
  660. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement