Merge pull request #207 from kezz/fix-server-ping

Adventureise ResponseData and cleanup code
This commit is contained in:
TheMode 2021-03-31 22:27:37 +02:00 committed by GitHub
commit 9a8b6e2a11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.network.packet.client.status; package net.minestom.server.network.packet.client.status;
import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.packet.client.ClientPreplayPacket;
import net.minestom.server.network.packet.server.handshake.ResponsePacket; import net.minestom.server.network.packet.server.handshake.ResponsePacket;
@ -18,11 +19,11 @@ public class StatusRequestPacket implements ClientPreplayPacket {
ResponseData responseData = new ResponseData(); ResponseData responseData = new ResponseData();
// Fill default params // Fill default params
responseData.setName(MinecraftServer.VERSION_NAME); responseData.setVersion(MinecraftServer.VERSION_NAME);
responseData.setProtocol(MinecraftServer.PROTOCOL_VERSION); responseData.setProtocol(MinecraftServer.PROTOCOL_VERSION);
responseData.setMaxPlayer(0); responseData.setMaxPlayer(0);
responseData.setOnline(0); responseData.setOnline(0);
responseData.setDescription("Minestom Server"); responseData.setDescription(Component.text("Minestom Server"));
responseData.setFavicon(""); responseData.setFavicon("");
if (consumer != null) if (consumer != null)

View File

@ -2,6 +2,10 @@ package net.minestom.server.ping;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
@ -15,19 +19,12 @@ import java.util.UUID;
* net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}. * net.minestom.server.MinecraftServer#start(String, int, ResponseDataConsumer)}.
*/ */
public class ResponseData { public class ResponseData {
private final JsonObject jsonObject;
private final JsonObject versionObject;
private final JsonObject playersObject;
private final JsonArray sampleArray;
private final JsonObject descriptionObject;
private final List<PingPlayer> pingPlayers; private final List<PingPlayer> pingPlayers;
private String name; private String version;
private int protocol; private int protocol;
private int maxPlayer; private int maxPlayer;
private int online; private int online;
private String description; private Component description;
private String favicon; private String favicon;
@ -35,11 +32,6 @@ public class ResponseData {
* Constructs a new {@link ResponseData}. * Constructs a new {@link ResponseData}.
*/ */
public 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<>(); this.pingPlayers = new ArrayList<>();
} }
@ -47,9 +39,20 @@ public class ResponseData {
* Sets the name for the response. * Sets the name for the response.
* *
* @param name The name for the response data. * @param name The name for the response data.
* @deprecated Use {@link #setVersion(String)}
*/ */
@Deprecated
public void setName(String name) { public void setName(String name) {
this.name = name; this.setVersion(name);
}
/**
* Sets the version name for the response.
*
* @param version The version name for the response data.
*/
public void setVersion(String version) {
this.version = version;
} }
/** /**
@ -79,6 +82,26 @@ public class ResponseData {
this.online = online; this.online = online;
} }
/**
* Adds some players to the response.
*
* @param players the players
*/
public void addPlayer(Iterable<Player> players) {
for (Player player : players) {
this.addPlayer(player);
}
}
/**
* Adds a player to the response.
*
* @param player the player
*/
public void addPlayer(Player player) {
this.addPlayer(player.getUsername(), player.getUuid());
}
/** /**
* Adds a player to the response. * Adds a player to the response.
* *
@ -104,8 +127,19 @@ public class ResponseData {
* Sets the response description. * Sets the response description.
* *
* @param description The description for the response data. * @param description The description for the response data.
* @deprecated Use {@link #setDescription(Component)}
*/ */
@Deprecated
public void setDescription(String description) { public void setDescription(String description) {
this.description = LegacyComponentSerializer.legacySection().deserialize(description);
}
/**
* Sets the response description.
*
* @param description The description for the response data.
*/
public void setDescription(Component description) {
this.description = description; this.description = description;
} }
@ -125,13 +159,19 @@ public class ResponseData {
*/ */
@NotNull @NotNull
public JsonObject build() { public JsonObject build() {
versionObject.addProperty("name", name); // version
versionObject.addProperty("protocol", protocol); final JsonObject versionObject = new JsonObject();
versionObject.addProperty("name", this.version);
versionObject.addProperty("protocol", this.protocol);
playersObject.addProperty("max", maxPlayer); // players info
playersObject.addProperty("online", online); final JsonObject playersObject = new JsonObject();
playersObject.addProperty("max", this.maxPlayer);
playersObject.addProperty("online", this.online);
for (PingPlayer pingPlayer : pingPlayers) { // individual players
final JsonArray sampleArray = new JsonArray();
for (PingPlayer pingPlayer : this.pingPlayers) {
JsonObject pingPlayerObject = new JsonObject(); JsonObject pingPlayerObject = new JsonObject();
pingPlayerObject.addProperty("name", pingPlayer.name); pingPlayerObject.addProperty("name", pingPlayer.name);
pingPlayerObject.addProperty("id", pingPlayer.uuid.toString()); pingPlayerObject.addProperty("id", pingPlayer.uuid.toString());
@ -139,12 +179,14 @@ public class ResponseData {
} }
playersObject.add("sample", sampleArray); playersObject.add("sample", sampleArray);
descriptionObject.addProperty("text", description); final JsonObject descriptionObject = GsonComponentSerializer.gson().serializer()
.toJsonTree(this.description).getAsJsonObject();
final JsonObject jsonObject = new JsonObject();
jsonObject.add("version", versionObject); jsonObject.add("version", versionObject);
jsonObject.add("players", playersObject); jsonObject.add("players", playersObject);
jsonObject.add("description", descriptionObject); jsonObject.add("description", descriptionObject);
jsonObject.addProperty("favicon", favicon); jsonObject.addProperty("favicon", this.favicon);
return jsonObject; return jsonObject;
} }