Search in sources :

Example 1 with LiteralText

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

the class JsonTextLiteralSerializer method deserialize.

@Override
public LiteralText deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    if (json.isJsonPrimitive()) {
        return Text.of(json.getAsString());
    }
    JsonObject json0 = json.getAsJsonObject();
    LiteralText.Builder builder = Text.builder(json0.get(TEXT).getAsString());
    deserialize(json0, builder, context);
    return builder.build();
}
Also used : JsonObject(com.google.gson.JsonObject) LiteralText(org.spongepowered.api.text.LiteralText)

Example 2 with LiteralText

use of org.spongepowered.api.text.LiteralText in project SpongeCommon by SpongePowered.

the class ScaledHealthTest method getSetHealthScale.

private static CommandCallable getSetHealthScale() {
    final LiteralText healthText = Text.of("health");
    return CommandSpec.builder().arguments(onlyOne(doubleNum(healthText))).description(Text.of(TextColors.AQUA, "Sets your health scale")).executor(((src, args) -> {
        if (!(src instanceof Player)) {
            return CommandResult.empty();
        }
        final double health = args.<Double>getOne(Text.of(healthText)).get();
        ((Player) src).offer(Keys.HEALTH_SCALE, health);
        return CommandResult.success();
    })).build();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) LiteralText(org.spongepowered.api.text.LiteralText)

Example 3 with LiteralText

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

the class TextTemplateConfigSerializer method parseArg.

private void parseArg(LiteralText source, List<Object> into) throws ObjectMappingException {
    String name = unwrap(source.getContent());
    boolean optional = this.root.getNode(NODE_ARGS, name, NODE_OPT).getBoolean();
    Text defaultValue = this.root.getNode(NODE_ARGS, name, NODE_DEF_VAL).getValue(TypeToken.of(Text.class));
    TextFormat format = source.getFormat();
    into.add(TextTemplate.arg(name).format(format).optional(optional).defaultValue(defaultValue).build());
}
Also used : TextFormat(org.spongepowered.api.text.format.TextFormat) Text(org.spongepowered.api.text.Text) LiteralText(org.spongepowered.api.text.LiteralText)

Example 4 with LiteralText

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

the class PaginationCalculator method getLength.

private int getLength(Text text, boolean bold) {
    final Boolean bold1 = text.getStyle().isBold().orElse(null);
    if (bold1 != null) {
        bold = bold1;
    }
    String txt;
    if (text instanceof LiteralText) {
        txt = ((LiteralText) text).getContent();
    } else {
        txt = text.toPlainSingle();
    }
    int length = getLength(txt, bold);
    // Get the length of all the children
    for (Text child : text.getChildren()) {
        length += getLength(child, bold);
    }
    return length;
}
Also used : LiteralText(org.spongepowered.api.text.LiteralText) Text(org.spongepowered.api.text.Text) LiteralText(org.spongepowered.api.text.LiteralText)

Example 5 with LiteralText

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

the class LegacyTexts method toLegacy.

