Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modem = peripheral.find("modem")
- local mainChannel = 68
- local channelPassword = "BigDonkeyButts"
- local player = "goblim"
- local cachedStorage = {}
- function establishChannel()
- while true do
- local rand = math.random(1,6000)
- while true do
- if rand ~= mainChannel then
- modem.transmit(rand,rand,"testChannel")
- os.startTimer(2)
- local event, side, channel, replyChannel, message = os.pullEvent()
- if event == "modem_message" then
- if message == "channelInUse" then
- break
- end
- else
- return rand
- end
- end
- end
- end
- end
- local myChannel = establishChannel()
- function listenForMessages()
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if event == "modem_message" then
- if channel == myChannel then
- if type(message) == "table" then
- if message[1] == "storage" then
- cachedStorage = message[2]
- end
- else
- if message == "testChannel" then
- modem.transmit(replyChannel,myChannel,"channelInUse")
- end
- end
- end
- end
- end
- end
- function sendMessage(sendChannel, replyChannel, message)
- print(sendChannel.. " ".. replyChannel)
- modem.transmit(sendChannel, replyChannel, message)
- end
- function requestItem(player, item, amount)
- sendMessage(mainChannel, myChannel, {channelPassword,"getItem",player,item,amount})
- end
- function requestStorage()
- sendMessage(mainChannel, myChannel, {channelPassword, "requestStorage"})
- end
- function getInput()
- local selectedItem = ""
- local amount = "0"
- local selector = 1
- local text = ""
- while true do
- local event, key = os.pullEvent()
- 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
- requestItem(player,selectedItem,tonumber(amount))
- selectedItem = ""
- amount = "0"
- text = ""
- selector = 1
- drawScreen()
- requestStorage()
- 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(cachedStorage) 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(1, i+5)
- 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 .. " " .. cachedStorage[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(1,2)
- end
- modem.open(myChannel)
- print(myChannel .. " " .. tostring(modem.isOpen(myChannel)))
- sendMessage(mainChannel, myChannel, {channelPassword,"requestStorage"})
- drawScreen()
- parallel.waitForAll(getInput,listenForMessages)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement