Search in sources :

Example 1 with RawAction

use of org.lanternpowered.server.text.LanternTextHelper.RawAction in project LanternServer by LanternPowered.

the class JsonTextBaseSerializer method serialize.

public static void serialize(JsonObject json, Text text, JsonSerializationContext context, List<Text> children) {
    final TextColor color = text.getColor();
    if (color != TextColors.NONE) {
        json.addProperty(COLOR, color.getId());
    }
    final TextStyle style = text.getStyle();
    style.isBold().ifPresent(v -> json.addProperty(BOLD, v));
    style.isItalic().ifPresent(v -> json.addProperty(ITALIC, v));
    style.hasUnderline().ifPresent(v -> json.addProperty(UNDERLINE, v));
    style.hasStrikethrough().ifPresent(v -> json.addProperty(STRIKETHROUGH, v));
    style.isObfuscated().ifPresent(v -> json.addProperty(OBFUSCATED, v));
    if (!children.isEmpty()) {
        json.add(CHILDREN, context.serialize(children.toArray(new Text[children.size()]), Text[].class));
    }
    text.getClickAction().ifPresent(clickAction -> {
        final RawAction raw = LanternTextHelper.raw(clickAction);
        final JsonObject jsonEvent = new JsonObject();
        jsonEvent.addProperty(EVENT_ACTION, raw.getAction());
        jsonEvent.addProperty(EVENT_VALUE, raw.getValueAsString());
        json.add(CLICK_EVENT, jsonEvent);
    });
    text.getHoverAction().ifPresent(clickAction -> {
        final RawAction raw = LanternTextHelper.raw(clickAction);
        final JsonObject jsonEvent = new JsonObject();
        jsonEvent.addProperty(EVENT_ACTION, raw.getAction());
        jsonEvent.addProperty(EVENT_VALUE, raw.getValueAsString());
        json.add(HOVER_EVENT, jsonEvent);
    });
    text.getShiftClickAction().ifPresent(shiftClickAction -> {
        if (shiftClickAction instanceof ShiftClickAction.InsertText) {
            json.addProperty(INSERTION, ((ShiftClickAction.InsertText) shiftClickAction).getResult());
        }
    });
}
Also used : TextStyle(org.spongepowered.api.text.format.TextStyle) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) RawAction(org.lanternpowered.server.text.LanternTextHelper.RawAction) JsonObject(com.google.gson.JsonObject) TextColor(org.spongepowered.api.text.format.TextColor)

Aggregations

JsonObject (com.google.gson.JsonObject)1 RawAction (org.lanternpowered.server.text.LanternTextHelper.RawAction)1 ShiftClickAction (org.spongepowered.api.text.action.ShiftClickAction)1 TextColor (org.spongepowered.api.text.format.TextColor)1 TextStyle (org.spongepowered.api.text.format.TextStyle)1