Advertisement
Sparkybearbomb

Create Vault Display

Nov 30th, 2024 (edited)
70
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.  
  138. -- Set text scale
  139. monitor.setTextScale(1)
  140. width, height = monitor.getSize()
  141.  
  142.  
  143. while true do
  144.     local ore = vault.list()
  145.     amount = 0
  146.     orename = nil
  147.     i = 0
  148.     for i, qty in pairs(ore) do
  149.       amount = amount + qty.count
  150.       if orename == nil then
  151.         if qty.count ~= nil then
  152.             orename = qty.name
  153.         end
  154.       end
  155.     end
  156.  
  157.     name = nil
  158.     if orename == nil then
  159.       orename = "Empty"
  160.       amount = 0
  161.     end
  162.     name = fixName(orename)
  163.  
  164.     local store = amount .. " of " .. vault.size() * 64
  165.  
  166.     -- Clear monitor and draw border
  167.     monitor.clear()
  168.     drawBorder()
  169.  
  170.     -- Display name and store info
  171.     local wPos = math.floor((width - string.len(name)) / 2) + 1
  172.     monitor.setCursorPos(wPos, math.floor(height / 2) - 1)
  173.     monitor.write(name)
  174.  
  175.     wPos = math.floor((width - string.len(store)) / 2) + 1
  176.     monitor.setCursorPos(wPos, math.floor(height / 2) + 1)
  177.     monitor.write(store)
  178.  
  179.  
  180.     if(modem ~= nil) then
  181.         if(wirelessSetup == true) then
  182.             parallel.waitForAny(transmit, connectionTimeout)
  183.         else
  184.             print("Wireless Setup In Progress")
  185.             parallel.waitForAny(establishConnection, connectionTimeout)
  186.             if(rx == nil) then
  187.                 wirelessSetup = false
  188.                 print("Wireless Setup Error")
  189.             else
  190.                 wirelessSetup = true
  191.                 print("Wireless Setup Complete. using channel: "..rx)
  192.             end
  193.         end
  194.     end
  195.     -- Delay to prevent constant looping
  196.     sleep(1)
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement