Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set the vault and monitor peripherals
- function vaultSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "create:item_vault" then
- return peripheral.wrap(name)
- else
- --return null
- end
- end
- end
- function monitorSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "monitor" then
- return peripheral.wrap(name)
- else
- --return null
- end
- end
- end
- function modemSearch()
- local names = peripheral.getNames()
- local i, name
- for i, name in pairs(names) do
- if peripheral.getType(name) == "modem" then
- if peripheral.call(name,"isWireless") == true then
- print("Wireless Modem Found")
- return peripheral.wrap(name)
- else
- print("Wired Modem Found")
- end
- else
- --return nil
- end
- end
- end
- function establishConnection()
- local RX = math.random(3,200)
- modem.open(RX)
- modem.transmit(1,RX,"Hello")
- local event, side, channel, replyChannel, message, distance
- event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if((replyChannel == 1) and (message == "Confirmed")) then
- rx = RX
- else
- rx = nil
- print("Channel error")
- modem.closeAll()
- end
- end
- function fixName(fullname)
- name = string.gsub(fullname, "minecraft:", "")
- name = string.gsub(name, "([%(%)%.%%%_%%:%%+%-%*%?%[%^%$])", " ")
- name = string.gsub(name, "(%a)([%w_']*)", titlecase)
- return name
- end
- function transmit()
- if(amount == 0) then
- amount = 1
- end
- local data = amount .. ":" .. name
- print("transmitting" .. data)
- local sent = false
- while (sent == false) do
- modem.open(rx)
- modem.transmit(2,rx, data)
- local event, side, channel, replyChannel, message, distance
- event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- if((replyChannel == 2)and (message == "Okay"))then
- sent = true
- print("Transmit success")
- else
- print("Transmit error")
- end
- modem.closeAll()
- end
- end
- function connectionTimeout()
- os.sleep(5)
- print("Connection Timeout")
- end
- -- Function to convert text to title case
- function titlecase(first, rest)
- return first:upper() .. rest:lower()
- end
- -- Function to draw a colored border on the monitor
- function drawBorder()
- -- Define simpler border characters
- local horizontalBorder = "-"
- local verticalBorder = "|"
- local topLeftCorner = "+"
- local topRightCorner = "+"
- local bottomLeftCorner = "+"
- local bottomRightCorner = "+"
- -- Set the color for the border (e.g., colors.red)
- monitor.setTextColor(colors.red)
- -- Draw top border
- monitor.setCursorPos(1, 1)
- monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
- -- Draw bottom border
- monitor.setCursorPos(1, height)
- monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
- -- Draw left and right borders
- for y = 2, height - 1 do
- monitor.setCursorPos(1, y)
- monitor.write(verticalBorder)
- monitor.setCursorPos(width, y)
- monitor.write(verticalBorder)
- end
- -- Reset color to white for the rest of the text
- monitor.setTextColor(colors.white)
- end
- rx = nil
- wirelessSetup = false
- monitor = monitorSearch()
- vault = vaultSearch()
- modem = modemSearch()
- -- Set text scale
- monitor.setTextScale(1)
- width, height = monitor.getSize()
- while true do
- local ore = vault.list()
- amount = 0
- orename = nil
- i = 0
- for i, qty in pairs(ore) do
- amount = amount + qty.count
- if orename == nil then
- if qty.count ~= nil then
- orename = qty.name
- end
- end
- end
- name = nil
- if orename == nil then
- orename = "Empty"
- amount = 0
- end
- name = fixName(orename)
- local store = amount .. " of " .. vault.size() * 64
- -- Clear monitor and draw border
- monitor.clear()
- drawBorder()
- -- Display name and store info
- local wPos = math.floor((width - string.len(name)) / 2) + 1
- monitor.setCursorPos(wPos, math.floor(height / 2) - 1)
- monitor.write(name)
- wPos = math.floor((width - string.len(store)) / 2) + 1
- monitor.setCursorPos(wPos, math.floor(height / 2) + 1)
- monitor.write(store)
- if(modem ~= nil) then
- if(wirelessSetup == true) then
- parallel.waitForAny(transmit, connectionTimeout)
- else
- print("Wireless Setup In Progress")
- parallel.waitForAny(establishConnection, connectionTimeout)
- if(rx == nil) then
- wirelessSetup = false
- print("Wireless Setup Error")
- else
- wirelessSetup = true
- print("Wireless Setup Complete. using channel: "..rx)
- end
- end
- end
- -- Delay to prevent constant looping
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement