Advertisement
craftyoyo

java json minecraft server status

Feb 13th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. package net.mcfr;
  2.  
  3. import org.json.simple.JSONObject;
  4. import org.json.simple.parser.JSONParser;
  5. import org.json.simple.parser.ParseException;
  6.  
  7. public class Server {
  8.  
  9.   public static final String ROLEPLAY = "Faction";
  10.   public static final String FREEBUILD = "freeBuild";
  11.   private static String serversState;
  12.  
  13.   private int playerCount;
  14.   private int capacity;
  15.   private boolean online;
  16.  
  17.   public Server(String label) {
  18.     initServersState();
  19.  
  20.     try {
  21.       JSONObject obj = (JSONObject) new JSONParser().parse(serversState);
  22.       if (this.online = ((JSONObject) obj.get(label)).get("status").equals("online")) {
  23.         this.playerCount = Integer.parseInt(((JSONObject) obj.get(label)).get("onlinePlayers").toString());
  24.         this.capacity = Integer.parseInt(((JSONObject) obj.get(label)).get("maxPlayers").toString());
  25.       }
  26.     } catch (ParseException e) {
  27.       e.printStackTrace();
  28.     }
  29.   }
  30.  
  31.   /**
  32.    * @return le nombre de joueurs présents sur le serveur.
  33.    */
  34.   public String getPlayerCount() {
  35.     return this.playerCount + "";
  36.   }
  37.  
  38.   /**
  39.    * @return la capacité de joueurs du serveur.
  40.    */
  41.   public String getCapacity() {
  42.     return this.capacity + "";
  43.   }
  44.  
  45.   /**
  46.    * @return {@code true} si le serveur est en ligne, {@code false} sinon.
  47.    */
  48.   public boolean isOnline() {
  49.     return this.online;
  50.   }
  51.  
  52.   private static void initServersState() {
  53.     if (serversState == null) {
  54.       serversState = Launcher.readJsonFromUrl("http://localhost/utils/status.php");
  55.     }
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement