View difference between Paste ID: 57ge94gA and fEhwrutB
SHOW: | | - or go back to the newest paste.
1
-- Set the vault and monitor peripherals
2-
function vaultSearch()
2+
function monitorSearch()
3-
    local names = peripheral.getNames()
3+
4-
    local i, name
4+
5-
    for i, name in pairs(names) do
5+
6-
       if peripheral.getType(name) == "create:item_vault" then
6+
      if peripheral.getType(name) == "monitor" then
7-
          return peripheral.wrap(name)
7+
         return peripheral.wrap(name)
8-
       else
8+
9-
          --return null
9+
         --return null
10-
       end
10+
11-
    end
11+
12-
 end
12+
13-
  
13+
14-
 function monitorSearch()
14+
15-
    local names = peripheral.getNames()
15+
16-
    local i, name
16+
17-
    for i, name in pairs(names) do
17+
18-
       if peripheral.getType(name) == "monitor" then
18+
19-
          return peripheral.wrap(name)
19+
         return peripheral.wrap(name)
20-
       else
20+
21-
          --return null
21+
         --return null
22-
       end
22+
23-
    end
23+
24-
 end
24+
25-
  
25+
26
27
28
-- Function to draw a colored border on the monitor
29
function drawBorder()
30
   -- Define simpler border characters
31-
        if peripheral.call(name,"isWireless") == true then
31+
   local horizontalBorder = "-"
32-
            print("Wireless Modem Found") 
32+
   local verticalBorder = "|"
33-
            return peripheral.wrap(name)
33+
   local topLeftCorner = "+"
34-
        else
34+
   local topRightCorner = "+"
35-
            print("Wired Modem Found")
35+
   local bottomLeftCorner = "+"
36-
        end
36+
   local bottomRightCorner = "+"
37
38-
        --return nil
38+
   -- Set the color for the border (e.g., colors.red)
39
   monitor.setTextColor(colors.red)
40
41
   -- Draw top border
42
   monitor.setCursorPos(1, 1)
43
   monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
44-
function establishConnection()
44+
45-
    local RX = math.random(3,200)
45+
   -- Draw bottom border
46-
    modem.open(RX)
46+
   monitor.setCursorPos(1, height)
47-
    modem.transmit(1,RX,"Hello")
47+
   monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
48-
    local event, side, channel, replyChannel, message, distance
48+
49-
    event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
49+
   -- Draw left and right borders
50-
    if((replyChannel == 1) and (message == "Confirmed")) then
50+
   for y = 2, height - 1 do
51-
        rx = RX
51+
       monitor.setCursorPos(1, y)
52-
    else
52+
       monitor.write(verticalBorder)
53-
        rx = nil
53+
       monitor.setCursorPos(width, y)
54-
        print("Channel error")
54+
       monitor.write(verticalBorder)
55-
        modem.closeAll()
55+
56-
    end
56+
57
   -- Reset color to white for the rest of the text
58
   monitor.setTextColor(colors.white)
59-
function fixName(fullname)
59+
60-
    name = string.gsub(fullname, "minecraft:", "")
60+
61-
    name = string.gsub(name, "([%(%)%.%%%_%%:%%+%-%*%?%[%^%$])", " ")
61+
-- Function to add or update entries
62-
    name = string.gsub(name, "(%a)([%w_']*)", titlecase)
62+
function addOrUpdateEntry(name, value, pc) 
63-
    return name
63+
   oreValues[pc] = {oreName = name, qty = value}
64
end
65
66-
function transmit()
66+
function receiveData()
67-
    if(amount == 0) then
67+
   local event, side, channel, replyChannel, message, distance
68-
        amount = 1
68+
   event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
69-
    end
69+
   if((channel == 1) and (message == "Hello")) then
70-
    local data = amount .. ":" .. name
70+
      modem.transmit(replyChannel, 1, "Confirmed")
71-
    print("transmitting" .. data)
71+
   elseif(channel == 2) then
72-
    local sent = false
72+
      charPos = string.find(message,":")
73-
    while (sent == false) do
73+
      oreName = string.sub(message,(charPos+1))
74-
        modem.open(rx)
74+
      qty = string.sub(message,0,(charPos-1))
75-
        modem.transmit(2,rx, data)
75+
      print(oreName.." "..qty)
76-
        local event, side, channel, replyChannel, message, distance
76+
      addOrUpdateEntry(oreName,qty,replyChannel)
77-
        event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
77+
      modem.transmit(replyChannel, 2, "Okay")
78-
        if((replyChannel == 2)and (message == "Okay"))then
78+
79-
            sent = true
79+
80-
            print("Transmit success")
80+
81-
        else
81+
function displayUpdate()
82-
            print("Transmit error")
82+
   monitor.setTextScale(1)
83-
        end
83+
   width, height = monitor.getSize()
84-
        modem.closeAll()
84+
   monitor.clear()
85-
    end
85+
   drawBorder()
86
   dispName = "Ore Collection Values"
87
   wPos = math.floor((width - string.len(dispName)) / 2) + 1
88-
function connectionTimeout()
88+
   hPos = 2
89-
    os.sleep(5)
89+
   monitor.setCursorPos(wPos,hPos)
90-
    print("Connection Timeout")
90+
   monitor.write(dispName)
91
   
92
   if next(oreValues) == nil then
93-
-- Function to convert text to title case
93+
      hPos = hPos + 2
94-
function titlecase(first, rest)
94+
      dispName = "Empty"
95-
    return first:upper() .. rest:lower()
95+
      wPos = math.floor((width - string.len(dispName)) / 2) + 1
96
      monitor.setCursorPos(wPos,hPos)
97-
 
97+
      monitor.write(dispName)
98
      return
99
   end
100-
    -- Define simpler border characters
100+
   
101-
    local horizontalBorder = "-"
101+
   local totalOreValues = {}
102-
    local verticalBorder = "|"
102+
   for pc, entry in pairs(oreValues) do
103-
    local topLeftCorner = "+"
103+
      local oreName = entry.oreName
104-
    local topRightCorner = "+"
104+
      local qty = tonumber(entry.qty) or 0
105-
    local bottomLeftCorner = "+"
105+
      if not totalOreValues[oreName] then
106-
    local bottomRightCorner = "+"
106+
         totalOreValues[oreName] = 0
107-
 
107+
108-
    -- Set the color for the border (e.g., colors.red)
108+
      totalOreValues[oreName] = totalOreValues[oreName] + qty
109-
    monitor.setTextColor(colors.red)
109+
110-
 
110+
   
111-
    -- Draw top border
111+
   for oreName, totalQty in pairs(totalOreValues) do
112-
    monitor.setCursorPos(1, 1)
112+
      hPos = hPos + 2
113-
    monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
113+
      dispName = oreName.. " = ".. totalQty
114-
 
114+
      wPos = math.floor((width - string.len(dispName)) / 2) + 1
115-
    -- Draw bottom border
115+
      monitor.setCursorPos(wPos,hPos)
116-
    monitor.setCursorPos(1, height)
116+
      monitor.write(dispName)
117-
    monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
117+
118-
 
118+
119-
    -- Draw left and right borders
119+
120-
    for y = 2, height - 1 do
120+
-- Attach Peripherals
121-
        monitor.setCursorPos(1, y)
121+
122-
        monitor.write(verticalBorder)
122+
123-
        monitor.setCursorPos(width, y)
123+
124-
        monitor.write(verticalBorder)
124+
-- Open Modem Channels
125-
    end
125+
modem.open(1)
126-
 
126+
modem.open(2)
127-
    -- Reset color to white for the rest of the text
127+
128-
    monitor.setTextColor(colors.white)
128+
-- Create the table
129
oreValues = {}
130
-- Set text scale
131-
rx = nil
131+
132-
wirelessSetup = false
132+
133
while true do
134-
vault = vaultSearch()
134+
   parallel.waitForAll(receiveData,displayUpdate)
135
end
136