SHOW:
|
|
- or go back to the newest paste.
| 1 | --[[ | |
| 2 | ||
| 3 | Minecraft Intranet | |
| 4 | Repeater program | |
| 5 | ||
| 6 | --]] | |
| 7 | ||
| 8 | print("Minecraft Intranet :: Repeater")
| |
| 9 | print() | |
| 10 | ||
| 11 | - | -- Program directory |
| 11 | + | -- Load dependencies |
| 12 | - | local progDir = shell.resolve("/"..shell.getRunningProgram().."/..")
|
| 12 | + | dofile("/common/all")
|
| 13 | program.load("message", "names", "device")
| |
| 14 | - | -- Load APIs |
| 14 | + | program.load("devices/repeater")
|
| 15 | - | dofile(progDir.."/config") |
| 15 | + | |
| 16 | -- Create repeater | |
| 17 | - | -- Load repeater config |
| 17 | + | |
| 18 | local defaultConfig = {
| |
| 19 | device = "repeater", | |
| 20 | - | modemSide = "back", |
| 20 | + | detectTimeout = 1 |
| 21 | - | detectTimeout = 5 |
| 21 | + | |
| 22 | local config = common.loadStore(configPath, defaultConfig) | |
| 23 | - | local config = mcnet.config.load(configPath, defaultConfig) |
| 23 | + | local repeater = mcnet.device.Repeater:new() |
| 24 | print(" Computer ID: "..tostring(os.computerID()))
| |
| 25 | print() | |
| 26 | - | print(" Modem side: "..config.modemSide)
|
| 26 | + | |
| 27 | -- Register handlers | |
| 28 | - | rednet.open(config.modemSide) |
| 28 | + | repeater:on("forwardMessage", function(success, senderId, msg)
|
| 29 | if success then | |
| 30 | - | -- Detect surrounding devices |
| 30 | + | print("Forwarded from "..senderId)
|
| 31 | - | print("Detecting surrounding devices...")
|
| 31 | + | else |
| 32 | - | rednet.broadcast("deviceOn")
|
| 32 | + | print("Failed to forwarded from "..senderId)
|
| 33 | end | |
| 34 | - | local surroundings = {}
|
| 34 | + | end) |
| 35 | ||
| 36 | - | local detectTimer = os.startTimer(config.detectTimeout) |
| 36 | + | repeater:on("char", function(char)
|
| 37 | - | while true do |
| 37 | + | if char == "q" then |
| 38 | - | local event, p1, p2 = os.pullEvent() |
| 38 | + | |
| 39 | - | local refresh = false |
| 39 | + | |
| 40 | local event, char = os.pullEvent("char")
| |
| 41 | - | if (event == "rednet_message") then |
| 41 | + | -- Check response |
| 42 | - | -- Add and notify if device started |
| 42 | + | local response = (string.lower(char) == "y" and "y") or "n" |
| 43 | - | if (p1 == "deviceOn") then |
| 43 | + | |
| 44 | - | surroundings[p1] = true |
| 44 | + | |
| 45 | - | rednet.send(p1, "deviceDetect") |
| 45 | + | repeater:stop() |
| 46 | - | refresh = true |
| 46 | + | |
| 47 | - | -- Add if device detected |
| 47 | + | |
| 48 | - | elseif (p1 == "deviceDetect") then |
| 48 | + | end) |
| 49 | - | surroundings[p1] = true |
| 49 | + | |
| 50 | - | refresh = true |
| 50 | + | repeater:on("error", function(errorType, errorMessage)
|
| 51 | - | -- Remove if device stopped |
| 51 | + | print("[ERROR] "..errorMessage)
|
| 52 | - | elseif (p1 == "deviceOff") then |
| 52 | + | end) |
| 53 | - | surroundings[p1] = nil |
| 53 | + | |
| 54 | - | refresh = true |
| 54 | + | -- Start |
| 55 | print("Starting...")
| |
| 56 | - | elseif (event == "timer" and p1 == detectTimer) then |
| 56 | + | repeater:start(config.detectTimeout) |
| 57 | - | -- Detection timed out |
| 57 | + | write(" Detected:")
|
| 58 | - | break |
| 58 | + | for i,deviceId in pairs(repeater:neighbours()) do |
| 59 | write(" "..deviceId)
| |
| 60 | end | |
| 61 | - | if refresh then |
| 61 | + | |
| 62 | - | -- Refresh timer |
| 62 | + | |
| 63 | - | detectTimer = os.startTimer(config.detectTimeout) |
| 63 | + | |
| 64 | -- Main loop | |
| 65 | print("Listening...")
| |
| 66 | print(" Press Q to quit")
| |
| 67 | - | write("Detected:")
|
| 67 | + | |
| 68 | - | for deviceId,v in pairs(surroundings) do |
| 68 | + | |
| 69 | - | write(" "..deviceId")
|
| 69 | + | while repeater.isRunning do |
| 70 | repeater:trigger(os.pullEvent()) | |
| 71 | end | |
| 72 | ||
| 73 | - | -- Start listening |
| 73 | + | -- Stop |
| 74 | - | print("Repeating...")
|
| 74 | + | |
| 75 | - | print("Press Q to quit")
|
| 75 | + | |
| 76 | print() |