Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local clients = peripheral.call("back", "getNamesRemote")
- local chests = {}
- local items = {}
- local itemCollection = {}
- local usingCollection = false
- local itemPage = 1
- local searchTerm = ""
- local function tprint (tbl, indent)
- if not indent then indent = 0 end
- for k, v in pairs(tbl) do
- formatting = string.rep(" ", indent) .. k .. ": "
- if type(v) == "table" then
- print(formatting)
- tprint(v, indent+1)
- elseif type(v) == 'boolean' then
- print(formatting .. tostring(v))
- else
- print(formatting .. v)
- end
- end
- end
- local function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- local function DiscoverChests()
- for k,client in pairs(clients) do
- if (string.sub(peripheral.getType(client), 1, 19) == "minecraft:ironchest") then
- table.insert(chests, client)
- end
- end
- end
- local function DiscoverItems()
- for _k,_chest in pairs(chests) do
- local chest = peripheral.wrap(_chest)
- local list = chest.list()
- local listNum = #list
- local chestNum = #chests
- for k,_item in pairs(list) do
- term.clear()
- term.setCursorPos(1,1)
- print(tostring(_k).."/"..tostring(chestNum))
- print(tostring(k).."/"..tostring(listNum))
- --tprint(_item)
- --local item = chest.getItem(k)
- local itemTable = {}
- --itemTable.metaData = item.getMetadata()
- --itemTable.displayName = itemTable.metaData.displayName
- itemTable.displayName = _item.name
- itemTable.name = _item.name
- itemTable.count = _item.count
- itemTable.chest = _chest
- itemTable.slot = k
- table.insert(items, itemTable)
- end
- end
- end
- local function Request(itemIndex, count)
- local item = items[itemIndex]
- local chest = peripheral.wrap(item.chest)
- if (count == nil) then
- chest.pushItems("minecraft:chest_2", item.slot)
- else
- chest.pushItems("minecraft:chest_2", item.slot, count)
- end
- end
- local function Deposit()
- local chest = peripheral.wrap("minecraft:chest_2")
- local depositChest = nil
- for k, v in pairs(chests) do
- local depChest = peripheral.wrap(v)
- if (#depChest.list() < depChest.size()) then
- depositChest = v
- end
- end
- if (depositChest == nil) then
- print("No available chest space")
- read()
- end
- for k, item in pairs(chest.list()) do
- chest.pushItems(depositChest, k)
- end
- end
- local function DrawScreen(start)
- term.clear()
- term.setCursorPos(1,1)
- print("Loaded "..#items.." Items")
- for i=start,11+start do
- if (#items >= i) then
- print(i..": "..items[i].displayName.." - "..items[i].count)
- end
- end
- print("\n")
- print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
- end
- local function DrawScreenCollection(start)
- term.clear()
- term.setCursorPos(1,1)
- print("Loaded "..#itemCollection.." Items")
- for i=start,11+start do
- if (#itemCollection >= i) then
- print(i..": "..itemCollection[i].displayName.." - "..itemCollection[i].count)
- end
- end
- print("\n")
- print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
- end
- local function LoadItemCollection()
- for k,item in pairs(items) do
- local found = false
- for _k,_v in pairs(itemCollection) do
- if (_v.name == item.name) then
- itemCollection[_k].count = itemCollection[_k].count + item.count
- table.insert(itemCollection[_k].slots, item)
- found = true
- end
- end
- if (found == false) then
- local itemCollectionEntry = {}
- itemCollectionEntry.count = item.count
- itemCollectionEntry.slots = {}
- itemCollectionEntry.displayName = item.displayName
- itemCollectionEntry.name = item.name
- table.insert(itemCollectionEntry.slots, item)
- table.insert(itemCollection, itemCollectionEntry)
- end
- end
- end
- local function Reload()
- chests = {}
- items = {}
- searchTerm = ""
- DiscoverChests()
- DiscoverItems()
- LoadItemCollection()
- end
- local function DrawSearch(search, start)
- local searchResults = {}
- for k,item in pairs(items) do
- if (string.find(string.lower(item.displayName), search) or string.find(string.lower(item.name), search)) then
- item.index = k
- table.insert(searchResults, item)
- end
- end
- if (start > #searchResults) then
- start = #searchResults
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Found "..#searchResults.." Items")
- if (#searchResults ~= 0) then
- for i=start,11+start do
- if (#searchResults >= i) then
- print(searchResults[i].index..": "..searchResults[i].displayName.." - "..searchResults[i].count)
- end
- end
- end
- print("\n")
- print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
- end
- local function DrawSearchCollection(search, start)
- local searchResults = {}
- for k,item in pairs(itemCollection) do
- if (string.find(string.lower(item.displayName), search) or string.find(string.lower(item.name), search)) then
- item.index = k
- table.insert(searchResults, item)
- end
- end
- if (start > #searchResults) then
- start = #searchResults
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Found "..#searchResults.." Items")
- if (#searchResults ~= 0) then
- for i=start,11+start do
- if (#searchResults >= i) then
- print(searchResults[i].index..": "..searchResults[i].displayName.." - "..searchResults[i].count)
- end
- end
- end
- print("\n")
- print("Commands: dep (deposit items), req (request item <index> <count>), find (find items <text>), col\nPage Left - Left Arrow | Page Right - Right Arrow")
- end
- local function main()
- if (searchTerm == "" or searchTerm == nil) then
- if (usingCollection) then
- DrawScreenCollection(itemPage)
- else
- DrawScreen(itemPage)
- end
- else
- if (usingCollection) then
- DrawSearchCollection(searchTerm, itemPage)
- else
- DrawSearch(searchTerm, itemPage)
- end
- end
- local event, key = os.pullEvent("key")
- if (key == keys.left) then
- itemPage = itemPage - 12
- if (itemPage < 1) then
- itemPage = 1
- end
- main()
- return
- end
- if (key == keys.right) then
- itemPage = itemPage + 12
- if (not usingCollection) then
- if (itemPage > #items) then
- itemPage = #items
- end
- else
- if (itemPage > #itemCollection) then
- itemPage = #itemCollection
- end
- end
- main()
- return
- end
- local input = read()
- if (string.lower(string.sub(input, 1, 3)) == "dep") then
- Deposit()
- Reload()
- end
- if (string.lower(string.sub(input, 1, 3)) == "col") then
- usingCollection = not usingCollection
- itemPage = 1
- end
- if (string.lower(string.sub(input, 1, 3)) == "req") then
- local inputs = split(input)
- local itemIndex = tonumber(inputs[2])
- local count = tonumber(inputs[3])
- if (itemIndex == nil) then
- print("Invalid input - Press Enter to continue")
- read()
- else
- Request(itemIndex, count)
- Reload()
- end
- end
- if (string.lower(string.sub(input, 1, 4)) == "find") then
- itemIndex = 1
- if (split(input)[2] ~= nil) then
- searchTerm = string.lower(split(input)[2])
- else
- searchTerm = ""
- end
- end
- main()
- end
- Reload()
- main()
Add Comment
Please, Sign In to add comment