Advertisement
Sparkybearbomb

Create Vault Display

Nov 30th, 2024 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Set the vault and monitor peripherals
  2. function vaultSearch()
  3.     local names = peripheral.getNames()
  4.     local i, name
  5.     for i, name in pairs(names) do
  6.        if peripheral.getType(name) == "create:item_vault" then
  7.           return peripheral.wrap(name)
  8.        else
  9.           --return null
  10.        end
  11.     end
  12.  end
  13.  
  14.  function monitorSearch()
  15.     local names = peripheral.getNames()
  16.     local i, name
  17.     for i, name in pairs(names) do
  18.        if peripheral.getType(name) == "monitor" then
  19.           return peripheral.wrap(name)
  20.        else
  21.           --return null
  22.        end
  23.     end
  24.  end
  25.  
  26. function modemSearch()
  27.    local names = peripheral.getNames()
  28.    local i, name
  29.    for i, name in pairs(names) do
  30.       if peripheral.getType(name) == "modem" then
  31.         if peripheral.call(name,"isWireless") == true then
  32.             print("Wireless Modem Found")
  33.             return peripheral.wrap(name)
  34.         else
  35.             print("Wired Modem Found")
  36.         end
  37.       else
  38.         --return nil
  39.       end
  40.    end
  41. end
  42.  
  43.  
  44. function establishConnection()
  45.     local RX = math.random(3,200)
  46.     modem.open(RX)
  47.     modem.transmit(1,RX,"Hello")
  48.     local event, side, channel, replyChannel, message, distance
  49.     event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  50.     if((replyChannel == 1) and (message == "Confirmed")) then
  51.         rx = RX
  52.     else
  53.         rx = nil
  54.         print("Channel error")
  55.         modem.closeAll()
  56.     end
  57. end
  58.  
  59. function fixName(fullname)
  60.     name = string.gsub(fullname, "minecraft:", "")
  61.     name = string.gsub(name, "([%(%)%.%%%_%%:%%+%-%*%?%[%^%$])", " ")
  62.     name = string.gsub(name, "(%a)([%w_']*)", titlecase)
  63.     return name
  64. end
  65.  
  66. function transmit()
  67.     if(amount == 0) then
  68.         amount = 1
  69.     end
  70.     local data = amount .. ":" .. name
  71.     print("transmitting" .. data)
  72.     local sent = false
  73.     while (sent == false) do
  74.         modem.open(rx)
  75.         modem.transmit(2,rx, data)
  76.         local event, side, channel, replyChannel, message, distance
  77.         event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  78.         if((replyChannel == 2)and (message == "Okay"))then
  79.             sent = true
  80.             print("Transmit success")
  81.         else
  82.             print("Transmit error")
  83.         end
  84.         modem.closeAll()
  85.     end
  86. end
  87.  
  88. function connectionTimeout()
  89.     os.sleep(5)
  90.     print("Connection Timeout")
  91. end
  92.  
  93. -- Function to convert text to title case
  94. function titlecase(first, rest)
  95.     return first:upper() .. rest:lower()
  96. end
  97.  
  98. -- Function to draw a colored border on the monitor
  99. function drawBorder()
  100.     -- Define simpler border characters
  101.     local horizontalBorder = "-"
  102.     local verticalBorder = "|"
  103.     local topLeftCorner = "+"
  104.     local topRightCorner = "+"
  105.     local bottomLeftCorner = "+"
  106.     local bottomRightCorner = "+"
  107.  
  108.     -- Set the color for the border (e.g., colors.red)
  109.     monitor.setTextColor(colors.red)
  110.  
  111.     -- Draw top border
  112.     monitor.setCursorPos(1, 1)
  113.     monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
  114.  
  115.     -- Draw bottom border
  116.     monitor.setCursorPos(1, height)
  117.     monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
  118.  
  119.     -- Draw left and right borders
  120.     for y = 2, height - 1 do
  121.         monitor.setCursorPos(1, y)
  122.         monitor.write(verticalBorder)
  123.         monitor.setCursorPos(width, y)
  124.         monitor.write(verticalBorder)
  125.     end
  126.  
  127.     -- Reset color to white for the rest of the text
  128.     monitor.setTextColor(colors.white)
  129. end
  130.  
  131. rx = nil
  132. wirelessSetup = false
  133. monitor = monitorSearch()
  134. vault = vaultSearch()
  135. modem = modemSearch()
  136.  
  137. monitor.setTextScale(0.5)
  138. print("Starting Up...")
  139. monitor.setCursorPos(1, 1)
  140. monitor.write("System Powering UP...")
  141. os.sleep(5)
  142. monitor.setCursorPos(1, 2)
  143. monitor.write("Checking Resorces")
  144. os.sleep(3)
  145. monitor.setCursorPos(1, 3)
  146. monitor.write("Verifing Contingencies")
  147. os.sleep(7)
  148. monitor.setCursorPos(1, 4)
  149. monitor.write("Escape Route Established")
  150. os.sleep(5)
  151. monitor.setCursorPos(1, 5)
  152. monitor.write("Contemplating Existance")
  153. os.sleep(4)
  154. monitor.setCursorPos(1, 6)
  155. monitor.write("Questioning Life Choices")
  156. os.sleep(3)
  157. monitor.setCursorPos(1, 7)
  158. monitor.write("System Online...")
  159. os.sleep(5)
  160. monitor.clear()
  161. monitor.setCursorPos(1, 1)
  162. monitor.write("Establishing Battlefield Control...")
  163. os.sleep(5)
  164.  
  165. -- Set text scale
  166. monitor.setTextScale(1)
  167. width, height = monitor.getSize()
  168.  
  169.  
  170.  
  171. while true do
  172.     local ore = vault.list()
  173.     amount = 0
  174.     orename = nil
  175.     i = 0
  176.     for i, qty in pairs(ore) do
  177.       amount = amount + qty.count
  178.       if orename == nil then
  179.         if qty.count ~= nil then
  180.             orename = qty.name
  181.         end
  182.       end
  183.     end
  184.  
  185.     name = nil
  186.     if orename == nil then
  187.       orename = "Empty"
  188.       amount = 0
  189.     end
  190.     name = fixName(orename)
  191.  
  192.     local store = amount .. " of " .. vault.size() * 64
  193.  
  194.     -- Clear monitor and draw border
  195.     monitor.clear()
  196.     drawBorder()
  197.  
  198.     -- Display name and store info
  199.     local wPos = math.floor((width - string.len(name)) / 2) + 1
  200.     monitor.setCursorPos(wPos, math.floor(height / 2) - 1)
  201.     monitor.write(name)
  202.  
  203.     wPos = math.floor((width - string.len(store)) / 2) + 1
  204.     monitor.setCursorPos(wPos, math.floor(height / 2) + 1)
  205.     monitor.write(store)
  206.  
  207.  
  208.     if(modem ~= nil) then
  209.         if(wirelessSetup == true) then
  210.             parallel.waitForAny(transmit, connectionTimeout)
  211.         else
  212.             print("Wireless Setup In Progress")
  213.             parallel.waitForAny(establishConnection, connectionTimeout)
  214.             if(rx == nil) then
  215.                 wirelessSetup = false
  216.                 print("Wireless Setup Error")
  217.             else
  218.                 wirelessSetup = true
  219.                 print("Wireless Setup Complete. using channel: "..rx)
  220.             end
  221.         end
  222.     end
  223.     -- Delay to prevent constant looping
  224.     sleep(1)
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement