Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Minecraft Intranet
- Repeater program
- --]]
- print("Minecraft Intranet :: Repeater")
- print()
- -- Program directory
- local progDir = shell.resolve("/"..shell.getRunningProgram().."/..")
- -- Load APIs
- dofile(progDir.."/config")
- -- Load repeater config
- local configPath = "/repeater.conf"
- local defaultConfig = {
- modemSide = "back",
- detectTimeout = 5
- }
- local config = mcnet.config.load(configPath, defaultConfig)
- print(" Computer ID: "..tostring(os.computerID()))
- print(" Modem side: "..config.modemSide)
- print()
- rednet.open(config.modemSide)
- -- Detect surrounding devices
- print("Detecting surrounding devices...")
- rednet.broadcast("deviceOn")
- local surroundings = {}
- local detectTimer = os.startTimer(config.detectTimeout)
- while true do
- local event, p1, p2 = os.pullEvent()
- local refresh = false
- if (event == "rednet_message") then
- -- Add and notify if device started
- if (p1 == "deviceOn") then
- surroundings[p1] = true
- rednet.send(p1, "deviceDetect")
- refresh = true
- -- Add if device detected
- elseif (p1 == "deviceDetect") then
- surroundings[p1] = true
- refresh = true
- -- Remove if device stopped
- elseif (p1 == "deviceOff") then
- surroundings[p1] = nil
- refresh = true
- end
- elseif (event == "timer" and p1 == detectTimer) then
- -- Detection timed out
- break
- end
- if refresh then
- -- Refresh timer
- detectTimer = os.startTimer(config.detectTimeout)
- end
- end
- write("Detected:")
- for deviceId,v in pairs(surroundings) do
- write(" "..deviceId")
- end
- print()
- -- Start listening
- print("Repeating...")
- print("Press Q to quit")
- print()
- while true do
- local event, p1, p2 = os.pullEvent()
- if (event == "rednet_message") then
- -- Forward message to other devices
- for deviceId,v in pairs(surroundings) do
- if (deviceId ~= p1) then
- rednet.broadcast(p2)
- end
- end
- elseif (event == "char" and p1 == "q") then
- -- Confirm quit
- write("Confirm quit? [y/n] > ")
- local event, response = os.pullEvent("char")
- print(response)
- if (response == "y") then
- break
- end
- end
- end
- rednet.broadcast("repeaterOff")
- rednet.close(config.modemSide)
- print("Stopped")
- print()
Advertisement
Add Comment
Please, Sign In to add comment