Created
June 26, 2021 19:25
-
-
Save jannis6023/508caf5037557c6104872dd702953cc5 to your computer and use it in GitHub Desktop.
MCStatus API for Java + ORG.JSON + OKHTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package eu.risemc.DCBot.util; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import org.json.JSONObject; | |
import java.util.ArrayList; | |
public class MCStatus { | |
private String ip; | |
private ArrayList<String> motd; | |
private String iconURL; | |
private boolean success; | |
public MCStatus(String ip) { | |
OkHttpClient client = new OkHttpClient().newBuilder() | |
.build(); | |
Request request = new Request.Builder() | |
.url("https://api.mcsrvstat.us/2/" + ip) | |
.method("GET", null) | |
.build(); | |
try { | |
Response response = client.newCall(request).execute(); | |
JSONObject obj = new JSONObject(response.body().string()); | |
JSONObject debugObj = obj.getJSONObject("debug"); | |
if(debugObj.getBoolean("ping")){ | |
ArrayList<String> motd = new ArrayList<>(); | |
motd.add(obj.getJSONObject("motd").getJSONArray("clean").getString(0)); | |
motd.add(obj.getJSONObject("motd").getJSONArray("clean").getString(1)); | |
this.ip = ip; | |
this.motd = motd; | |
this.iconURL = obj.getString("icon"); | |
success = true; | |
}else{ | |
success = false; | |
} | |
}catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
public String getIp() { | |
return ip; | |
} | |
public ArrayList<String> getMotd() { | |
return motd; | |
} | |
public String getIconURL() { | |
return iconURL; | |
} | |
public boolean isSuccess() { | |
return success; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment