Advertisement
Sparkybearbomb

Create Vault Display

Nov 30th, 2024 (edited)
170
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, "createoreexcavation:", "")
  62.     name = string.gsub(name, "create:", "")
  63.     name = string.gsub(name, "([%(%)%.%%%_%%:%%+%-%*%?%[%^%$])", " ")
  64.     name = string.gsub(name, "(%a)([%w_']*)", titlecase)
  65.     return name
  66. end
  67.  
  68. function transmit()
  69.     if(amount == 0) then
  70.         amount = 1
  71.     end
  72.     local data = amount .. ":" .. name
  73.     print("transmitting" .. data)
  74.     local sent = false
  75.     while (sent == false) do
  76.         modem.open(rx)
  77.         modem.transmit(2,rx, data)
  78.         local event, side, channel, replyChannel, message, distance
  79.         event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  80.         if((replyChannel == 2)and (message == "Okay"))then
  81.             sent = true
  82.             print("Transmit success")
  83.         else
  84.             print("Transmit error")
  85.         end
  86.         modem.closeAll()
  87.     end
  88. end
  89.  
  90. function connectionTimeout()
  91.     os.sleep(5)
  92.     print("Connection Timeout")
  93. end
  94.  
  95. -- Function to convert text to title case
  96. function titlecase(first, rest)
  97.     return first:upper() .. rest:lower()
  98. end
  99.  
  100. -- Function to draw a colored border on the monitor
  101. function drawBorder()
  102.     -- Define simpler border characters
  103.     local horizontalBorder = "-"
  104.     local verticalBorder = "|"
  105.     local topLeftCorner = "+"
  106.     local topRightCorner = "+"
  107.     local bottomLeftCorner = "+"
  108.     local bottomRightCorner = "+"
  109.  
  110.     -- Set the color for the border (e.g., colors.red)
  111.     monitor.setTextColor(colors.red)
  112.  
  113.     -- Draw top border
  114.     monitor.setCursorPos(1, 1)
  115.     monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
  116.  
  117.     -- Draw bottom border
  118.     monitor.setCursorPos(1, height)
  119.     monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
  120.  
  121.     -- Draw left and right borders
  122.     for y = 2, height - 1 do
  123.         monitor.setCursorPos(1, y)
  124.         monitor.write(verticalBorder)
  125.         monitor.setCursorPos(width, y)
  126.         monitor.write(verticalBorder)
  127.     end
  128.  
  129.     -- Reset color to white for the rest of the text
  130.     monitor.setTextColor(colors.white)
  131. end
  132.  
  133. rx = nil
  134. wirelessSetup = false
  135. monitor = monitorSearch()
  136. vault = vaultSearch()
  137. modem = modemSearch()
  138.  
  139. monitor.setTextScale(0.5)
  140. print("Starting Up...")
  141. monitor.setCursorPos(1, 1)
  142. monitor.write("System Powering UP...")
  143. os.sleep(5)
  144. monitor.setCursorPos(1, 2)
  145. monitor.write("Checking Resorces")
  146. os.sleep(3)
  147. monitor.setCursorPos(1, 3)
  148. monitor.write("Verifing Contingencies")
  149. os.sleep(7)
  150. monitor.setCursorPos(1, 4)
  151. monitor.write("Escape Route Established")
  152. os.sleep(5)
  153. monitor.setCursorPos(1, 5)
  154. monitor.write("Contemplating Existance")
  155. os.sleep(4)
  156. monitor.setCursorPos(1, 6)
  157. monitor.write("Questioning Life Choices")
  158. os.sleep(3)
  159. monitor.setCursorPos(1, 7)
  160. monitor.write("System Online...")
  161. os.sleep(5)
  162. monitor.clear()
  163. monitor.setCursorPos(1, 1)
  164. monitor.write("Establishing Battlefield Control...")
  165. os.sleep(5)
  166.  
  167. -- Set text scale
  168. monitor.setTextScale(1)
  169. width, height = monitor.getSize()
  170.  
  171.  
  172.  
  173. while true do
  174.     local ore = vault.list()
  175.     amount = 0
  176.     orename = nil
  177.     i = 0
  178.     for i, qty in pairs(ore) do
  179.       amount = amount + qty.count
  180.       if orename == nil then
  181.         if qty.count ~= nil then
  182.             orename = qty.name
  183.         end
  184.       end
  185.     end
  186.  
  187.     name = nil
  188.     if orename == nil then
  189.       orename = "Empty"
  190.       amount = 0
  191.     end
  192.     name = fixName(orename)
  193.  
  194.     local store = amount .. " of " .. vault.size() * 64
  195.  
  196.     -- Clear monitor and draw border
  197.     monitor.clear()
  198.     drawBorder()
  199.  
  200.     -- Display name and store info
  201.     local wPos = math.floor((width - string.len(name)) / 2) + 1
  202.     monitor.setCursorPos(wPos, math.floor(height / 2) - 1)
  203.     monitor.write(name)
  204.  
  205.     wPos = math.floor((width - string.len(store)) / 2) + 1
  206.     monitor.setCursorPos(wPos, math.floor(height / 2) + 1)
  207.     monitor.write(store)
  208.  
  209.  
  210.     if(modem ~= nil) then
  211.         if(wirelessSetup == true) then
  212.             parallel.waitForAny(transmit, connectionTimeout)
  213.         else
  214.             print("Wireless Setup In Progress")
  215.             parallel.waitForAny(establishConnection, connectionTimeout)
  216.             if(rx == nil) then
  217.                 wirelessSetup = false
  218.                 print("Wireless Setup Error")
  219.             else
  220.                 wirelessSetup = true
  221.                 print("Wireless Setup Complete. using channel: "..rx)
  222.             end
  223.         end
  224.     end
  225.     -- Delay to prevent constant looping
  226.     sleep(1)
  227. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement