MattiasBuelens

MCNet repeater

Jun 8th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3.     Minecraft Intranet
  4.     Repeater program
  5.  
  6. --]]
  7.  
  8. print("Minecraft Intranet :: Repeater")
  9. print()
  10.  
  11. -- Load dependencies
  12. dofile("/common/all")
  13. program.load("message", "names", "device")
  14. program.load("devices/repeater")
  15.  
  16. -- Create repeater
  17. local configPath = "/repeater.conf"
  18. local defaultConfig = {
  19.     device = "repeater",
  20.     detectTimeout = 1
  21. }
  22. local config = common.loadStore(configPath, defaultConfig)
  23. local repeater = mcnet.device.Repeater:new()
  24. print("  Computer ID: "..tostring(os.computerID()))
  25. print()
  26.  
  27. -- Register handlers
  28. repeater:on("forwardMessage", function(success, senderId, msg)
  29.     if success then
  30.         print("Forwarded from "..senderId)
  31.     else
  32.         print("Failed to forwarded from "..senderId)
  33.     end
  34. end)
  35.  
  36. repeater:on("char", function(char)
  37.     if char == "q" then
  38.         -- Confirm quit
  39.         write("Confirm quit? [y/n] > ")
  40.         local event, char = os.pullEvent("char")
  41.         -- Check response
  42.         local response = (string.lower(char) == "y" and "y") or "n"
  43.         print(response)
  44.         if (response == "y") then
  45.             repeater:stop()
  46.         end
  47.     end
  48. end)
  49.  
  50. repeater:on("error", function(errorType, errorMessage)
  51.     print("[ERROR] "..errorMessage)
  52. end)
  53.  
  54. -- Start
  55. print("Starting...")
  56. repeater:start(config.detectTimeout)
  57. write("  Detected:")
  58. for i,deviceId in pairs(repeater:neighbours()) do
  59.     write(" "..deviceId)
  60. end
  61. print()
  62. print()
  63.  
  64. -- Main loop
  65. print("Listening...")
  66. print("  Press Q to quit")
  67. print()
  68.  
  69. while repeater.isRunning do
  70.     repeater:trigger(os.pullEvent())
  71. end
  72.  
  73. -- Stop
  74. print()
  75. print("Stopped")
  76. print()
Advertisement
Add Comment
Please, Sign In to add comment