Text deserializer
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
package mc.protocol.converter;
|
|
||||||
|
|
||||||
public interface Converter<S, T> {
|
|
||||||
|
|
||||||
T convert(S source);
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package mc.protocol.converter;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
||||||
import lombok.AccessLevel;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import mc.protocol.text.Text;
|
|
||||||
import mc.protocol.text.TextStyle;
|
|
||||||
import mc.protocol.utils.JsonUtils;
|
|
||||||
|
|
||||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
|
||||||
@Getter
|
|
||||||
public class TextToJsonConverter implements Converter<Text, String> {
|
|
||||||
|
|
||||||
private static final TextToJsonConverter instance = new TextToJsonConverter();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String convert(Text text) {
|
|
||||||
return JsonUtils.objectToJson(createObjectNode(text));
|
|
||||||
}
|
|
||||||
|
|
||||||
private ObjectNode createObjectNode(Text text) {
|
|
||||||
final ObjectNode objectNode = JsonUtils.getObjectMapper().createObjectNode();
|
|
||||||
objectNode.put("text", text.getContent());
|
|
||||||
|
|
||||||
if (text.getColor() != null) {
|
|
||||||
objectNode.put("color", text.getColor().getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.getStyle() != null) {
|
|
||||||
final TextStyle style = text.getStyle();
|
|
||||||
style.getBold().ifPresent(value -> objectNode.put("bold", value));
|
|
||||||
style.getItalic().ifPresent(value -> objectNode.put("italic", value));
|
|
||||||
style.getObfuscated().ifPresent(value -> objectNode.put("obfuscated", value));
|
|
||||||
style.getStrikethrough().ifPresent(value -> objectNode.put("strikethrough", value));
|
|
||||||
style.getUnderline().ifPresent(value -> objectNode.put("underlined", value));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.getChildren() != null) {
|
|
||||||
final ArrayNode arrayNode = JsonUtils.getObjectMapper().createArrayNode();
|
|
||||||
text.getChildren().forEach(childText -> arrayNode.add(createObjectNode(childText)));
|
|
||||||
objectNode.set("extra", arrayNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
return objectNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,6 +3,7 @@ package mc.protocol.dto;
|
|||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.ToString;
|
||||||
import mc.protocol.text.Text;
|
import mc.protocol.text.Text;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -10,6 +11,7 @@ import java.util.UUID;
|
|||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
@Data
|
@Data
|
||||||
|
@ToString(exclude = "faviconBase64")
|
||||||
public class ServerInfo {
|
public class ServerInfo {
|
||||||
|
|
||||||
private Version version;
|
private Version version;
|
||||||
@@ -17,7 +19,6 @@ public class ServerInfo {
|
|||||||
@JsonProperty("players")
|
@JsonProperty("players")
|
||||||
private PlayersInfo playersInfo;
|
private PlayersInfo playersInfo;
|
||||||
|
|
||||||
//FIXME так не получится. Нужен кастомный Jackson сериализатор
|
|
||||||
private Text description;
|
private Text description;
|
||||||
|
|
||||||
@JsonProperty("favicon")
|
@JsonProperty("favicon")
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import mc.protocol.Packet;
|
|||||||
import mc.protocol.dto.ServerInfo;
|
import mc.protocol.dto.ServerInfo;
|
||||||
import mc.protocol.io.NetInputStream;
|
import mc.protocol.io.NetInputStream;
|
||||||
import mc.protocol.io.NetOutputStream;
|
import mc.protocol.io.NetOutputStream;
|
||||||
import mc.protocol.utils.JsonUtils;
|
import mc.protocol.utils.json.JsonUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Status server packet, response.
|
* Status server packet, response.
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ package mc.protocol.text;
|
|||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@ToString
|
||||||
public class Text {
|
public class Text {
|
||||||
private static final Text EMPTY = new Text();
|
private static final Text EMPTY = new Text();
|
||||||
private static final Text NEW_LINE = new Text("\n", null, null, null);
|
private static final Text NEW_LINE = new Text("\n", null, null, null);
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package mc.protocol.text;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
public enum TextColor {
|
public enum TextColor {
|
||||||
@@ -23,6 +25,12 @@ public enum TextColor {
|
|||||||
YELLOW ("yellow", 'e'),
|
YELLOW ("yellow", 'e'),
|
||||||
WHITE ("white", 'f');
|
WHITE ("white", 'f');
|
||||||
|
|
||||||
|
public static TextColor valueOfColorName(String name) {
|
||||||
|
return Stream.of(TextColor.values())
|
||||||
|
.filter(textColor -> textColor.getName().equals(name))
|
||||||
|
.findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final char code;
|
private final char code;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package mc.protocol.utils;
|
package mc.protocol.utils.json;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import mc.protocol.text.Text;
|
||||||
|
import mc.protocol.utils.json.serializer.TextDeserializer;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -40,7 +43,11 @@ public class JsonUtils {
|
|||||||
|
|
||||||
public ObjectMapper getObjectMapper() {
|
public ObjectMapper getObjectMapper() {
|
||||||
if (objectMapper == null) {
|
if (objectMapper == null) {
|
||||||
|
SimpleModule module = new SimpleModule();
|
||||||
|
module.addDeserializer(Text.class, new TextDeserializer());
|
||||||
|
|
||||||
objectMapper = new ObjectMapper();
|
objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.registerModule(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
return objectMapper;
|
return objectMapper;
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package mc.protocol.utils.json.serializer;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
|
import mc.protocol.text.Text;
|
||||||
|
import mc.protocol.text.TextColor;
|
||||||
|
import mc.protocol.text.TextStyle;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class TextDeserializer extends StdDeserializer<Text> {
|
||||||
|
|
||||||
|
public TextDeserializer() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TextDeserializer(Class<Text> t) {
|
||||||
|
super(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Text deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
|
||||||
|
final Text.Builder builder = Text.builder();
|
||||||
|
|
||||||
|
final JsonNode jsonNode = parser.getCodec().readTree(parser);
|
||||||
|
|
||||||
|
Optional.ofNullable(jsonNode.get("text"))
|
||||||
|
.ifPresent(node -> builder.append(node.asText()));
|
||||||
|
Optional.ofNullable(jsonNode.get("color"))
|
||||||
|
.ifPresent(node -> builder.color(TextColor.valueOfColorName(node.asText())));
|
||||||
|
|
||||||
|
if (jsonNode.get("bold") != null && jsonNode.get("bold").isBoolean()
|
||||||
|
&& jsonNode.get("bold").asBoolean()) {
|
||||||
|
builder.style(TextStyle.BOLD);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonNode.get("italic") != null && jsonNode.get("italic").isBoolean()
|
||||||
|
&& jsonNode.get("italic").asBoolean()) {
|
||||||
|
builder.style(TextStyle.ITALIC);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonNode.get("obfuscated") != null && jsonNode.get("obfuscated").isBoolean()
|
||||||
|
&& jsonNode.get("obfuscated").asBoolean()) {
|
||||||
|
builder.style(TextStyle.OBFUSCATED);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonNode.get("strikethrough") != null && jsonNode.get("strikethrough").isBoolean()
|
||||||
|
&& jsonNode.get("strikethrough").asBoolean()) {
|
||||||
|
builder.style(TextStyle.STRIKETHOUGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonNode.get("underlined") != null && jsonNode.get("underlined").isBoolean()
|
||||||
|
&& jsonNode.get("underlined").asBoolean()) {
|
||||||
|
builder.style(TextStyle.UNDERLINE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonNode.get("extra") != null && jsonNode.get("extra").isArray()) {
|
||||||
|
final JsonNode nodeExtra = jsonNode.get("extra");
|
||||||
|
for (JsonNode node : nodeExtra) {
|
||||||
|
builder.append(parser.getCodec().treeToValue(node, Text.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user