Skip to content

Commit

Permalink
Merge pull request #22 from RedCokeDevelopment/dev
Browse files Browse the repository at this point in the history
Version 0.1.5
  • Loading branch information
RedTeaDev authored Jun 3, 2024
2 parents 44eb973 + f25818f commit 04513a6
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 75 deletions.
20 changes: 7 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.redcoke</groupId>
<artifactId>mcserverping</artifactId>
<version>0.1.4</version>
<version>0.1.5</version>

<name>MCServerPing</name>
<description>A Java API for obtaining info about a Minecraft Server</description>
Expand Down Expand Up @@ -61,11 +61,6 @@
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
<repository>
<id>central</id>
<name>bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
<repository>
<id>bungeecord-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
Expand All @@ -86,20 +81,19 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.1.0</version>
</dependency>
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
<version>1.17-R0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/dev/redcoke/mcserverping/MCServerPing.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

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

if (descriptionJsonObject.has("extra")) {
descriptionJsonObject.addProperty("text",
new TextComponent(ComponentSerializer.parse(
descriptionJsonObject
.get("extra")
.getAsJsonArray()
.toString()
)).toLegacyText()
);
descriptionJsonObject.add("raw", descriptionJsonObject.get("extra").getAsJsonArray().getAsJsonArray());
descriptionJsonObject.addProperty("text", TextComponentFormatter.toLegacyText(descriptionJsonObject.get("extra").getAsJsonArray()));
jsonObj.add("description", descriptionJsonObject);
}

Expand Down
71 changes: 20 additions & 51 deletions src/main/java/dev/redcoke/mcserverping/MCServerPingResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,47 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.jetbrains.annotations.Nullable;

