Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Receiver
- -- Define the inventory to move items to
- local storage = ""
- -- Load target inventory from file
- if fs.exists("targetInv") then
- local file = fs.open("targetInv", "r")
- storage = file.readAll()
- file.close()
- end
- -- Prompt user to select target inventory if not set
- if storage == "" then
- -- Find and display available inventories
- local modem = nil
- for i, side in ipairs(peripheral.getNames()) do
- local device = peripheral.getType(side)
- if device == "modem" then
- modem = peripheral.wrap(side)
- break
- end
- end
- if modem then
- local options = modem.getNamesRemote()
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.yellow)
- print("Select a target inventory:\n")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- for i, option in ipairs(options) do
- print(i .. ". " .. option)
- end
- local choice = tonumber(read())
- if choice and choice >= 1 and choice <= #options then
- storage = options[choice]
- local file = fs.open("targetInv", "w")
- file.write(storage)
- file.close()
- term.clear()
- term.setTextColor(colors.green)
- term.setCursorPos(1, 1)
- term.write("Set ")
- term.setTextColor(colors.yellow)
- term.write(storage)
- term.setTextColor(colors.green)
- print(" as target inventory.")
- term.setTextColor(colors.gray)
- print("\n\nPress any key to continue..")
- event, key = os.pullEvent("key")
- term.clear()
- else
- print("Invalid choice.")
- return
- end
- else
- print("No modem detected.")
- return
- end
- end
- --------------------------------------------
- -- Initialize
- local toInventory = peripheral.wrap(storage)
- local itemToTransfer = 0
- local amountToTransfer = 0
- fromInventories = {}
- modem = nil
- modem2 = nil
- -- Connect peripherals
- local sides = {"left", "right", "top", "bottom", "front", "back"}
- for i, side in ipairs(sides) do
- local device = peripheral.getType(side)
- if device == "modem" then
- rednet.open(side)
- if modem == nil then
- modem = peripheral.wrap(side)
- else
- modem2 = peripheral.wrap(side)
- end
- -- print("Opened rednet on " .. side)
- elseif device then
- local wrapper = peripheral.wrap(side)
- -- print("Wrapped " .. device .. " on " .. side)
- end
- end
- function checkWireless(modemName)
- for i, side in ipairs(sides) do
- if modemName ~= nil then
- if modemName.isWireless() then
- return true
- else
- return false
- end
- else
- return "noModem"
- end
- end
- end
- if checkWireless(modem) == "noModem" or checkWireless(modem2) == "noModem" then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.orange)
- print("Missing modems!\n\nNeed one wired and one wireless modem.")
- event, key = os.pullEvent(key)
- term.setTextColor(colors.red)
- print("Rebooting.")
- sleep(3)
- os.reboot()
- end
- if checkWireless(modem) == false and checkWireless(modem2) == false then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.orange)
- print("No >wireless< modem connected!\n\nConnect one and press any key.")
- event, key = os.pullEvent(key)
- term.setTextColor(colors.red)
- print("Rebooting.")
- sleep(3)
- os.reboot()
- elseif checkWireless(modem) == true and checkWireless(modem2) == true then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.orange)
- print("No >wired< modem connected!\n\nConnect one and press any key.")
- event, key = os.pullEvent(key)
- term.setTextColor(colors.red)
- print("Rebooting.")
- sleep(3)
- os.reboot()
- end
- function scanAndSendInventories(fromInventories)
- local scannedItems = {}
- local sentItems = {}
- for i = 1, #fromInventories do
- local inventory = peripheral.wrap(fromInventories[i])
- for slot, item in pairs(inventory.list()) do
- if not scannedItems[item.name] then
- scannedItems[item.name] = item.count
- else
- scannedItems[item.name] = scannedItems[item.name] + item.count
- end
- end
- end
- for itemName, itemCount in pairs(scannedItems) do
- if not sentItems[itemName] then
- sendRednetMessage(itemName, "stockpileupdate")
- sentItems[itemName] = true
- end
- end
- end
- -- Function to send rednet message
- function sendRednetMessage(itemName, protocoll)
- rednet.broadcast("saveItem " .. itemName, protocoll)
- end
- function moveItems()
- -- Loop through the list of inventories
- for i, fromName in ipairs(fromInventories) do
- local fromInventory = peripheral.wrap(fromName)
- -- Get a list of all items in the inventory
- local items = fromInventory.list()
- -- Loop through the items in the inventory
- for slot, item in pairs(items) do
- -- Check if the item is the specified item to transfer
- if item.name == itemToTransfer then
- local transferred = fromInventory.pushItems(peripheral.getName(toInventory), slot, amountToTransfer)
- amountToTransfer = amountToTransfer - transferred
- if amountToTransfer <= 0 then
- return
- end
- end
- end
- end
- end
- function split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function receive()
- senderID, message, protocol = rednet.receive()
- split(message, " ")
- itemToTransfer = tostring(result[1])
- amountToTransfer = tonumber(result[2])
- end
- function wholething()
- receive()
- if protocol == "stockpile" then
- term.setTextColor(colors.white)
- print(itemToTransfer, ": ",amountToTransfer)
- moveItems()
- end
- end
- function updateAvailableItems()
- term.clear()
- term.setCursorPos(1,1)
- print("Scan all attached inventories and send them to my brain?\n")
- term.setTextColor(colors.red)
- print("Hit Enter to confirm.")
- local event, key = os.pullEvent("key")
- if key == keys.enter then
- scanAndSendInventories(fromInventories)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.lime)
- print("Items transmitted!")
- sleep(2)
- os.reboot()
- end
- end
- function retransmitItems()
- event, key = os.pullEvent("key")
- if key == keys.space then
- updateAvailableItems()
- end
- end
- --initialize inventories
- if checkWireless(modem) == false then
- newInventories = modem.getNamesRemote()
- else
- newInventories = modem2.getNamesRemote()
- end
- for _, inventoryName in ipairs(newInventories) do
- if inventoryName ~= storage then
- -- print(inventoryName)
- table.insert(fromInventories, inventoryName)
- end
- end
- ---loopy doo
- while true do
- parallel.waitForAny(retransmitItems, wholething)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement