Search in sources :

Example 1 with TextStyle

use of org.spongepowered.api.text.format.TextStyle 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)

Example 2 with TextStyle

use of org.spongepowered.api.text.format.TextStyle in project LanternServer by LanternPowered.

the class JsonTextBaseSerializer method deserialize.

public static void deserialize(JsonObject json, Text.Builder builder, JsonDeserializationContext context, @Nullable JsonArray children) throws JsonParseException {
    JsonElement element;
    if ((element = json.get(COLOR)) != null) {
        Sponge.getRegistry().getType(TextColor.class, element.getAsString()).ifPresent(builder::color);
    }
    TextStyle style = builder.getStyle();
    if ((element = json.get(BOLD)) != null) {
        style = style.bold(element.getAsBoolean());
    }
    if ((element = json.get(ITALIC)) != null) {
        style = style.italic(element.getAsBoolean());
    }
    if ((element = json.get(UNDERLINE)) != null) {
        style = style.underline(element.getAsBoolean());
    }
    if ((element = json.get(STRIKETHROUGH)) != null) {
        style = style.strikethrough(element.getAsBoolean());
    }
    if ((element = json.get(OBFUSCATED)) != null) {
        style = style.obfuscated(element.getAsBoolean());
    }
    builder.style(style);
    if (children != null) {
        builder.append((Text[]) context.deserialize(children, Text[].class));
    }
    if ((element = json.get(CLICK_EVENT)) != null) {
        final JsonObject jsonClickEvent = element.getAsJsonObject();
        if (jsonClickEvent != null) {
            final JsonPrimitive jsonEventAction = jsonClickEvent.getAsJsonPrimitive(EVENT_ACTION);
            final JsonPrimitive jsonEventValue = jsonClickEvent.getAsJsonPrimitive(EVENT_VALUE);
            if (jsonEventAction != null && jsonEventValue != null) {
                final String action = jsonEventAction.getAsString();
                final String value = jsonEventValue.getAsString();
                final ClickAction<?> clickAction = LanternTextHelper.parseClickAction(action, value);
                if (clickAction != null) {
                    builder.onClick(clickAction);
                }
            }
        }
    }
    if ((element = json.get(HOVER_EVENT)) != null) {
        final JsonObject jsonHoverEvent = element.getAsJsonObject();
        if (jsonHoverEvent != null) {
            final JsonPrimitive jsonEventAction = jsonHoverEvent.getAsJsonPrimitive(EVENT_ACTION);
            final JsonPrimitive jsonEventValue = jsonHoverEvent.getAsJsonPrimitive(EVENT_VALUE);
            if (jsonEventAction != null && jsonEventValue != null) {
                final String action = jsonEventAction.getAsString();
                final String value = jsonEventValue.getAsString();
                final HoverAction<?> hoverAction = LanternTextHelper.parseHoverAction(action, value);
                if (hoverAction != null) {
                    builder.onHover(hoverAction);
                }
            }
        }
    }
    if ((element = json.get(INSERTION)) != null) {
        builder.onShiftClick(TextActions.insertText(element.getAsString()));
    }
}
Also used : TextStyle(org.spongepowered.api.text.format.TextStyle) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor)

Example 3 with TextStyle

use of org.spongepowered.api.text.format.TextStyle in project SpongeAPI by SpongePowered.

the class TextFormatConfigSerializer method deserialize.

@Override
public TextFormat deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
    TextColor color = TextColors.NONE;
    GameRegistry registry = Sponge.getRegistry();
    String colorId = value.getNode(NODE_COLOR).getString();
    if (colorId != null) {
        color = registry.getType(TextColor.class, colorId).orElseThrow(() -> new ObjectMappingException("Color not found: " + colorId));
    }
    TextStyle style = TextStyles.NONE;
    ConfigurationNode styleNode = value.getNode(NODE_STYLE);
    for (TextStyle.Base component : registry.getAllOf(TextStyle.Base.class)) {
        if (styleNode.getNode(component.getId().toLowerCase()).getBoolean()) {
            style = style.and(component);
        }
    }
    return TextFormat.NONE.color(color).style(style);
}
Also used : TextStyle(org.spongepowered.api.text.format.TextStyle) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) GameRegistry(org.spongepowered.api.GameRegistry) TextColor(org.spongepowered.api.text.format.TextColor) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException)

Example 4 with TextStyle

use of org.spongepowered.api.text.format.TextStyle in project SpongeAPI by SpongePowered.

the class TextFormatConfigSerializer method serialize.

@Override
public void serialize(TypeToken<?> type, TextFormat obj, ConfigurationNode value) throws ObjectMappingException {
    value.getNode(NODE_COLOR).setValue(obj.getColor().getId());
    ConfigurationNode styleNode = value.getNode(NODE_STYLE);
    TextStyle composite = obj.getStyle();
    Sponge.getRegistry().getAllOf(TextStyle.Base.class).forEach(style -> styleNode.getNode(style.getId().toLowerCase()).setValue(composite.contains(style)));
}
Also used : ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) TextStyle(org.spongepowered.api.text.format.TextStyle)

Example 5 with TextStyle

use of org.spongepowered.api.text.format.TextStyle in project Nucleus by NucleusPowered.

the class TextParsingUtils method addUrls.

public static Text addUrls(String message) {
    if (message == null || message.isEmpty()) {
        return Text.EMPTY;
    }
    Matcher m = urlParser.matcher(message);
    if (!m.find()) {
        return TextSerializers.FORMATTING_CODE.deserialize(message);
    }
    List<Text> texts = Lists.newArrayList();
    String remaining = message;
    TextParsingUtils.StyleTuple st = TextParsingUtils.EMPTY;
    do {
        // We found a URL. We split on the URL that we have.
        String[] textArray = remaining.split(urlParser.pattern(), 2);
        Text first = Text.builder().color(st.colour).style(st.style).append(TextSerializers.FORMATTING_CODE.deserialize(textArray[0])).build();
        // Add this text to the list regardless.
        texts.add(first);
        // If we have more to do, shove it into the "remaining" variable.
        if (textArray.length == 2) {
            remaining = textArray[1];
        } else {
            remaining = null;
        }
        // Get the last colour & styles
        String colourMatch = m.group("colour");
        if (colourMatch != null && !colourMatch.isEmpty()) {
            // If there is a reset, explicitly do it.
            TextStyle reset = TextStyles.NONE;
            if (m.group("reset") != null) {
                reset = TextStyles.RESET;
            }
            first = Text.of(reset, TextSerializers.FORMATTING_CODE.deserialize(m.group("colour") + " "));
        }
        st = TextParsingUtils.getLastColourAndStyle(first, st);
        // Build the URL
        String whiteSpace = m.group("first");
        String url = m.group("url");
        texts.add(TextParsingUtils.getTextForUrl(url, url, whiteSpace, st, m.group("options")));
    } while (remaining != null && m.find());
    // Add the last bit.
    if (remaining != null) {
        texts.add(Text.builder().color(st.colour).style(st.style).append(TextSerializers.FORMATTING_CODE.deserialize(remaining)).build());
    }
    // Join it all together.
    return Text.join(texts);
}
Also used : Matcher(java.util.regex.Matcher) TextStyle(org.spongepowered.api.text.format.TextStyle) Text(org.spongepowered.api.text.Text)

Aggregations

TextStyle (org.spongepowered.api.text.format.TextStyle)10 TextColor (org.spongepowered.api.text.format.TextColor)8 Text (org.spongepowered.api.text.Text)6 JsonObject (com.google.gson.JsonObject)2 Optional (java.util.Optional)2 Matcher (java.util.regex.Matcher)2 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)2 TextRepresentable (org.spongepowered.api.text.TextRepresentable)2 HoverAction (org.spongepowered.api.text.action.HoverAction)2 ShiftClickAction (org.spongepowered.api.text.action.ShiftClickAction)2 TextFormat (org.spongepowered.api.text.format.TextFormat)2 Translation (org.spongepowered.api.text.translation.Translation)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 NucleusPlugin (io.github.nucleuspowered.nucleus.NucleusPlugin)1 ModularUserService (io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService)1