I'm making a plugin for my Minecraft server which updates an API every 5 seconds with the amount of players online. Every time the server starts up, it gets stuck while enabling the plugin.
This is the function:
public static void playerListLoop(String status, int onlinePlayers) {
Boolean on = true;
try {
while (on = true) {
try {
URL url = new URL("http://192.168.1.14:3000");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("x-_id", "518747784");
connection.setRequestProperty("x-api-key", "7");
connection.setDoOutput(true);
String urlParameters = "{\"survival\": {\"status\": \"" + status + "\", \"players\": \"" + onlinePlayers + "\"}}";
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())){
wr.write(postData);
}
StringBuilder content;
try (BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String line;
content = new StringBuilder();
while ((line = br.readLine()) != null) {
content.append(line);
content.append(System.lineSeparator());
}
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
Thread.sleep(5 * 1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thanks in advance!