0

add ServerInfo DTO

This commit is contained in:
2020-05-10 19:53:13 +03:00
parent d69e1f8b06
commit 49a932baea
3 changed files with 82 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ dependencies {
/* COMPONENTS */
compile (group: 'com.google.guava', name: 'guava', version: '28.0-jre')
compile (group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.0')
/* LOMBOK */
annotationProcessor (group: 'org.projectlombok', name: 'lombok', version: lombok_version)

View File

@@ -0,0 +1,49 @@
package mc.protocol.dto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
import java.util.List;
import java.util.UUID;
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class ServerInfo {
private Version version;
@JsonProperty("players")
private PlayersInfo playersInfo;
//TODO необходимо реализовать объект типа Chat (см. https://wiki.vg/index.php?title=Chat&oldid=8329)
private JsonNode description;
@JsonProperty("favicon")
private String faviconBase64;
@Data
public static class Version {
private String name;
private int protocol;
}
@Data
public static class PlayersInfo {
private int max;
private int online;
@JsonProperty("sample")
private List<SamplePlayer> samplePlayers;
}
@Data
public static class SamplePlayer {
private UUID id;
private String name;
}
}

View File

@@ -1,7 +1,10 @@
package mc.protocol.status.server;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import mc.protocol.Packet;
import mc.protocol.dto.ServerInfo;
import mc.protocol.io.NetInputStream;
import mc.protocol.io.NetOutputStream;
@@ -49,15 +52,41 @@ public class StatusServerResponse implements Packet {
* }
* </pre>
*/
private String info;
private String serverInfo;
private ServerInfo serverInfoDto;
public ServerInfo getServerInfoDto() {
if (serverInfoDto == null) {
try {
ObjectMapper mapper = new ObjectMapper();
serverInfoDto = mapper.readValue(serverInfo, ServerInfo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
return new ServerInfo();
}
}
return serverInfoDto;
}
@Override
public void readSelf(NetInputStream netInputStream) {
info = netInputStream.readString();
serverInfo = netInputStream.readString();
}
@Override
public void writeSelf(NetOutputStream netOutputStream) {
netOutputStream.writeString(info);
if (serverInfo == null) {
try {
ObjectMapper mapper = new ObjectMapper();
serverInfo = mapper.writeValueAsString(serverInfoDto);
} catch (JsonProcessingException e) {
e.printStackTrace();
serverInfo = "{}";
}
}
netOutputStream.writeString(serverInfo);
}
}