Advertisement
bcash8

Storage System Pocket Program

Nov 16th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.11 KB | None | 0 0
  1. modem = peripheral.find("modem")
  2. local mainChannel = 68
  3. local channelPassword = "BigDonkeyButts"
  4. local player = "goblim"
  5. local cachedStorage = {}
  6.  
  7. function establishChannel()
  8.     while true do
  9.         local rand = math.random(1,6000)
  10.         while true do
  11.             if rand ~= mainChannel then
  12.                 modem.transmit(rand,rand,"testChannel")
  13.                 os.startTimer(2)
  14.                 local event, side, channel, replyChannel, message = os.pullEvent()
  15.                 if event == "modem_message" then
  16.                     if message == "channelInUse" then
  17.                         break
  18.                     end
  19.                 else
  20.                     return rand
  21.                 end
  22.             end
  23.         end
  24.     end
  25. end
  26.  
  27. local myChannel = establishChannel()
  28.  
  29. function listenForMessages()
  30.     while true do
  31.         local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  32.         if event == "modem_message" then
  33.             if channel == myChannel then
  34.                 if type(message) == "table" then
  35.                     if message[1] == "storage" then
  36.                         cachedStorage = message[2]
  37.                     end
  38.                 else
  39.                     if message == "testChannel" then
  40.                         modem.transmit(replyChannel,myChannel,"channelInUse")
  41.                     end
  42.                 end
  43.             end
  44.         end
  45.     end
  46. end
  47.  
  48. function sendMessage(sendChannel, replyChannel, message)
  49.     print(sendChannel.. " ".. replyChannel)
  50.     modem.transmit(sendChannel, replyChannel, message)
  51. end
  52.  
  53. function requestItem(player, item, amount)
  54.     sendMessage(mainChannel, myChannel, {channelPassword,"getItem",player,item,amount})
  55. end
  56.  
  57. function requestStorage()
  58.     sendMessage(mainChannel, myChannel, {channelPassword, "requestStorage"})
  59. end
  60.  
  61. function getInput()
  62.     local selectedItem = ""
  63.     local amount = "0"
  64.     local selector = 1
  65.     local text = ""
  66.     while true do
  67.         local event, key = os.pullEvent()
  68.         if event == "char" or event == "key" then
  69.             if event == "char" then
  70.                 if selectedItem == "" then
  71.                     text = text .. key
  72.                 elseif tonumber(key) then
  73.                     amount = amount .. key
  74.                 end
  75.             elseif event == "key" then
  76.                 if key == keys.backspace then
  77.                     text = string.sub(text, 1, -2)
  78.                     selectedItem = ""
  79.                     amount = "0"
  80.                     selector = 1
  81.                 elseif key == keys.enter then
  82.                     if selectedItem == "" then
  83.                         if #list > 0 then
  84.                             selectedItem = list[selector]
  85.                         end
  86.                     elseif tonumber(amount) > 0 then
  87.                         requestItem(player,selectedItem,tonumber(amount))
  88.                         selectedItem = ""
  89.                         amount = "0"
  90.                         text = ""
  91.                         selector = 1
  92.                         drawScreen()
  93.                         requestStorage()
  94.                     end
  95.                 elseif key == keys.down then
  96.                     selector = selector + 1
  97.                     if selector > 10 then
  98.                         selector = 10
  99.                     end
  100.                 elseif key == keys.up then
  101.                     selector = selector - 1
  102.                     if selector < 1 then
  103.                         selector = 1
  104.                     end
  105.                 end
  106.             end
  107.  
  108.             drawScreen()
  109.             print(text)
  110.             if selectedItem ~= "" then
  111.                 term.setCursorPos(1,3)
  112.                 print(tonumber(amount))
  113.             end
  114.             local stringLength = string.len(text)
  115.             if stringLength > 1 then
  116.                 list = {}
  117.                 for name, _ in pairs(cachedStorage) do
  118.                     if string.find(name, text) then
  119.                         table.insert(list, name)
  120.                         if #list >= 10 then
  121.                             break
  122.                         end
  123.                     end
  124.                 end
  125.                 for i, name in pairs(list) do
  126.                     term.setCursorPos(1, i+5)
  127.                     if i == selector then
  128.                         term.setBackgroundColor(colors.blue)
  129.                     else
  130.                         term.setBackgroundColor(colors.gray)
  131.                     end
  132.                     local colon = string.find(name, ":")+1
  133.                     local DisplayName = string.sub(name, colon)
  134.                     print(DisplayName .. " " .. cachedStorage[name].count)
  135.                 end
  136.             end
  137.         end
  138.     end
  139. end
  140.  
  141. function drawScreen()
  142.     term.setBackgroundColor(colors.gray)
  143.     term.clear()
  144.     term.setCursorPos(1,1)
  145.     term.write("Type Item Name")
  146.     term.setCursorPos(1,2)
  147. end
  148.  
  149.  
  150. modem.open(myChannel)
  151. print(myChannel .. " " .. tostring(modem.isOpen(myChannel)))    
  152. sendMessage(mainChannel, myChannel, {channelPassword,"requestStorage"})
  153. drawScreen()
  154. parallel.waitForAll(getInput,listenForMessages)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement