[Resource] Check if a Mojang server is online/offline

Discussion in 'Resources' started by sebasju1234, Jan 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    sebasju1234

    - This code was pure crap. Removed it. -
     
    GregMC and Yonas like this.
  2. Offline

    thepaperboy99

    Pretty cool resource, thanks. :)
     
  3. Offline

    TheTinySpider

    Pretty cool resource, I thought I would expand it a little:

    Code:java
    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStreamReader;
    4. import java.net.URL;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.json.simple.JSONObject;
    8. import org.json.simple.parser.JSONParser;
    9. import org.json.simple.parser.ParseException;
    10.  
    11. public enum StatusChecker {
    12. ACCOUNTS("Accounts Service", "account.mojang.com"),
    13. AUTHENTICATION("Authentication Service", "auth.mojang.com"),
    14. AUTHENTICATION_SERVER("Authentication Server", "authserver.mojang.com"),
    15. LOGIN("Login Service", "login.minecraft.net"),
    16. SESSION_MINECRAFT("Minecraft Session Server", "session.minecraft.net"),
    17. SESSION_MOJANG("Mojang Session Server", "sessionserver.mojang.com"),
    18. SKINS("Skin Server", "skins.minecraft.net"),
    19. MAIN_WEBSITE("Main Site", "minecraft.net");
    20.  
    21. /*
    22.   * @Author TheTinySpider
    23.   * Idea from: sebasju1234
    24.   *
    25.   * Status names and other information can be found at:
    26.   * [url]http://minecraft.gamepedia.com/User_talk:Oxguy3/Minecraft.net_API[/url]
    27.   */
    28.  
    29. private String name, serviceURL;
    30. private JSONParser jsonParser = new JSONParser();
    31.  
    32. StatusChecker(String name, String serviceURL) {
    33. this.name = name;
    34. this.serviceURL = serviceURL;
    35. }
    36.  
    37. public String getName() {
    38. return name;
    39. }
    40.  
    41. /**
    42.   * Check the current Mojang service for it's status, errors are ignored.
    43.   *
    44.   * @return Status of the service.
    45.   */
    46. public Status getStatus() {
    47. return getStatus(true);
    48. }
    49.  
    50. /**
    51.   * Check the current Mojang service for it's status.
    52.   *
    53.   * @param suppressErrors - Don't print errors in console.
    54.   * @return Status of the service.
    55.   */
    56. public Status getStatus(boolean suppressErrors) {
    57. try {
    58. URL url = new URL("[url]http://status.mojang.com/check?service=[/url]" + serviceURL);
    59. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
    60.  
    61. Object object = jsonParser.parse(bufferedReader);
    62. JSONObject jsonObject = (JSONObject) object;
    63.  
    64. String status = (String) jsonObject.get(serviceURL);
    65.  
    66. return Status.get(status);
    67.  
    68. } catch (IOException | ParseException exception) {
    69.  
    70. if (!suppressErrors) {
    71. exception.printStackTrace();
    72. }
    73.  
    74. return Status.UNKNOWN;
    75. }
    76. }
    77.  
    78. public enum Status {
    79. ONLINE("Online", ChatColor.GREEN.toString(), "No problems detected!"),
    80. UNSTABLE("Unstable", ChatColor.YELLOW.toString(), "May be experiencing issues..."),
    81. OFFLINE("Offline", ChatColor.DARK_RED.toString(), "Experiencing problems!"),
    82. UNKNOWN("Unknown", ChatColor.WHITE.toString(), "Couldn't connect to Mojang!");
    83.  
    84. private String status, color, description;
    85.  
    86. Status(String status, String color, String description) {
    87. this.status = status;
    88. this.color = color;
    89. this.description = description;
    90. }
    91.  
    92. public String getStatus() {
    93. return status;
    94. }
    95.  
    96. public String getColor() {
    97. return color;
    98. }
    99.  
    100. public String getDescription() {
    101. return description;
    102. }
    103.  
    104. public static Status get(String status) {
    105. status = status.toLowerCase();
    106.  
    107. switch (status) {
    108. case "green":
    109. return Status.ONLINE;
    110. case "yellow":
    111. return Status.UNSTABLE;
    112. case "red":
    113. return Status.OFFLINE;
    114. default:
    115. return Status.UNKNOWN;
    116. }
    117. }
    118. }
    119. }


    #Edit
    It seems to work pretty smooth, I would still suggest doing it ASync.
    Here is a little example on how to use it:

    Code:java
    1. for (StatusChecker statusChecker : StatusChecker.values()) {
    2. String service = statusChecker.getName();
    3. Status status = statusChecker.getStatus(false);
    4.  
    5. Bukkit.broadcastMessage(service + ": " + status.getColor() + status.getStatus() + " - " + status.getDescription());
    6. }


    Hope you enjoy!
     
    Skyost likes this.
  4. Offline

    Yonas

    Skyost likes this.
  5. Offline

    Garris0n

    You know, you can just do return status.equals("green")...
     
    Skyost likes this.
  6. Offline

    sebasju1234

  7. Offline

    Flybelette

    With isOnline("sessionserver") I got an HTTP response code: 400, Any Idea ? ^^
     
  8. Offline

    Yonas

    Flybelette change "sessionserver" to "session" .

    edit#
    sessionserver works only with mojang.com not minecraft.net
     
  9. Offline

    TeamJesus

    pritty good idea.. so how would one call this on a timer, say every 20 or so min?

    Cheers
     
  10. Offline

    sebasju1234

    Use a BukkitRunnable for that.
     
Thread Status:
Not open for further replies.

Share This Page