Search in sources :

Example 6 with Translation

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

the class LanternAdvancementProgress method invalidateAchievedState.

public void invalidateAchievedState() {
    // Invalidate the achieved state of all the criteria progress
    this.progress.values().forEach(AbstractCriterionProgress::invalidateAchievedState);
    // Get the new achieved state
    final boolean achievedState = achieved();
    if (!this.achievedState && achievedState) {
        // The advancement got granted
        this.dirtyVisibility = true;
        for (AbstractCriterionProgress progress : this.progress.values()) {
            progress.detachTrigger();
        }
        final Optional<DisplayInfo> optDisplay = this.advancement.getDisplayInfo();
        final boolean sendMessage = getPlayer().getWorld().getOrCreateRule(RuleTypes.ANNOUNCE_ADVANCEMENTS).getValue() && optDisplay.map(DisplayInfo::doesAnnounceToChat).orElse(false);
        final Text message = optDisplay.<Text>map(display -> {
            final Translation translation = tr("chat.type.advancement." + display.getType().getName().toLowerCase());
            return Text.of(translation, getPlayer().getName(), this.advancement.toText());
        }).orElseGet(() -> Text.of(getPlayer().getName() + " achieved ", this.advancement.toText()));
        final MessageEvent.MessageFormatter formatter = new MessageEvent.MessageFormatter(message);
        final Cause cause = CauseStack.current().getCurrentCause();
        final Instant instant = get().orElseThrow(() -> new IllegalStateException("Something funky happened"));
        final AdvancementEvent.Grant event = SpongeEventFactory.createAdvancementEventGrant(cause, MessageChannel.TO_ALL, Optional.of(MessageChannel.TO_ALL), this.advancement, formatter, getPlayer(), instant, !sendMessage);
        Sponge.getEventManager().post(event);
        if (!event.isMessageCancelled()) {
            event.getChannel().ifPresent(channel -> channel.send(event.getMessage()));
        }
    } else if (this.achievedState && !achievedState) {
        // The advancement got revoked
        this.dirtyVisibility = true;
        for (AbstractCriterionProgress progress : this.progress.values()) {
            if (!progress.achieved()) {
                progress.attachTrigger();
            }
        }
        final Cause cause = CauseStack.current().getCurrentCause();
        final AdvancementEvent.Revoke event = SpongeEventFactory.createAdvancementEventRevoke(cause, this.advancement, getPlayer());
        Sponge.getEventManager().post(event);
    }
    this.achievedState = achievedState;
    // The progress should be updated
    this.dirtyProgress = true;
    this.playerAdvancements.dirtyProgress.add(this);
}
Also used : DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) AdvancementEvent(org.spongepowered.api.event.advancement.AdvancementEvent) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) ScoreAdvancementCriterion(org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion) HashMap(java.util.HashMap) Object2LongMap(it.unimi.dsi.fastutil.objects.Object2LongMap) EmptyCriterion(org.lanternpowered.server.advancement.criteria.EmptyCriterion) CauseStack(org.lanternpowered.server.event.CauseStack) Text(org.spongepowered.api.text.Text) LanternOrCriterion(org.lanternpowered.server.advancement.criteria.LanternOrCriterion) AbstractCriterion(org.lanternpowered.server.advancement.criteria.AbstractCriterion) RuleTypes(org.lanternpowered.server.world.rules.RuleTypes) Map(java.util.Map) LanternAndCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternAndCriterionProgress) AdvancementProgress(org.spongepowered.api.advancement.AdvancementProgress) Translation(org.spongepowered.api.text.translation.Translation) LanternCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternCriterionProgress) LanternAndCriterion(org.lanternpowered.server.advancement.criteria.LanternAndCriterion) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) LanternScoreCriterion(org.lanternpowered.server.advancement.criteria.LanternScoreCriterion) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Sponge(org.spongepowered.api.Sponge) LanternScoreCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternScoreCriterionProgress) AdvancementCriterion(org.spongepowered.api.advancement.criteria.AdvancementCriterion) Instant(java.time.Instant) ScoreCriterionProgress(org.spongepowered.api.advancement.criteria.ScoreCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause) LanternOrCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternOrCriterionProgress) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) LanternCriterion(org.lanternpowered.server.advancement.criteria.LanternCriterion) LanternEmptyCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.LanternEmptyCriterionProgress) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) CriterionProgress(org.spongepowered.api.advancement.criteria.CriterionProgress) Advancement(org.spongepowered.api.advancement.Advancement) MessageEvent(org.spongepowered.api.event.message.MessageEvent) Object2LongOpenHashMap(it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap) Translation(org.spongepowered.api.text.translation.Translation) DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) MessageEvent(org.spongepowered.api.event.message.MessageEvent) AdvancementEvent(org.spongepowered.api.event.advancement.AdvancementEvent) Instant(java.time.Instant) Text(org.spongepowered.api.text.Text) AbstractCriterionProgress(org.lanternpowered.server.advancement.criteria.progress.AbstractCriterionProgress) Cause(org.spongepowered.api.event.cause.Cause)

