Advertisement
jig487

inventoryFunctions_old

Jun 28th, 2023 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.75 KB | None | 0 0
  1. --NEW VERSION IS COMPLETELY CONTAINED IN STARTUP FILE:
  2. --[[
  3. --OLD STARTUP FILE:
  4. local inv = require("inventoryFunctions")
  5.  
  6. local menuList = {
  7.     { name = "Search Inventory" },
  8.     { name = "Item Dump" },
  9. }
  10.  
  11. local outChest = peripheral.wrap("minecraft:chest_23")
  12. --local garbage = peripheral.wrap()
  13.  
  14. --inv.getItemMenu(outChest)
  15.  
  16. while true do
  17.     --Present a list of possible pages to pick from
  18.     local cursor, _ = inv.handleMenu(menuList)
  19.     --Option results:
  20.     if cursor == 1 then
  21.         inv.getItemMenu(outChest)
  22.     elseif cursor == 2 then
  23.         inv.dump(outChest)
  24.     end
  25. end
  26. ]]
  27.  
  28. local debug = false
  29.  
  30. --captures any amount of arguments and returns them in a table
  31. local function capArgs(...)
  32.     return arg
  33. end
  34.  
  35. --returns a table of connected inventory wrap tables and names
  36. local function getStorageChests(exclude)
  37.     if not exclude then exclude = "" end
  38.  
  39.     local chestWraps = {}
  40.  
  41.     local peripheralNames = peripheral.getNames()
  42.  
  43.     --Remove dump chest and extra peripherals from storage list and add valid chests to chestWraps
  44.     for key,val in pairs(peripheralNames) do
  45.         if val == exclude or val == "top" or val == "left" or val == "bottom" or val == "right" or val == "back" then
  46.             table.remove(peripheralNames, key)
  47.         else
  48.             --Load chestWraps list for valid inventories
  49.             local tempList = capArgs(peripheral.getType(val))
  50.             for i = 1, #tempList do
  51.                 if tempList[i] == "inventory" then
  52.                     local num = #chestWraps+1
  53.                     chestWraps[num] = {}
  54.                     chestWraps[num].wrap = peripheral.wrap(val)
  55.                     chestWraps[num].name = val
  56.                     break
  57.                 end
  58.             end
  59.         end
  60.     end
  61.  
  62.     return chestWraps
  63. end
  64.  
  65. --takes a peripheral wraps from getStorageChests() and returns a table of every item in the connected inventories
  66. local function getInventoryList(chestWraps)
  67.     local inventory = {}
  68.     for i = 1, #chestWraps do
  69.         local temp = chestWraps[i].wrap.list()
  70.         for key,val in pairs(temp) do
  71.             local num = #inventory+1
  72.             inventory[num] = val
  73.             inventory[num].slot = key
  74.             inventory[num].chest = i
  75.             inventory[num].index = num
  76.             inventory[num].count = val.count
  77.             inventory[num].name = string.lower(string.gsub(val.name, ".+%:", ""))
  78.         end
  79.     end
  80.     return inventory
  81. end
  82.  
  83. --Returns a table of matching items from item list
  84. --And returns a total count of items found
  85. --[[
  86. foundInChestList = {
  87.     [1] = {
  88.         chestID = 'number'
  89.         { name = "", slot = "", count = "" }
  90.         ...
  91.     }
  92.     ...
  93. }
  94. ]]
  95. local function search(search,itemList)
  96.  
  97.     search = string.lower(search)
  98.     local foundItems = {}
  99.     local totalCount = 0
  100.  
  101.     --Look through each item
  102.     for i = 1, #itemList do
  103.         --Check to see if current item matches item we are searching for
  104.         if string.find( itemList[i].name, search ) then
  105.             --found an item that matches search. Add it to foundItems table and update total found item count
  106.             local num = #foundItems+1
  107.             foundItems[num] = itemList[i]
  108.             totalCount = totalCount + itemList[i].count
  109.         end
  110.     end
  111.  
  112.     return foundItems,totalCount
  113. end
  114.  
  115. --Displays a arrow key controlled menu using a provided menuPage
  116. local function handleMenu(menuOptions, cursor)
  117.     if not cursor then cursor = 1 end
  118.  
  119.     local _,pageLength = term.getSize()
  120.     pageLength = pageLength - 3
  121.     local maxPage = math.ceil(#menuOptions/(pageLength))
  122.     local page = math.min( math.ceil( cursor / pageLength ) ,maxPage)
  123.  
  124.     term.clear()
  125.     while true do
  126.         local displayStartIndex = math.max( pageLength*(page-1)+1, 1 )
  127.         local displayEndIndex = math.min( pageLength*(page), #menuOptions )
  128.         local col = term.getTextColor()
  129.  
  130.         cursor = math.min(cursor,displayEndIndex)
  131.         cursor = math.max(cursor,1)
  132.  
  133.         if debug then
  134.             term.setTextColor(colors.white)
  135.             local hor = 25
  136.             term.setCursorPos(hor,1)
  137.             print("cursor :",cursor.."  ")
  138.             term.setCursorPos(hor,2)
  139.             print("page :",page.."  ")
  140.             term.setCursorPos(hor,3)
  141.             print("pageLen :",pageLength.."  ")
  142.             term.setCursorPos(hor,4)
  143.             print("maxPg. :",maxPage.."  ")
  144.             term.setCursorPos(hor,5)
  145.             print("disp. Start :",displayStartIndex.."  ")
  146.             term.setCursorPos(hor,6)
  147.             print("disp. End :",displayEndIndex.."  ")
  148.  
  149.         end
  150.        
  151.         local menuColor = colors.green
  152.         term.setTextColor(menuColor)
  153.         term.setCursorPos(1,1)
  154.         --Display item options
  155.         for index = displayStartIndex, displayEndIndex do
  156.  
  157.             local displayStr = ""
  158.             if menuOptions[index] then
  159.                 displayStr = menuOptions[index].name
  160.            
  161.  
  162.             if menuOptions[index].count then
  163.                 if menuOptions[index].count ~= 1 then
  164.                     displayStr = displayStr.." x"..menuOptions[index].count
  165.                 end
  166.             end
  167.             end
  168.  
  169.             if index == cursor then
  170.                 term.setTextColor(colors.orange)
  171.                 print(">"..(displayStr).."<")
  172.                 term.setTextColor(menuColor)
  173.             else
  174.                 print("["..(displayStr).."]")
  175.             end
  176.         end
  177.         term.setTextColor(col)
  178.         print("Page "..page.." / "..maxPage)
  179.         local _, key = os.pullEvent("key")
  180.         if key == keys.up then
  181.             --Move cursor up if it's not at first option
  182.             if cursor > displayStartIndex then
  183.                 cursor = cursor - 1
  184.             end
  185.         elseif key == keys.down then
  186.             --move cursor down if it's not at last option
  187.             if cursor < displayEndIndex then
  188.                 cursor = cursor + 1
  189.             end
  190.         elseif key == keys.left then
  191.             --move to previous page if not at first page
  192.             if page > 1 then
  193.                 page = page - 1
  194.                 cursor = cursor - pageLength
  195.                 term.clear()
  196.             end
  197.         elseif key == keys.right then
  198.             --move to next page if not at last page
  199.             if page < maxPage then
  200.                 page = page + 1
  201.                 cursor = cursor + pageLength
  202.                 term.clear()
  203.             end
  204.         elseif key == keys.enter then
  205.             --return the menu choice
  206.             return cursor,"select"
  207.         elseif key == keys.backspace then
  208.             --exit the menu
  209.             return cursor,"exit"
  210.         else
  211.             --player wants to search for a string
  212.             return cursor,"search"
  213.         end
  214.     end
  215. end
  216.  
  217. local function updateIndex(itemList)
  218.     for key,val in pairs(itemList) do
  219.         itemList[key].index = key
  220.     end
  221.     return itemList
  222. end
  223.  
  224. --Moves 'count' number of items from foundItemList to the outChest
  225. local function getItemMenu(outChest)
  226.  
  227.     local outChestName = peripheral.getName(outChest)
  228.     local wrapList = getStorageChests(outChestName)
  229.     local inventoryList = getInventoryList(wrapList)
  230.     local activeInventory = inventoryList
  231.  
  232.     local searchKey = ""
  233.     local cursor = 1
  234.     local strKey
  235.  
  236.     while true do
  237.  
  238.         cursor,strKey = handleMenu(activeInventory,cursor)
  239.  
  240.         if strKey == "exit" then
  241.             --If player wants to go back
  242.             if searchKey == "" then
  243.                 break
  244.             else
  245.                 searchKey = ""
  246.                 activeInventory = inventoryList
  247.             end
  248.         elseif strKey == "search" then
  249.             term.clear()
  250.             term.setCursorPos(1,1)
  251.             print("Enter search target:")
  252.             searchKey = read()
  253.             activeInventory = search(searchKey,inventoryList)
  254.         else
  255.             if activeInventory[cursor] then
  256.                 --move highlighted item to output chest
  257.                 local itemSlot = activeInventory[cursor].slot
  258.                 local wrapID = wrapList[ activeInventory[cursor].chest ].wrap
  259.  
  260.                 local count = activeInventory[cursor].count
  261.                 if wrapID.pushItems(outChestName, itemSlot, 64) ~= 0 then
  262.                     --remove it from the list if stack is <= 64, then update lists
  263.                     if count <= 64 then
  264.                         local removeIndex = activeInventory[cursor].index
  265.  
  266.                         table.remove(inventoryList, removeIndex)
  267.                         inventoryList = updateIndex(inventoryList)
  268.  
  269.                         table.remove(activeInventory, cursor)
  270.                         activeInventory = updateIndex(activeInventory)
  271.                     else
  272.                         activeInventory[cursor].count = count - 64
  273.                     end
  274.                 end
  275.             end
  276.         end
  277.     end
  278. end
  279.  
  280. --Takes everything in a specific chest and tries to push it into other peripheral inventories
  281. --Takes an inventory peripheral wrap table
  282. local function dump(dumpChest)
  283.     local inventoryWraps = getStorageChests(dumpChest)
  284.     local storageSize = #inventoryWraps
  285.  
  286.     local totalItemCount = 0
  287.     local movedItems = 0
  288.  
  289.     for chestNum = 1, storageSize do
  290.  
  291.         local dumpList = dumpChest.list()
  292.  
  293.         local chestWrap = inventoryWraps[chestNum]
  294.  
  295.         for slot,_ in pairs(dumpList) do
  296.             totalItemCount = totalItemCount + 1
  297.  
  298.             if dumpChest.pushItems(chestWrap.name, slot) ~= 0 then
  299.                 movedItems = movedItems + 1
  300.             end
  301.         end
  302.  
  303.         if movedItems == totalItemCount then return end
  304.     end
  305. end
  306.  
  307. return {
  308.     getItemMenu = getItemMenu,
  309.     handleMenu = handleMenu,
  310.     dump = dump
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement