Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
1.Optimize json parsing.
2.No data returned is changed to the string "null".(players data is -1)
  • Loading branch information
Pmaru-top authored Jul 22, 2021
1 parent 06e3330 commit d024a8f
Showing 1 changed file with 66 additions and 48 deletions.
114 changes: 66 additions & 48 deletions tax/cute/minecraftserverping/MCPing.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MCPing {
private String version_name;
Expand All @@ -18,9 +19,9 @@ public class MCPing {
private String type;
private int mod_count;
private int delay;
private ArrayList<String> modList;
// this constructor don't need to public
private List<String> modList;

// this constructor don't need to public
private MCPing(
String version_name,
String version_protocol,
Expand All @@ -31,7 +32,7 @@ private MCPing(
String type,
int mod_count,
int delay,
ArrayList<String> modList
List<String> modList
) {
this.version_name = version_name;
this.version_protocol = version_protocol;
Expand All @@ -44,29 +45,26 @@ private MCPing(
this.modList = modList;
this.delay = delay;
}
public static MCPing getMotd(String host, int port) throws IOException{
return MCPing.getMotd(host, port, 5000);

public static MCPing getMotd(String host, int port) throws IOException {
return MCPing.getMotd(host, port, 5000);
}

public static MCPing getMotd(String host, int port, int timeout) throws IOException {
// initialization
ArrayList<String> modList = new ArrayList<>();
String description = "";
List<String> modList = new ArrayList<>();
JSONObject version_json;
String version_protocol = null;
String version_name = null;
String description = "";
String version_protocol = "null";
String version_name = "null";
String favicon = "null";
String type = "null";
JSONObject players_json;
int max_players = -1;
int online_players = -1;
int mod_count = 0;
String favicon = null;
String type = null;
JSONObject modinfo_json;
JSONArray modList_json;
JSONObject description_json;

// connection
// connection
Socket socket = new Socket();

String _encode_host = Punycode.encodeURL(host);
Expand Down Expand Up @@ -105,8 +103,8 @@ public static MCPing getMotd(String host, int port, int timeout) throws IOExcept
out.close();
in.close();
socket.close();
String data = new String(bytes,"UTF-8"); // get json

String data = new String(bytes, "UTF-8"); // get json
// json parsing
JSONObject data_json = JSONObject.parseObject(data);

Expand All @@ -121,46 +119,66 @@ public static MCPing getMotd(String host, int port, int timeout) throws IOExcept
}

if (data_json.containsKey("players")) {
players_json = data_json.getJSONObject("players");
if (players_json.get("max") instanceof Integer) {
max_players = players_json.getIntValue("max");
}
if (players_json.get("online") instanceof Integer) {
online_players = players_json.getIntValue("online");
if (data_json.get("players") instanceof JSONObject) {
players_json = data_json.getJSONObject("players");
if (players_json.get("max") instanceof Integer) {
max_players = players_json.getInteger("max");
}

if (players_json.get("online") instanceof Integer) {
online_players = players_json.getIntValue("online");
}
}
}

if (data_json.get("favicon") instanceof String) {
favicon = data_json.getString("favicon").split(",")[1];
if (data_json.containsKey("favicon")) {
if (data_json.get("favicon") instanceof String) {
favicon = data_json.getString("favicon").split(",")[1];
}
}

if (data_json.containsKey("modinfo")) {
modinfo_json = data_json.getJSONObject("modinfo");
type = modinfo_json.getString("type");
modList_json = modinfo_json.getJSONArray("modList");
mod_count = modList_json.size();
for (int i = 0; i < modList_json.size(); i++) {
modList.add(modList_json.getJSONObject(i).getString("modid"));
if (data_json.get("modinfo") instanceof JSONObject) {
JSONObject modinfo_json = data_json.getJSONObject("modinfo");

if (modinfo_json.get("type") instanceof String) {
type = modinfo_json.getString("type");
}

if (modinfo_json.get("modList") instanceof JSONArray) {
JSONArray modList_json = modinfo_json.getJSONArray("modList");
mod_count = modList_json.size();
for (int i = 0; i < modList_json.size(); i++) {
modList.add(modList_json.getJSONObject(i).getString("modid"));
}
}
}
}

if (data_json.containsKey("description")) {
if (data_json.get("description") instanceof String) {
description = data_json.getString("description");
} else {
description_json = data_json.getJSONObject("description");
if (description_json.containsKey("text")) {
description = description_json.getString("text");
}
if (description_json.containsKey("extra")) {
JSONArray extra_array = description_json.getJSONArray("extra");
JSONObject text_json;
for (int i = 0; i < extra_array.size(); i++) {
text_json = extra_array.getJSONObject(i);
description += text_json.getString("text");
try {
if (data_json.containsKey("description")) {
if (data_json.get("description") instanceof String) {
description = data_json.getString("description");
} else if (data_json.get("description") instanceof JSONObject) {
JSONObject description_json = data_json.getJSONObject("description");
if (description_json.containsKey("text")) {
description = description_json.getString("text");
}
if (description_json.containsKey("extra")) {
JSONArray extra_array = description_json.getJSONArray("extra");
JSONObject text_json;
for (int i = 0; i < extra_array.size(); i++) {
text_json = extra_array.getJSONObject(i);
if (text_json.containsKey("text")) {
description += text_json.getString("text");
}
}
}

}
}
} catch (NullPointerException e) {
description = "null";
}

return new MCPing(
Expand Down Expand Up @@ -210,7 +228,7 @@ public int getMod_count() {
return this.mod_count;
}

public ArrayList<String> getModList() {
public List<String> getModList() {
return this.modList;
}

Expand Down

0 comments on commit d024a8f

Please sign in to comment.