@Data
@AllArgsConstructor
public final class MCServerPingResponse {
private final int ping;
private final String version;
private final int protocol;
private final int maxPlayers;
private final int onlinePlayers;
@Nullable private final Integer maxPlayers;
@Nullable private final Integer onlinePlayers;
private final String motd;
private final JsonArray descriptionExtras;
private final String serverIcon;

public MCServerPingResponse(int ping, String name, int protocol, Integer playerMax, Integer playerOnline, String motd, JsonArray descriptionExtras, String serverIcon) {
this.ping = ping;
this.version = name;
this.protocol = protocol;
this.maxPlayers = playerMax;
this.onlinePlayers = playerOnline;
this.motd = motd;
this.descriptionExtras = descriptionExtras;
this.serverIcon = serverIcon;
}

public static MCServerPingResponse serverPingFromJsonObj(JsonObject jsonObj) {
int serverPing = jsonObj.get("ping").getAsInt();
String versionName = jsonObj.get("version").getAsString();
int serverProtocol = jsonObj.get("protocol").getAsInt();
var serverPing = jsonObj.get("ping").getAsInt();
String versionName;
int serverProtocol;
if (jsonObj.get("version").getAsJsonObject().has("name")) { // 1.19+ format
versionName = jsonObj.get("version").getAsJsonObject().get("name").getAsString();
serverProtocol = jsonObj.get("version").getAsJsonObject().get("protocol").getAsInt();
} else { // legacy SLP format (pre 1.19.4)
versionName = jsonObj.get("version").getAsString();
serverProtocol = jsonObj.get("protocol").getAsInt();
}
Integer playerMax = null;
Integer playerOnline = null;
if (jsonObj.has("players")) { // Players object is optional somehow
playerMax = jsonObj.get("players").getAsJsonObject().get("max").getAsInt();
playerOnline = jsonObj.get("players").getAsJsonObject().get("online").getAsInt();
}
String serverMOTD = jsonObj.get("description").getAsJsonObject().get("text").getAsString();
JsonArray serverDescriptionExtra = (jsonObj.get("description").getAsJsonObject().get("extra") == null) ? null : jsonObj.get("description").getAsJsonObject().get("extra").getAsJsonArray();
String favIcon = jsonObj.get("favicon").getAsString();
var serverMOTD = jsonObj.get("description").getAsJsonObject().get("text").getAsString();
var serverDescriptionExtra = (jsonObj.get("description").getAsJsonObject().get("extra") == null) ? null : jsonObj.get("description").getAsJsonObject().get("extra").getAsJsonArray();
var favIcon = jsonObj.get("favicon").getAsString();
return new MCServerPingResponse(
serverPing, versionName, serverProtocol, playerMax, playerOnline, serverMOTD, serverDescriptionExtra, favIcon
);
}

public int getPing() {
return ping;
}

public String getName() {
return version;
}

public int getProtocol() {
return protocol;
}

public Integer getPlayerMax() {
return maxPlayers;
}

public Integer getPlayerOnline() {
return onlinePlayers;
}

public String getMotd() {
return motd;
}

public JsonArray getDescriptionExtras() {
return descriptionExtras;
}

public String getServerIcon() {
return serverIcon;
}

public String getAsJsonString() {
return new Gson().newBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(this);
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/dev/redcoke/mcserverping/TextComponentFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dev.redcoke.mcserverping;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
* Utility class for formatting Minecraft text components.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TextComponentFormatter {
public static final String FORMATTING_CHAR = "§"; // U+00A7 for Minecraft, "&" for Classic
public static final String RESET = FORMATTING_CHAR + "r";
public static final String BOLD = FORMATTING_CHAR + "l";
public static final String OBFUSCATED = FORMATTING_CHAR + "k";
public static final String STRIKETHROUGH = FORMATTING_CHAR + "m";
public static final String UNDERLINE = FORMATTING_CHAR + "n";
public static final String ITALIC = FORMATTING_CHAR + "o";
protected static final Map<String, String> COLOR_CODES;

static {
Map<String, String> colorCodes = new HashMap<>();
colorCodes.put("black", "0");
colorCodes.put("dark_blue", "1");
colorCodes.put("dark_green", "2");
colorCodes.put("dark_aqua", "3");
colorCodes.put("dark_red", "4");
colorCodes.put("dark_purple", "5");
colorCodes.put("gold", "6");
colorCodes.put("gray", "7");
colorCodes.put("dark_gray", "8");
colorCodes.put("blue", "9");
colorCodes.put("green", "a");
colorCodes.put("aqua", "b");
colorCodes.put("red", "c");
colorCodes.put("light_purple", "d");
colorCodes.put("yellow", "e");
colorCodes.put("white", "f");
COLOR_CODES = colorCodes;
}

public static String toLegacyText(JsonArray text) {
StringBuilder legacyText = new StringBuilder();
for (var component : text) {
JsonObject componentObj = component.getAsJsonObject();
if (componentObj.has("bold") && componentObj.get("bold").getAsBoolean()) {
legacyText.append(BOLD);
}
if (componentObj.has("obfuscated") && componentObj.get("obfuscated").getAsBoolean()) {
legacyText.append(OBFUSCATED);
}
if (componentObj.has("strikethrough") && componentObj.get("strikethrough").getAsBoolean()) {
legacyText.append(STRIKETHROUGH);
}
if (componentObj.has("underline") && componentObj.get("underline").getAsBoolean()) {
legacyText.append(UNDERLINE);
}
if (componentObj.has("italic") && componentObj.get("italic").getAsBoolean()) {
legacyText.append(ITALIC);
}
if (componentObj.has("color")) {
String color = componentObj.get("color").getAsString();
legacyText.append(FORMATTING_CHAR).append(COLOR_CODES.get(color));
}
legacyText.append(componentObj.get("text").getAsString());
}
return legacyText.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class MCServerPingDemoTest {
@Test
void test() {
List<String> servers = List.of("hypixel.net", "2b2t.org", "play.cubecraft.net", "play.potwmc.com");
List<String> servers = List.of("hypixel.net", "2b2t.org", "play.cubecraft.net");
for (var server : servers) {
try {
MCServerPing.getPing(server, 25565).getAsJsonString();
Expand Down

0 comments on commit 04513a6

Please sign in to comment.