diff --git a/src/main/java/net/minestom/server/ping/ResponseData.java b/src/main/java/net/minestom/server/ping/ResponseData.java index 687506bb0..af8f18b4b 100644 --- a/src/main/java/net/minestom/server/ping/ResponseData.java +++ b/src/main/java/net/minestom/server/ping/ResponseData.java @@ -2,101 +2,150 @@ package net.minestom.server.ping; import com.google.gson.JsonArray; import com.google.gson.JsonObject; - import java.util.ArrayList; import java.util.List; import java.util.UUID; /** * Represents the data sent to the player when refreshing the server list. - *

- * Filled by {@link ResponseDataConsumer} and specified in {@link net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}. + * + *

Filled by {@link ResponseDataConsumer} and specified in {@link + * net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}. */ public class ResponseData { - private final JsonObject jsonObject = new JsonObject(); + private final JsonObject jsonObject; - private final JsonObject versionObject = new JsonObject(); - private final JsonObject playersObject = new JsonObject(); - private final JsonArray sampleArray = new JsonArray(); - private final JsonObject descriptionObject = new JsonObject(); + private final JsonObject versionObject; + private final JsonObject playersObject; + private final JsonArray sampleArray; + private final JsonObject descriptionObject; + private final List pingPlayers; + private String name; + private int protocol; + private int maxPlayer; + private int online; + private String description; + private String favicon; + + /** Constructs a new {@link ResponseData}. */ + public ResponseData() { + this.jsonObject = new JsonObject(); + this.versionObject = new JsonObject(); + this.playersObject = new JsonObject(); + this.sampleArray = new JsonArray(); + this.descriptionObject = new JsonObject(); + this.pingPlayers = new ArrayList<>(); + } + + /** + * Sets the name for the response. + * + * @param name The name for the response data. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Sets the response protocol version. + * + * @param protocol The protocol version for the response data. + */ + public void setProtocol(int protocol) { + this.protocol = protocol; + } + + /** + * Sets the response maximum player count. + * + * @param maxPlayer The maximum player count for the response data. + */ + public void setMaxPlayer(int maxPlayer) { + this.maxPlayer = maxPlayer; + } + + /** + * Sets the response online count. + * + * @param online The online count for the response data. + */ + public void setOnline(int online) { + this.online = online; + } + + /** + * Adds a player to the response. + * + * @param name The name of the player. + * @param uuid The unique identifier of the player. + */ + public void addPlayer(String name, UUID uuid) { + PingPlayer pingPlayer = new PingPlayer(); + pingPlayer.name = name; + pingPlayer.uuid = uuid; + this.pingPlayers.add(pingPlayer); + } + + /** + * Removes all of the ping players from this {@link #pingPlayers}. The {@link #pingPlayers} list + * will be empty this call returns. + */ + public void clearPlayers() { + this.pingPlayers.clear(); + } + + /** + * Sets the response description. + * + * @param description The description for the response data. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Sets the response favicon. + * + * @param favicon The favicon for the response data. + */ + public void setFavicon(String favicon) { + this.favicon = favicon; + } + + /** + * Converts the response data into a {@link JsonObject}. + * + * @return The converted response data as a json tree. + */ + public JsonObject build() { + versionObject.addProperty("name", name); + versionObject.addProperty("protocol", protocol); + + playersObject.addProperty("max", maxPlayer); + playersObject.addProperty("online", online); + + for (PingPlayer pingPlayer : pingPlayers) { + JsonObject pingPlayerObject = new JsonObject(); + pingPlayerObject.addProperty("name", pingPlayer.name); + pingPlayerObject.addProperty("id", pingPlayer.uuid.toString()); + sampleArray.add(pingPlayerObject); + } + playersObject.add("sample", sampleArray); + + descriptionObject.addProperty("text", description); + + jsonObject.add("version", versionObject); + jsonObject.add("players", playersObject); + jsonObject.add("description", descriptionObject); + jsonObject.addProperty("favicon", favicon); + return jsonObject; + } + + /** Represents a player line in the server list hover. */ + private static class PingPlayer { private String name; - private int protocol; - - private int maxPlayer; - private int online; - private final List pingPlayers = new ArrayList<>(); - - private String description; - - private String favicon; - - public void setName(String name) { - this.name = name; - } - - public void setProtocol(int protocol) { - this.protocol = protocol; - } - - public void setMaxPlayer(int maxPlayer) { - this.maxPlayer = maxPlayer; - } - - public void setOnline(int online) { - this.online = online; - } - - public void addPlayer(String name, UUID uuid) { - PingPlayer pingPlayer = new PingPlayer(); - pingPlayer.name = name; - pingPlayer.uuid = uuid; - this.pingPlayers.add(pingPlayer); - } - - public void setDescription(String description) { - this.description = description; - } - - public void setFavicon(String favicon) { - this.favicon = favicon; - } - - /** - * Converts the response data into a {@link JsonObject}. - * - * @return the converted json data - */ - public JsonObject build() { - versionObject.addProperty("name", name); - versionObject.addProperty("protocol", protocol); - - playersObject.addProperty("max", maxPlayer); - playersObject.addProperty("online", online); - - for (PingPlayer pingPlayer : pingPlayers) { - JsonObject pingPlayerObject = new JsonObject(); - pingPlayerObject.addProperty("name", pingPlayer.name); - pingPlayerObject.addProperty("id", pingPlayer.uuid.toString()); - sampleArray.add(pingPlayerObject); - } - playersObject.add("sample", sampleArray); - - descriptionObject.addProperty("text", description); - - jsonObject.add("version", versionObject); - jsonObject.add("players", playersObject); - jsonObject.add("description", descriptionObject); - jsonObject.addProperty("favicon", favicon); - return jsonObject; - } - - /** - * Represents a player line in the server list hover. - */ - private static class PingPlayer { - private String name; - private UUID uuid; - } + private UUID uuid; + } } diff --git a/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java b/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java index b59c02d49..251ef7179 100644 --- a/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java +++ b/src/main/java/net/minestom/server/ping/ResponseDataConsumer.java @@ -4,10 +4,18 @@ import net.minestom.server.network.player.PlayerConnection; /** * Consumer used to fill a {@link ResponseData} object before being sent to a connection. - *

- * Can be specified in {@link net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}. + * + *

Can be specified in {@link net.minestom.server.MinecraftServer#start(String, int, + * ResponseDataConsumer)}. */ @FunctionalInterface public interface ResponseDataConsumer { - void accept(PlayerConnection playerConnection, ResponseData responseData); + + /** + * A method to fill the data of the response. + * + * @param playerConnection The player connection to which the response should be sent. + * @param responseData The data for the response. + */ + void accept(PlayerConnection playerConnection, ResponseData responseData); }