MattiasBuelens

MCNet mainframe

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