0

Merge branch 'feature/text-component' into development

This commit is contained in:
2020-05-13 17:06:18 +03:00
2 changed files with 67 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import mc.protocol.text.Text; import mc.protocol.text.Text;
import mc.protocol.utils.json.serializer.TextDeserializer; import mc.protocol.utils.json.serializer.TextDeserializer;
import mc.protocol.utils.json.serializer.TextSerializer;
import java.util.Optional; import java.util.Optional;
@@ -45,6 +46,7 @@ public class JsonUtils {
if (objectMapper == null) { if (objectMapper == null) {
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
module.addDeserializer(Text.class, new TextDeserializer()); module.addDeserializer(Text.class, new TextDeserializer());
module.addSerializer(Text.class, new TextSerializer());
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
objectMapper.registerModule(module); objectMapper.registerModule(module);

View File

@@ -0,0 +1,65 @@
package mc.protocol.utils.json.serializer;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import mc.protocol.text.Text;
import mc.protocol.text.TextStyle;
import java.io.IOException;
public class TextSerializer extends StdSerializer<Text> {
public TextSerializer() {
this(null);
}
public TextSerializer(Class<Text> t) {
super(t);
}
@Override
public void serialize(Text text, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeStringField("text", text.getContent());
if (text.getColor() != null) {
gen.writeStringField("colot", text.getColor().getName());
}
if (text.getStyle() != null) {
final TextStyle style = text.getStyle();
if (style.getBold().isPresent()) {
gen.writeBooleanField("bold", style.getBold().get());
}
if (style.getItalic().isPresent()) {
gen.writeBooleanField("italic", style.getItalic().get());
}
if (style.getObfuscated().isPresent()) {
gen.writeBooleanField("obfuscated", style.getObfuscated().get());
}
if (style.getStrikethrough().isPresent()) {
gen.writeBooleanField("strikethrough", style.getStrikethrough().get());
}
if (style.getUnderline().isPresent()) {
gen.writeBooleanField("underlined", style.getUnderline().get());
}
}
if (text.getChildren() != null) {
gen.writeArrayFieldStart("extra");
for (Text textChild : text.getChildren()) {
serialize(textChild, gen, provider);
}
gen.writeEndArray();
}
gen.writeEndObject();
}
}