private static StringBuilder toLegacy(StringBuilder builder, Locale locale, Text text, char legacyChar, Style base, Style applied) {
    if (legacyChar != 0) {
        final TextFormat format = text.getFormat();
        final TextColor color = format.getColor();
        final TextStyle style = format.getStyle();
        // Create a new style object
        final Style newStyle = base.copyTo(new Style());
        base = newStyle;
        if (color != TextColors.NONE) {
            newStyle.color = color == TextColors.RESET ? null : color;
        }
        style.isBold().ifPresent(value -> newStyle.bold = value);
        style.isItalic().ifPresent(value -> newStyle.italic = value);
        style.isObfuscated().ifPresent(value -> newStyle.obfuscated = value);
        style.hasUnderline().ifPresent(value -> newStyle.underlined = value);
        style.hasStrikethrough().ifPresent(value -> newStyle.strikethrough = value);
        if ((applied.color != null && newStyle.color == null) || (applied.bold && !newStyle.bold) || (applied.italic && !newStyle.italic) || (applied.obfuscated && !newStyle.obfuscated) || (applied.underlined && !newStyle.underlined) || (applied.strikethrough && !newStyle.strikethrough)) {
            builder.append(legacyChar).append(TextConstants.RESET);
            if (newStyle.color != null) {
                builder.append(legacyChar).append(((FormattingCodeHolder) newStyle.color).getCode());
            }
            if (newStyle.bold) {
                builder.append(legacyChar).append(TextConstants.BOLD);
            }
            if (newStyle.italic) {
                builder.append(legacyChar).append(TextConstants.ITALIC);
            }
            if (newStyle.obfuscated) {
                builder.append(legacyChar).append(TextConstants.OBFUSCATED);
            }
            if (newStyle.underlined) {
                builder.append(legacyChar).append(TextConstants.UNDERLINE);
            }
            if (newStyle.strikethrough) {
                builder.append(legacyChar).append(TextConstants.STRIKETHROUGH);
            }
        } else {
            if (applied.color != newStyle.color) {
                builder.append(legacyChar).append(((FormattingCodeHolder) newStyle.color).getCode());
            }
            if (applied.bold != newStyle.bold) {
                builder.append(legacyChar).append(TextConstants.BOLD);
            }
            if (applied.italic != newStyle.italic) {
                builder.append(legacyChar).append(TextConstants.ITALIC);
            }
            if (applied.obfuscated != newStyle.obfuscated) {
                builder.append(legacyChar).append(TextConstants.OBFUSCATED);
            }
            if (applied.underlined != newStyle.underlined) {
                builder.append(legacyChar).append(TextConstants.UNDERLINE);
            }
            if (applied.strikethrough != newStyle.strikethrough) {
                builder.append(legacyChar).append(TextConstants.STRIKETHROUGH);
            }
        }
        newStyle.copyTo(applied);
    }
    if (text instanceof LiteralText) {
        builder.append(((LiteralText) text).getContent());
    } else if (text instanceof SelectorText) {
        builder.append(((SelectorText) text).getSelector().toPlain());
    } else if (text instanceof TranslatableText) {
        final TranslatableText text0 = (TranslatableText) text;
        final Translation translation = text0.getTranslation();
        final ImmutableList<Object> args = text0.getArguments();
        final Object[] args0 = new Object[args.size()];
        for (int i = 0; i < args0.length; i++) {
            Object object = args.get(i);
            if (object instanceof Text || object instanceof Text.Builder || object instanceof TextRepresentable) {
                if (object instanceof Text) {
                // Ignore
                } else if (object instanceof Text.Builder) {
                    object = ((Text.Builder) object).build();
                } else {
                    object = ((TextRepresentable) object).toText();
                }
                args0[i] = toLegacy(new StringBuilder(), locale, (Text) object, legacyChar, base, applied).toString();
            } else {
                args0[i] = object;
            }
        }
        builder.append(translation.get(locale, args0));
    } else if (text instanceof ScoreText) {
        final ScoreText text0 = (ScoreText) text;
        final Optional<String> override = text0.getOverride();
        if (override.isPresent()) {
            builder.append(override.get());
        } else {
            builder.append(text0.getScore().getScore());
        }
    }
    for (Text child : text.getChildren()) {
        toLegacy(builder, locale, child, legacyChar, base, applied);
    }
    return builder;
}
Also used : TranslatableText(org.spongepowered.api.text.TranslatableText) TextRepresentable(org.spongepowered.api.text.TextRepresentable) SelectorText(org.spongepowered.api.text.SelectorText) Translation(org.spongepowered.api.text.translation.Translation) Optional(java.util.Optional) ScoreText(org.spongepowered.api.text.ScoreText) LiteralText(org.spongepowered.api.text.LiteralText) ScoreText(org.spongepowered.api.text.ScoreText) SelectorText(org.spongepowered.api.text.SelectorText) Text(org.spongepowered.api.text.Text) TranslatableText(org.spongepowered.api.text.TranslatableText) TextStyle(org.spongepowered.api.text.format.TextStyle) TextFormat(org.spongepowered.api.text.format.TextFormat) TextStyle(org.spongepowered.api.text.format.TextStyle) TextColor(org.spongepowered.api.text.format.TextColor) LiteralText(org.spongepowered.api.text.LiteralText)

Aggregations

LiteralText (org.spongepowered.api.text.LiteralText)5 Text (org.spongepowered.api.text.Text)3 TextFormat (org.spongepowered.api.text.format.TextFormat)2 JsonObject (com.google.gson.JsonObject)1 Optional (java.util.Optional)1 Player (org.spongepowered.api.entity.living.player.Player)1 ScoreText (org.spongepowered.api.text.ScoreText)1 SelectorText (org.spongepowered.api.text.SelectorText)1 TextRepresentable (org.spongepowered.api.text.TextRepresentable)1 TranslatableText (org.spongepowered.api.text.TranslatableText)1 TextColor (org.spongepowered.api.text.format.TextColor)1 TextStyle (org.spongepowered.api.text.format.TextStyle)1 Translation (org.spongepowered.api.text.translation.Translation)1