Search in sources :

Example 1 with ShiftClickAction

use of org.spongepowered.api.text.action.ShiftClickAction 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 ShiftClickAction

use of org.spongepowered.api.text.action.ShiftClickAction in project SpongeAPI by SpongePowered.

the class Text method of.

/**
 * Builds a {@link Text} from a given array of objects.
 *
 * <p>For instance, you can use this like
 * <code>Text.of(TextColors.DARK_AQUA, "Hi", TextColors.AQUA, "Bye")</code>
 * </p>
 *
 * <p>This will create the correct {@link Text} instance if the input object
 * is the input for one of the {@link Text} types or convert the object to a
 * string otherwise.</p>
 *
 * <p>For instances of type {@link TextRepresentable} (e.g. {@link Text},
 * {@link Builder}, ...) the formatting of appended text has priority over
 * the current formatting in the method, e.g. the following results in a
 * green, then yellow and at the end green again {@link Text}:</p>
 *
 * <code>Text.of(TextColors.GREEN, "Hello ", Text.of(TextColors.YELLOW,
 * "Spongie"), '!');</code>
 *
 * @param objects The object array
 * @return The built text object
 */
public static Text of(Object... objects) {
    // Shortcut for lonely TextRepresentables
    if (objects.length == 1 && objects[0] instanceof TextRepresentable) {
        return ((TextRepresentable) objects[0]).toText();
    }
    final Text.Builder builder = builder();
    TextFormat format = TextFormat.NONE;
    HoverAction<?> hoverAction = null;
    ClickAction<?> clickAction = null;
    ShiftClickAction<?> shiftClickAction = null;
    boolean changedFormat = false;
    for (Object obj : objects) {
        // Text formatting + actions
        if (obj instanceof TextFormat) {
            changedFormat = true;
            format = (TextFormat) obj;
        } else if (obj instanceof TextColor) {
            changedFormat = true;
            format = format.color((TextColor) obj);
        } else if (obj instanceof TextStyle) {
            changedFormat = true;
            format = format.style(obj.equals(TextStyles.RESET) ? TextStyles.NONE : format.getStyle().and((TextStyle) obj));
        } else if (obj instanceof TextAction) {
            changedFormat = true;
            if (obj instanceof HoverAction) {
                hoverAction = (HoverAction<?>) obj;
            } else if (obj instanceof ClickAction) {
                clickAction = (ClickAction<?>) obj;
            } else if (obj instanceof ShiftClickAction) {
                shiftClickAction = (ShiftClickAction<?>) obj;
            } else {
            // Unsupported TextAction
            }
        } else if (obj instanceof TextRepresentable) {
            // Special content
            changedFormat = false;
            Text.Builder childBuilder = ((TextRepresentable) obj).toText().toBuilder();
            // Merge format (existing format has priority)
            childBuilder.format(format.merge(childBuilder.format));
            // Overwrite text actions if *NOT* present
            if (childBuilder.clickAction == null) {
                childBuilder.clickAction = clickAction;
            }
            if (childBuilder.hoverAction == null) {
                childBuilder.hoverAction = hoverAction;
            }
            if (childBuilder.shiftClickAction == null) {
                childBuilder.shiftClickAction = shiftClickAction;
            }
            builder.append(childBuilder.build());
        } else {
            // Simple content
            changedFormat = false;
            Text.Builder childBuilder;
            if (obj instanceof String) {
                childBuilder = builder((String) obj);
            } else if (obj instanceof Translation) {
                childBuilder = builder((Translation) obj);
            } else if (obj instanceof Translatable) {
                childBuilder = builder(((Translatable) obj).getTranslation());
            } else if (obj instanceof Selector) {
                childBuilder = builder((Selector) obj);
            } else if (obj instanceof Score) {
                childBuilder = builder((Score) obj);
            } else {
                childBuilder = builder(String.valueOf(obj));
            }
            if (hoverAction != null) {
                childBuilder.onHover(hoverAction);
            }
            if (clickAction != null) {
                childBuilder.onClick(clickAction);
            }
            if (shiftClickAction != null) {
                childBuilder.onShiftClick(shiftClickAction);
            }
            builder.append(childBuilder.format(format).build());
        }
    }
    if (changedFormat) {
        // Did the formatting change without being applied to something?
        // Then just append an empty text with that formatting
        final Text.Builder childBuilder = builder();
        if (hoverAction != null) {
            childBuilder.onHover(hoverAction);
        }
        if (clickAction != null) {
            childBuilder.onClick(clickAction);
        }
        if (shiftClickAction != null) {
            childBuilder.onShiftClick(shiftClickAction);
        }
        builder.append(childBuilder.format(format).build());
    }
    if (builder.children.size() == 1) {
        // Single content, reduce Text depth
        return builder.children.get(0);
    }
    return builder.build();
}
Also used : Translation(org.spongepowered.api.text.translation.Translation) Translatable(org.spongepowered.api.text.translation.Translatable) TextAction(org.spongepowered.api.text.action.TextAction) Score(org.spongepowered.api.scoreboard.Score) HoverAction(org.spongepowered.api.text.action.HoverAction) TextStyle(org.spongepowered.api.text.format.TextStyle) ClickAction(org.spongepowered.api.text.action.ClickAction) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) ShiftClickAction(org.spongepowered.api.text.action.ShiftClickAction) TextFormat(org.spongepowered.api.text.format.TextFormat) TextColor(org.spongepowered.api.text.format.TextColor) Selector(org.spongepowered.api.text.selector.Selector)

Aggregations

ShiftClickAction (org.spongepowered.api.text.action.ShiftClickAction)2 TextColor (org.spongepowered.api.text.format.TextColor)2 TextStyle (org.spongepowered.api.text.format.TextStyle)2 JsonObject (com.google.gson.JsonObject)1 RawAction (org.lanternpowered.server.text.LanternTextHelper.RawAction)1 Score (org.spongepowered.api.scoreboard.Score)1 ClickAction (org.spongepowered.api.text.action.ClickAction)1 HoverAction (org.spongepowered.api.text.action.HoverAction)1 TextAction (org.spongepowered.api.text.action.TextAction)1 TextFormat (org.spongepowered.api.text.format.TextFormat)1 Selector (org.spongepowered.api.text.selector.Selector)1 Translatable (org.spongepowered.api.text.translation.Translatable)1 Translation (org.spongepowered.api.text.translation.Translation)1