Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local OUTPUT = "minecraft:barrel_7"
- local INPUT = "minecraft:barrel_8"
- local playerInventories = {goblim = {adjacentInventory = "minecraft:barrel_9", inventoryManager = "inventoryManager_0"},
- WhiteCarsMatter = {adjacentInventory = "minecraft:barrel_10", inventoryManager = "inventoryManager_1"},
- beaudog = {adjacentInventory = "minecraft:barrel_11", inventoryManager = "inventoryManager_2"}
- }
- local chests = {peripheral.find("minecraft:chest")}
- local storage = {} --"item" = {location = {"chestName" = {1,2,3}}, count = 0}
- local movingItems = false
- local checkInvList = {INPUT, "enderstorage:ender_chest_0"}
- --modem setup
- local listenChannel = 68
- local channelPassword = "BigDonkeyButts"
- local modem = peripheral.wrap("back")
- if not modem.isOpen(listenChannel) then modem.open(listenChannel) end -- open our modem
- print(modem.isOpen(listenChannel))
- --local OUTPUT = "minecraft:chest_3"
- function getStorage()
- for _ , chest in pairs(chests) do
- for slot, item in pairs(chest.list()) do
- if not storage[item.name] then storage[item.name] = {count = 0, location = {}} end
- storage[item.name].count = storage[item.name].count + item.count
- if storage[item.name].location[peripheral.getName(chest)] == nil then
- storage[item.name].location[peripheral.getName(chest)] = {}
- table.insert(storage[item.name].location[peripheral.getName(chest)], slot)
- else
- table.insert(storage[item.name].location[peripheral.getName(chest)], slot)
- end
- end
- end
- end
- function getItem(name,amount,destInv)
- if not storage[name] then return false end
- destInv = destInv or OUTPUT
- movingItems = true
- drawScreen()
- term.setCursorPos(1,2)
- print("Retrieving " .. name)
- term.setCursorPos(1,4)
- for chestName, slots in pairs(storage[name].location) do
- coroutine.yield()
- local chest = peripheral.wrap(chestName)
- for i, slot in pairs(slots) do
- local movedItems = chest.pushItems(destInv,slot,amount)
- storage[name].count = storage[name].count - movedItems
- amount = amount - movedItems
- local details = chest.getItemDetail(slot)
- if not details then
- table.remove(storage[name].location[chestName],i)
- end
- if storage[name].count <= 0 then
- storage[name] = nil
- end
- if amount <= 0 then movingItems = false return true end
- end
- end
- movingItems = false
- end
- function putInInventory(player, item, amount)
- term.setCursorPos(1,10)
- getItem(item,amount,playerInventories[player].adjacentInventory)
- local manager = peripheral.wrap(playerInventories[player].inventoryManager)
- manager.addItemToPlayer("UP",amount,-1,item)
- end
- function moveToInv(destInv, destSlot, originInv, originSlot, amount)
- local fromChest = peripheral.wrap(originInv)
- local itemsMoved = fromChest.pushItems(destInv, originSlot, amount, destSlot)
- return itemsMoved
- end
- function findItemStackWithSpace(item)
- for chestName, slots in pairs(storage[item].location) do -- check every known instance of that item in the storage
- local testChest = peripheral.wrap(chestName)
- for i, slot in pairs(slots) do
- local space = 64 - testChest.getItemDetail(slot).count
- if space > 0 then
- print("found part stack at: " .. chestName .. " " .. slot .. " " .. space)
- return true, chestName, slot, space
- end
- end
- end
- return false
- end
- function findEmptySpace(lastOpenSlot)
- local startInv = lastOpenSlot[1]
- local startSlot = lastOpenSlot[2]
- local startPos = 1
- if startInv ~= "" then
- for i, chest in pairs(chests) do
- if peripheral.getName(chest) == startInv then
- startPos = i
- break
- end
- end
- end
- for i=startPos,#chests do
- local chest = chests[i]
- for b=startSlot, chest.size() do
- if not chest.getItemDetail(b) then
- return {peripheral.getName(chest), b}
- end
- end
- end
- return {"",1}
- end
- function checkInputs()
- while true do
- sleep(1)
- local lastOpenSlot = {"",1}
- local testedItems = {}
- for _, inputChest in pairs(checkInvList) do --check every input chest
- local thisInv = peripheral.wrap(inputChest)
- for inputSlot, item in pairs(thisInv.list()) do -- chest every slot in that chest
- local numberOfItems = item.count
- if storage[item.name] then
- if not testedItems[item.name] then
- testedItems[item.name] = true
- local worked, chestName, slot, space = findItemStackWithSpace(item.name)
- if worked then
- local movedItems = moveToInv(chestName,slot,inputChest,inputSlot,numberOfItems)
- storage[item.name].count = storage[item.name].count + movedItems
- numberOfItems = numberOfItems - movedItems
- if numberOfItems <= 0 then break end
- end
- end
- end
- if numberOfItems > 0 then
- lastOpenSlot = findEmptySpace(lastOpenSlot)
- local movedItems = moveToInv(lastOpenSlot[1], lastOpenSlot[2], inputChest, inputSlot, numberOfItems)
- if not storage[item.name] then storage[item.name] = {count = 0, location = {}} end
- if storage[item.name].location[lastOpenSlot[1]] == nil then
- storage[item.name].location[lastOpenSlot[1]] = {}
- table.insert(storage[item.name].location[lastOpenSlot[1]], lastOpenSlot[2])
- else
- table.insert(storage[item.name].location[lastOpenSlot[1]], lastOpenSlot[2])
- end
- storage[item.name].count = storage[item.name].count + movedItems
- end
- end
- end
- end
- end
- function commands()
- local text = ""
- local amount = "0"
- local list = {}
- local selectedItem = ""
- local selector = 1
- while true do
- local event, key = os.pullEvent()
- term.setCursorPos(1,2)
- if event == "char" or event == "key" then
- if event == "char" then
- if selectedItem == "" then
- text = text .. key
- elseif tonumber(key) then
- amount = amount .. key
- end
- elseif event == "key" then
- if key == keys.backspace then
- text = string.sub(text, 1, -2)
- selectedItem = ""
- amount = "0"
- selector = 1
- elseif key == keys.enter then
- if selectedItem == "" then
- if #list > 0 then
- selectedItem = list[selector]
- end
- elseif tonumber(amount) > 0 then
- getItem(selectedItem, tonumber(amount))
- selectedItem = ""
- amount = "0"
- text = ""
- selector = 1
- end
- elseif key == keys.down then
- selector = selector + 1
- if selector > 10 then
- selector = 10
- end
- elseif key == keys.up then
- selector = selector - 1
- if selector < 1 then
- selector = 1
- end
- end
- end
- drawScreen()
- print(text)
- if selectedItem ~= "" then
- term.setCursorPos(1,3)
- print(tonumber(amount))
- end
- local stringLength = string.len(text)
- if stringLength > 1 then
- list = {}
- for name, _ in pairs(storage) do
- if string.find(name, text) then
- table.insert(list, name)
- if #list >= 10 then
- break
- end
- end
- end
- for i, name in pairs(list) do
- term.setCursorPos(18, i)
- if i == selector then
- term.setBackgroundColor(colors.blue)
- else
- term.setBackgroundColor(colors.gray)
- end
- local colon = string.find(name, ":")+1
- local DisplayName = string.sub(name, colon)
- print(DisplayName .. " " .. storage[name].count)
- end
- end
- end
- end
- end
- function drawScreen()
- term.setBackgroundColor(colors.gray)
- term.clear()
- term.setCursorPos(1,1)
- term.write("Type Item Name")
- term.setCursorPos(20,1)
- --term.setBackgroundColor(colors.white)
- --term.write("Get")
- --term.setBackgroundColor(colors.lightBlue)
- --term.setCursorPos(25, 1)
- --term.write("Craft")
- --term.setBackgroundColor(colors.gray)
- term.setCursorPos(1,2)
- end
- function listenForMessages()
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if channel == listenChannel then
- if type(message) == "table" then
- if message[1] == channelPassword then
- if message[2] == "requestStorage" then
- modem.transmit(replyChannel, listenChannel, {"storage",storage})
- elseif message[2] == "getItem" then
- modem.transmit(replyChannel, listenChannel, message[4])
- putInInventory(message[3],message[4],message[5])
- end
- end
- end
- end
- end
- end
- getStorage()
- drawScreen()
- parallel.waitForAll(checkInputs, commands, listenForMessages)
Add Comment
Please, Sign In to add comment