Skip to content

Commit 04513a6

Browse files
authored
Merge pull request #22 from RedCokeDevelopment/dev
Version 0.1.5
2 parents 44eb973 + f25818f commit 04513a6

File tree

5 files changed

+104
-75
lines changed

5 files changed

+104
-75
lines changed

pom.xml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.redcoke</groupId>
88
<artifactId>mcserverping</artifactId>
9-
<version>0.1.4</version>
9+
<version>0.1.5</version>
1010

1111
<name>MCServerPing</name>
1212
<description>A Java API for obtaining info about a Minecraft Server</description>
@@ -61,11 +61,6 @@
6161
<name>jcenter-bintray</name>
6262
<url>https://jcenter.bintray.com</url>
6363
</repository>
64-
<repository>
65-
<id>central</id>
66-
<name>bintray</name>
67-
<url>https://jcenter.bintray.com</url>
68-
</repository>
6964
<repository>
7065
<id>bungeecord-repo</id>
7166
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
@@ -86,20 +81,19 @@
8681
<dependency>
8782
<groupId>org.projectlombok</groupId>
8883
<artifactId>lombok</artifactId>
89-
<version>1.18.24</version>
84+
<version>1.18.32</version>
9085
<scope>provided</scope>
9186
</dependency>
87+
<dependency>
88+
<groupId>org.jetbrains</groupId>
89+
<artifactId>annotations</artifactId>
90+
<version>24.1.0</version>
91+
</dependency>
9292
<dependency>
9393
<groupId>dnsjava</groupId>
9494
<artifactId>dnsjava</artifactId>
9595
<version>3.5.1</version>
9696
</dependency>
97-
<dependency>
98-
<groupId>net.md-5</groupId>
99-
<artifactId>bungeecord-api</artifactId>
100-
<version>1.17-R0.1-SNAPSHOT</version>
101-
<scope>compile</scope>
102-
</dependency>
10397
<dependency>
10498
<groupId>org.junit.jupiter</groupId>
10599
<artifactId>junit-jupiter-engine</artifactId>

src/main/java/dev/redcoke/mcserverping/MCServerPing.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import java.util.concurrent.*;
1313
import java.util.concurrent.atomic.AtomicInteger;
1414

15-
import net.md_5.bungee.api.chat.TextComponent;
16-
import net.md_5.bungee.chat.ComponentSerializer;
1715
import org.xbill.DNS.Lookup;
1816
import org.xbill.DNS.SRVRecord;
1917
import org.xbill.DNS.Type;
@@ -134,14 +132,8 @@ public static MCServerPingResponse getPing(final String address, final int port)
134132
var descriptionJsonObject = descriptionJsonElement.getAsJsonObject();
135133

136134
if (descriptionJsonObject.has("extra")) {
137-
descriptionJsonObject.addProperty("text",
138-
new TextComponent(ComponentSerializer.parse(
139-
descriptionJsonObject
140-
.get("extra")
141-
.getAsJsonArray()
142-
.toString()
143-
)).toLegacyText()
144-
);
135+
descriptionJsonObject.add("raw", descriptionJsonObject.get("extra").getAsJsonArray().getAsJsonArray());
136+
descriptionJsonObject.addProperty("text", TextComponentFormatter.toLegacyText(descriptionJsonObject.get("extra").getAsJsonArray()));
145137
jsonObj.add("description", descriptionJsonObject);
146138
}
147139

src/main/java/dev/redcoke/mcserverping/MCServerPingResponse.java

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,47 @@
44
import com.google.gson.JsonArray;
55
import com.google.gson.JsonObject;
66
import com.google.gson.JsonParser;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Data;
9+
import org.jetbrains.annotations.Nullable;
710

11+
@Data
12+
@AllArgsConstructor
813
public final class MCServerPingResponse {
914
private final int ping;
1015
private final String version;
1116
private final int protocol;
12-
private final int maxPlayers;
13-
private final int onlinePlayers;
17+
@Nullable private final Integer maxPlayers;
18+
@Nullable private final Integer onlinePlayers;
1419
private final String motd;
1520
private final JsonArray descriptionExtras;
1621
private final String serverIcon;
1722

18-
public MCServerPingResponse(int ping, String name, int protocol, Integer playerMax, Integer playerOnline, String motd, JsonArray descriptionExtras, String serverIcon) {
19-
this.ping = ping;
20-
this.version = name;
21-
this.protocol = protocol;
22-
this.maxPlayers = playerMax;
23-
this.onlinePlayers = playerOnline;
24-
this.motd = motd;
25-
this.descriptionExtras = descriptionExtras;
26-
this.serverIcon = serverIcon;
27-
}
28-
2923
public static MCServerPingResponse serverPingFromJsonObj(JsonObject jsonObj) {
30-
int serverPing = jsonObj.get("ping").getAsInt();
31-
String versionName = jsonObj.get("version").getAsString();
32-
int serverProtocol = jsonObj.get("protocol").getAsInt();
24+
var serverPing = jsonObj.get("ping").getAsInt();
25+
String versionName;
26+
int serverProtocol;
27+
if (jsonObj.get("version").getAsJsonObject().has("name")) { // 1.19+ format
28+
versionName = jsonObj.get("version").getAsJsonObject().get("name").getAsString();
29+
serverProtocol = jsonObj.get("version").getAsJsonObject().get("protocol").getAsInt();
30+
} else { // legacy SLP format (pre 1.19.4)
31+
versionName = jsonObj.get("version").getAsString();
32+
serverProtocol = jsonObj.get("protocol").getAsInt();
33+
}
3334
Integer playerMax = null;
3435
Integer playerOnline = null;
3536
if (jsonObj.has("players")) { // Players object is optional somehow
3637
playerMax = jsonObj.get("players").getAsJsonObject().get("max").getAsInt();
3738
playerOnline = jsonObj.get("players").getAsJsonObject().get("online").getAsInt();
3839
}
39-
String serverMOTD = jsonObj.get("description").getAsJsonObject().get("text").getAsString();
40-
JsonArray serverDescriptionExtra = (jsonObj.get("description").getAsJsonObject().get("extra") == null) ? null : jsonObj.get("description").getAsJsonObject().get("extra").getAsJsonArray();
41-
String favIcon = jsonObj.get("favicon").getAsString();
40+
var serverMOTD = jsonObj.get("description").getAsJsonObject().get("text").getAsString();
41+
var serverDescriptionExtra = (jsonObj.get("description").getAsJsonObject().get("extra") == null) ? null : jsonObj.get("description").getAsJsonObject().get("extra").getAsJsonArray();
42+
var favIcon = jsonObj.get("favicon").getAsString();
4243
return new MCServerPingResponse(
4344
serverPing, versionName, serverProtocol, playerMax, playerOnline, serverMOTD, serverDescriptionExtra, favIcon
4445
);
4546
}
4647

47-
public int getPing() {
48-
return ping;
49-
}
50-
51-
public String getName() {
52-
return version;
53-
}
54-
55-
public int getProtocol() {
56-
return protocol;
57-
}
58-
59-
public Integer getPlayerMax() {
60-
return maxPlayers;
61-
}
62-
63-
public Integer getPlayerOnline() {
64-
return onlinePlayers;
65-
}
66-
67-
public String getMotd() {
68-
return motd;
69-
}
70-
71-
public JsonArray getDescriptionExtras() {
72-
return descriptionExtras;
73-
}
74-
75-
public String getServerIcon() {
76-
return serverIcon;
77-
}
78-
7948
public String getAsJsonString() {
8049
return new Gson().newBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(this);
8150
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package dev.redcoke.mcserverping;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import lombok.AccessLevel;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* Utility class for formatting Minecraft text components.
13+
*/
14+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
15+
public class TextComponentFormatter {
16+
public static final String FORMATTING_CHAR = "§"; // U+00A7 for Minecraft, "&" for Classic
17+
public static final String RESET = FORMATTING_CHAR + "r";
18+
public static final String BOLD = FORMATTING_CHAR + "l";
19+
public static final String OBFUSCATED = FORMATTING_CHAR + "k";
20+
public static final String STRIKETHROUGH = FORMATTING_CHAR + "m";
21+
public static final String UNDERLINE = FORMATTING_CHAR + "n";
22+
public static final String ITALIC = FORMATTING_CHAR + "o";
23+
protected static final Map<String, String> COLOR_CODES;
24+
25+
static {
26+
Map<String, String> colorCodes = new HashMap<>();
27+
colorCodes.put("black", "0");
28+
colorCodes.put("dark_blue", "1");
29+
colorCodes.put("dark_green", "2");
30+
colorCodes.put("dark_aqua", "3");
31+
colorCodes.put("dark_red", "4");
32+
colorCodes.put("dark_purple", "5");
33+
colorCodes.put("gold", "6");
34+
colorCodes.put("gray", "7");
35+
colorCodes.put("dark_gray", "8");
36+
colorCodes.put("blue", "9");
37+
colorCodes.put("green", "a");
38+
colorCodes.put("aqua", "b");
39+
colorCodes.put("red", "c");
40+
colorCodes.put("light_purple", "d");
41+
colorCodes.put("yellow", "e");
42+
colorCodes.put("white", "f");
43+
COLOR_CODES = colorCodes;
44+
}
45+
46+
public static String toLegacyText(JsonArray text) {
47+
StringBuilder legacyText = new StringBuilder();
48+
for (var component : text) {
49+
JsonObject componentObj = component.getAsJsonObject();
50+
if (componentObj.has("bold") && componentObj.get("bold").getAsBoolean()) {
51+
legacyText.append(BOLD);
52+
}
53+
if (componentObj.has("obfuscated") && componentObj.get("obfuscated").getAsBoolean()) {
54+
legacyText.append(OBFUSCATED);
55+
}
56+
if (componentObj.has("strikethrough") && componentObj.get("strikethrough").getAsBoolean()) {
57+
legacyText.append(STRIKETHROUGH);
58+
}
59+
if (componentObj.has("underline") && componentObj.get("underline").getAsBoolean()) {
60+
legacyText.append(UNDERLINE);
61+
}
62+
if (componentObj.has("italic") && componentObj.get("italic").getAsBoolean()) {
63+
legacyText.append(ITALIC);
64+
}
65+
if (componentObj.has("color")) {
66+
String color = componentObj.get("color").getAsString();
67+
legacyText.append(FORMATTING_CHAR).append(COLOR_CODES.get(color));
68+
}
69+
legacyText.append(componentObj.get("text").getAsString());
70+
}
71+
return legacyText.toString();
72+
}
73+
74+
}

src/test/java/dev/redcoke/mcserverping/MCServerPingDemoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class MCServerPingDemoTest {
1111
@Test
1212
void test() {
13-
List<String> servers = List.of("hypixel.net", "2b2t.org", "play.cubecraft.net", "play.potwmc.com");
13+
List<String> servers = List.of("hypixel.net", "2b2t.org", "play.cubecraft.net");
1414
for (var server : servers) {
1515
try {
1616
MCServerPing.getPing(server, 25565).getAsJsonString();

0 commit comments

Comments
 (0)