Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --NEW VERSION IS COMPLETELY CONTAINED IN STARTUP FILE:
- --[[
- --OLD STARTUP FILE:
- local inv = require("inventoryFunctions")
- local menuList = {
- { name = "Search Inventory" },
- { name = "Item Dump" },
- }
- local outChest = peripheral.wrap("minecraft:chest_23")
- --local garbage = peripheral.wrap()
- --inv.getItemMenu(outChest)
- while true do
- --Present a list of possible pages to pick from
- local cursor, _ = inv.handleMenu(menuList)
- --Option results:
- if cursor == 1 then
- inv.getItemMenu(outChest)
- elseif cursor == 2 then
- inv.dump(outChest)
- end
- end
- ]]
- local debug = false
- --captures any amount of arguments and returns them in a table
- local function capArgs(...)
- return arg
- end
- --returns a table of connected inventory wrap tables and names
- local function getStorageChests(exclude)
- if not exclude then exclude = "" end
- local chestWraps = {}
- local peripheralNames = peripheral.getNames()
- --Remove dump chest and extra peripherals from storage list and add valid chests to chestWraps
- for key,val in pairs(peripheralNames) do
- if val == exclude or val == "top" or val == "left" or val == "bottom" or val == "right" or val == "back" then
- table.remove(peripheralNames, key)
- else
- --Load chestWraps list for valid inventories
- local tempList = capArgs(peripheral.getType(val))
- for i = 1, #tempList do
- if tempList[i] == "inventory" then
- local num = #chestWraps+1
- chestWraps[num] = {}
- chestWraps[num].wrap = peripheral.wrap(val)
- chestWraps[num].name = val
- break
- end
- end
- end
- end
- return chestWraps
- end
- --takes a peripheral wraps from getStorageChests() and returns a table of every item in the connected inventories
- local function getInventoryList(chestWraps)
- local inventory = {}
- for i = 1, #chestWraps do
- local temp = chestWraps[i].wrap.list()
- for key,val in pairs(temp) do
- local num = #inventory+1
- inventory[num] = val
- inventory[num].slot = key
- inventory[num].chest = i
- inventory[num].index = num
- inventory[num].count = val.count
- inventory[num].name = string.lower(string.gsub(val.name, ".+%:", ""))
- end
- end
- return inventory
- end
- --Returns a table of matching items from item list
- --And returns a total count of items found
- --[[
- foundInChestList = {
- [1] = {
- chestID = 'number'
- { name = "", slot = "", count = "" }
- ...
- }
- ...
- }
- ]]
- local function search(search,itemList)
- search = string.lower(search)
- local foundItems = {}
- local totalCount = 0
- --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, search ) then
- --found an item that matches search. Add it to foundItems table and update total found item count
- local num = #foundItems+1
- foundItems[num] = itemList[i]
- totalCount = totalCount + itemList[i].count
- end
- end
- return foundItems,totalCount
- end
- --Displays a arrow key controlled menu using a provided menuPage
- local function handleMenu(menuOptions, cursor)
- if not cursor then cursor = 1 end
- local _,pageLength = term.getSize()
- pageLength = pageLength - 3
- local maxPage = math.ceil(#menuOptions/(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), #menuOptions )
- local col = term.getTextColor()
- cursor = math.min(cursor,displayEndIndex)
- cursor = math.max(cursor,1)
- if debug then
- term.setTextColor(colors.white)
- local hor = 25
- term.setCursorPos(hor,1)
- print("cursor :",cursor.." ")
- term.setCursorPos(hor,2)
- print("page :",page.." ")
- term.setCursorPos(hor,3)
- print("pageLen :",pageLength.." ")
- term.setCursorPos(hor,4)
- print("maxPg. :",maxPage.." ")
- term.setCursorPos(hor,5)
- print("disp. Start :",displayStartIndex.." ")
- term.setCursorPos(hor,6)
- print("disp. End :",displayEndIndex.." ")
- end
- local menuColor = colors.green
- term.setTextColor(menuColor)
- term.setCursorPos(1,1)
- --Display item options
- for index = displayStartIndex, displayEndIndex do
- local displayStr = ""
- if menuOptions[index] then
- displayStr = menuOptions[index].name
- if menuOptions[index].count then
- if menuOptions[index].count ~= 1 then
- displayStr = displayStr.." x"..menuOptions[index].count
- end
- end
- end
- if index == cursor then
- term.setTextColor(colors.orange)
- print(">"..(displayStr).."<")
- term.setTextColor(menuColor)
- else
- print("["..(displayStr).."]")
- end
- end
- term.setTextColor(col)
- 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
- local function updateIndex(itemList)
- for key,val in pairs(itemList) do
- itemList[key].index = key
- end
- return itemList
- end
- --Moves 'count' number of items from foundItemList to the outChest
- local function getItemMenu(outChest)
- local outChestName = peripheral.getName(outChest)
- local wrapList = getStorageChests(outChestName)
- local inventoryList = getInventoryList(wrapList)
- local activeInventory = inventoryList
- local searchKey = ""
- local cursor = 1
- local strKey
- while true do
- cursor,strKey = handleMenu(activeInventory,cursor)
- if strKey == "exit" then
- --If player wants to go back
- if searchKey == "" then
- break
- else
- searchKey = ""
- activeInventory = inventoryList
- end
- elseif strKey == "search" then
- term.clear()
- term.setCursorPos(1,1)
- print("Enter search target:")
- searchKey = read()
- activeInventory = search(searchKey,inventoryList)
- else
- if activeInventory[cursor] then
- --move highlighted item to output chest
- local itemSlot = activeInventory[cursor].slot
- local wrapID = wrapList[ activeInventory[cursor].chest ].wrap
- local count = activeInventory[cursor].count
- if wrapID.pushItems(outChestName, itemSlot, 64) ~= 0 then
- --remove it from the list if stack is <= 64, then update lists
- if count <= 64 then
- local removeIndex = activeInventory[cursor].index
- table.remove(inventoryList, removeIndex)
- inventoryList = updateIndex(inventoryList)
- table.remove(activeInventory, cursor)
- activeInventory = updateIndex(activeInventory)
- else
- activeInventory[cursor].count = count - 64
- end
- end
- end
- end
- end
- end
- --Takes everything in a specific chest and tries to push it into other peripheral inventories
- --Takes an inventory peripheral wrap table
- local function dump(dumpChest)
- local inventoryWraps = getStorageChests(dumpChest)
- local storageSize = #inventoryWraps
- local totalItemCount = 0
- local movedItems = 0
- for chestNum = 1, storageSize do
- local dumpList = dumpChest.list()
- local chestWrap = inventoryWraps[chestNum]
- for slot,_ in pairs(dumpList) do
- totalItemCount = totalItemCount + 1
- if dumpChest.pushItems(chestWrap.name, slot) ~= 0 then
- movedItems = movedItems + 1
- end
- end
- if movedItems == totalItemCount then return end
- end
- end
- return {
- getItemMenu = getItemMenu,
- handleMenu = handleMenu,
- dump = dump
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement