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, "createoreexcavation:", "")
61+
-- Function to add or update entries
62-
    name = string.gsub(name, "create:", "")
62+
function addOrUpdateEntry(name, value, pc) 
63-
    name = string.gsub(name, "([%(%)%.%%%_%%:%%+%-%*%?%[%^%$])", " ")
63+
   oreValues[pc] = {oreName = name, qty = value}
64-
    name = string.gsub(name, "(%a)([%w_']*)", titlecase)
64+
65-
    return name
65+
66
function receiveData()
67
   local event, side, channel, replyChannel, message, distance
68-
function transmit()
68+
   event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
69-
    if(amount == 0) then
69+
   if((channel == 1) and (message == "Hello")) then
70-
        amount = 1
70+
      if (type(oreValues[replyChannel]) ~= "table") then
71-
    end
71+
         print("New Device Found")
72-
    local data = amount .. ":" .. name
72+
         modem.transmit(replyChannel, 1, "Confirmed")
73-
    print("transmitting" .. data)
73+
74-
    local sent = false
74+
         print("Conflict Detected, Ignoring")
75-
    while (sent == false) do
75+
76-
        modem.open(rx)
76+
   elseif(channel == 2) then
77-
        modem.transmit(2,rx, data)
77+
      charPos = string.find(message,":")
78-
        local event, side, channel, replyChannel, message, distance
78+
      oreName = string.sub(message,(charPos+1))
79-
        event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
79+
      qty = string.sub(message,0,(charPos-1))
80-
        if((replyChannel == 2)and (message == "Okay"))then
80+
      print(oreName.." "..qty)
81-
            sent = true
81+
      addOrUpdateEntry(oreName,qty,replyChannel)
82-
            print("Transmit success")
82+
      modem.transmit(replyChannel, 2, "Okay")
83-
        else
83+
84-
            print("Transmit error")
84+
85-
        end
85+
86-
        modem.closeAll()
86+
function displayUpdate()
87-
    end
87+
   monitor.setTextScale(1)
88
   width, height = monitor.getSize()
89
   monitor.clear()
90-
function connectionTimeout()
90+
   drawBorder()
91-
    os.sleep(5)
91+
   dispName = "Ore Collection Values"
92-
    print("Connection Timeout")
92+
   wPos = math.floor((width - string.len(dispName)) / 2) + 1
93
   hPos = 2
94
   monitor.setCursorPos(wPos,hPos)
95-
-- Function to convert text to title case
95+
   monitor.write(dispName)
96-
function titlecase(first, rest)
96+
   
97-
    return first:upper() .. rest:lower()
97+
   if next(oreValues) == nil then
98
      hPos = hPos + 2
99-
 
99+
      dispName = "Empty"
100
      wPos = math.floor((width - string.len(dispName)) / 2) + 1
101
      monitor.setCursorPos(wPos,hPos)
102-
    -- Define simpler border characters
102+
      monitor.write(dispName)
103-
    local horizontalBorder = "-"
103+
      return
104-
    local verticalBorder = "|"
104+
105-
    local topLeftCorner = "+"
105+
   
106-
    local topRightCorner = "+"
106+
   local totalOreValues = {}
107-
    local bottomLeftCorner = "+"
107+
   for pc, entry in pairs(oreValues) do
108-
    local bottomRightCorner = "+"
108+
      local oreName = entry.oreName
109-
 
109+
      local qty = tonumber(entry.qty) or 0
110-
    -- Set the color for the border (e.g., colors.red)
110+
      if not totalOreValues[oreName] then
111-
    monitor.setTextColor(colors.red)
111+
         totalOreValues[oreName] = 0
112-
 
112+
113-
    -- Draw top border
113+
      totalOreValues[oreName] = totalOreValues[oreName] + qty
114-
    monitor.setCursorPos(1, 1)
114+
115-
    monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
115+
   
116-
 
116+
   for oreName, totalQty in pairs(totalOreValues) do
117-
    -- Draw bottom border
117+
      hPos = hPos + 2
118-
    monitor.setCursorPos(1, height)
118+
      dispName = oreName.. " = ".. totalQty
119-
    monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
119+
      wPos = math.floor((width - string.len(dispName)) / 2) + 1
120-
 
120+
      monitor.setCursorPos(wPos,hPos)
121-
    -- Draw left and right borders
121+
      monitor.write(dispName)
122-
    for y = 2, height - 1 do
122+
123-
        monitor.setCursorPos(1, y)
123+
124-
        monitor.write(verticalBorder)
124+
125-
        monitor.setCursorPos(width, y)
125+
-- Attach Peripherals
126-
        monitor.write(verticalBorder)
126+
127-
    end
127+
128-
 
128+
129-
    -- Reset color to white for the rest of the text
129+
-- Open Modem Channels
130-
    monitor.setTextColor(colors.white)
130+
modem.open(1)
131
modem.open(2)
132
133-
rx = nil
133+
-- Create the table
134-
wirelessSetup = false
134+
oreValues = {}
135
-- Set text scale
136-
vault = vaultSearch()
136+
137
138
while true do
139-
monitor.setTextScale(0.5)
139+
   parallel.waitForAll(receiveData,displayUpdate)
140-
print("Starting Up...")
140+
141-
monitor.setCursorPos(1, 1)
141+