Example 7 with Translation

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

the class JsonTextTranslatableSerializer method deserialize.

@Override
public TranslatableText deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    final JsonObject obj = element.getAsJsonObject();
    final String name = obj.get(TRANSLATABLE).getAsString();
    final Translation translation = this.translationManager.get(name);
    final Object[] arguments;
    if ((element = obj.get(TRANSLATABLE_ARGS)) != null) {
        final Text[] with = context.deserialize(element, Text[].class);
        arguments = new Object[with.length];
        System.arraycopy(with, 0, arguments, 0, with.length);
    } else {
        arguments = new Object[0];
    }
    final TranslatableText.Builder builder = Text.builder(translation, arguments);
    deserialize(obj, builder, context);
    return builder.build();
}
Also used : TranslatableText(org.spongepowered.api.text.TranslatableText) MinecraftTranslation(org.lanternpowered.server.text.translation.MinecraftTranslation) Translation(org.spongepowered.api.text.translation.Translation) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) Text(org.spongepowered.api.text.Text) TranslatableText(org.spongepowered.api.text.TranslatableText)

Example 8 with Translation

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

the class JsonTextTranslatableSerializer method serialize.

@SuppressWarnings("deprecation")
@Override
public JsonElement serialize(TranslatableText src, Type typeOfSrc, JsonSerializationContext context) {
    final Translation translation = src.getTranslation();
    if (this.networkingFormat && !(translation instanceof MinecraftTranslation)) {
        final ImmutableList<Object> arguments = src.getArguments();
        final Object[] rawArguments = arguments.toArray(new Object[arguments.size()]);
        final Locale locale = currentLocale.get();
        for (int i = 0; i < rawArguments.length; i++) {
            Object object = rawArguments[i];
            if (object instanceof TextRepresentable) {
                if (!(object instanceof Text)) {
                    object = ((TextRepresentable) object).toText();
                }
                rawArguments[i] = ((LanternTextSerializer) TextSerializers.LEGACY_FORMATTING_CODE).serialize((Text) object, locale);
            } else {
                rawArguments[i] = object.toString();
            }
        }
        final String content = src.getTranslation().get(locale, rawArguments);
        return JsonTextLiteralSerializer.serializeLiteralText(src, content, context, this.removeComplexity);
    }
    final JsonObject obj = new JsonObject();
    obj.addProperty(TRANSLATABLE, src.getTranslation().getId());
    final ImmutableList<Object> arguments = src.getArguments();
    if (!arguments.isEmpty()) {
        final JsonArray argumentsArray = new JsonArray();
        for (Object object : arguments) {
            // so we need to convert the objects if possible
            if (object instanceof TextRepresentable) {
                if (!(object instanceof Text)) {
                    object = ((TextRepresentable) object).toText();
                }
                argumentsArray.add(context.serialize(object, Text.class));
            } else {
                argumentsArray.add(new JsonPrimitive(object.toString()));
            }
        }
        obj.add(TRANSLATABLE_ARGS, argumentsArray);
    }
    serialize(obj, src, context);
    return obj;
}
Also used : Locale(java.util.Locale) TextRepresentable(org.spongepowered.api.text.TextRepresentable) MinecraftTranslation(org.lanternpowered.server.text.translation.MinecraftTranslation) Translation(org.spongepowered.api.text.translation.Translation) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) Text(org.spongepowered.api.text.Text) TranslatableText(org.spongepowered.api.text.TranslatableText) JsonArray(com.google.gson.JsonArray) MinecraftTranslation(org.lanternpowered.server.text.translation.MinecraftTranslation) JsonObject(com.google.gson.JsonObject)

Example 9 with Translation

use of org.spongepowered.api.text.translation.Translation 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

Translation (org.spongepowered.api.text.translation.Translation)8 Optional (java.util.Optional)4 Text (org.spongepowered.api.text.Text)4 TranslatableText (org.spongepowered.api.text.TranslatableText)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 JsonObject (com.google.gson.JsonObject)2 MinecraftTranslation (org.lanternpowered.server.text.translation.MinecraftTranslation)2 TextRepresentable (org.spongepowered.api.text.TextRepresentable)2 TextColor (org.spongepowered.api.text.format.TextColor)2 TextFormat (org.spongepowered.api.text.format.TextFormat)2 TextStyle (org.spongepowered.api.text.format.TextStyle)2 Translatable (org.spongepowered.api.text.translation.Translatable)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 JsonArray (com.google.gson.JsonArray)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 Object2IntMap (it.unimi.dsi.fastutil.objects.Object2IntMap)1 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)1 Object2LongMap (it.unimi.dsi.fastutil.objects.Object2LongMap)1