Skip to content

Commit

Permalink
fix: hex colors being downsampled when converting to legacy strings
Browse files Browse the repository at this point in the history
Fixes #429
  • Loading branch information
diogotcorreia committed Jul 17, 2024
1 parent 402670f commit bd70339
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.rexcantor64.triton.api.language.SignLocation;
import com.rexcantor64.triton.api.players.LanguagePlayer;
import com.rexcantor64.triton.language.localized.StringLocale;
import com.rexcantor64.triton.utils.ComponentUtils;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
Expand Down Expand Up @@ -40,7 +41,7 @@ public String getText(@NonNull LanguagePlayer p, String code, Object... args) {
.map(arg -> LegacyComponentSerializer.legacyAmpersand().deserialize(arg))
.toArray(Component[]::new);
val resultComponent = triton.getTranslationManager().getTextComponentOr404(p, code, argsComponents);
return LegacyComponentSerializer.legacySection().serialize(resultComponent);
return ComponentUtils.serializeToLegacy(resultComponent);
}

@Deprecated
Expand All @@ -50,7 +51,7 @@ public String getText(@NonNull String languageName, @NonNull String code, @NonNu
.map(arg -> LegacyComponentSerializer.legacyAmpersand().deserialize(arg))
.toArray(Component[]::new);
val resultComponent = triton.getTranslationManager().getTextComponentOr404(new StringLocale(languageName), code, argsComponents);
return LegacyComponentSerializer.legacySection().serialize(resultComponent);
return ComponentUtils.serializeToLegacy(resultComponent);
}

@Deprecated
Expand Down Expand Up @@ -81,13 +82,13 @@ public String[] getSign(@NonNull String language, @NonNull SignLocation location
new StringLocale(language),
location,
() -> Arrays.stream(defaultLines.get())
.map(line -> LegacyComponentSerializer.legacySection().deserialize(line))
.map(ComponentUtils::deserializeFromLegacy)
.toArray(Component[]::new)
);

return signComponents.map(components ->
Arrays.stream(components)
.map(line -> LegacyComponentSerializer.legacySection().serialize(line))
.map(ComponentUtils::serializeToLegacy)
.toArray(String[]::new)
)
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

Expand All @@ -48,10 +57,10 @@ public class AdventureParser implements MessageParser {
@Override
public @NotNull TranslationResult<String> translateString(String text, Localized language, FeatureSyntax syntax) {
return translateComponent(
LegacyComponentSerializer.legacySection().deserialize(text),
ComponentUtils.deserializeFromLegacy(text),
language,
syntax
).map(component -> LegacyComponentSerializer.legacySection().serialize(component));
).map(ComponentUtils::serializeToLegacy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
public class ComponentUtils {

public final static char SECTION_CHAR = '§';
private final static LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacySection()
.toBuilder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.build();

/**
* Deserialize a JSON string representing a {@link Component}.
Expand All @@ -44,6 +49,28 @@ public static String serializeToJson(@NotNull Component component) {
return GsonComponentSerializer.gson().serialize(component);
}

/**
* Deserialize a legacy string, that might contain hex characters in the md_5's
* chat library format, representing a {@link Component}.
*
* @param message The legacy string to deserialize.
* @return The corresponding {@link Component}.
*/
public static Component deserializeFromLegacy(@NotNull String message) {
return LEGACY_SERIALIZER.deserialize(message);
}

/**
* Serialize a {@link Component} to a legacy string, preserving hex characters in
* the md_5's chat library format.
*
* @param component The {@link Component} to serialize.
* @return The corresponding legacy string.
*/
public static String serializeToLegacy(@NotNull Component component) {
return LEGACY_SERIALIZER.serialize(component);
}

/**
* Given a {@link Component}, splits it by new lines, preserving style and hierarchy.
*
Expand Down Expand Up @@ -141,8 +168,7 @@ public static Component unflattenLegacyFormatting(final Component component) {
if (component instanceof TextComponent) {
final TextComponent textComponent = (TextComponent) component;
if (hasLegacyFormatting(textComponent.content())) {
val deserializedComponent = LegacyComponentSerializer.legacySection()
.deserialize(textComponent.content());
val deserializedComponent = deserializeFromLegacy(textComponent.content());
val children = new ArrayList<>(deserializedComponent.children());
children.addAll(newChildren);
return deserializedComponent.applyFallbackStyle(textComponent.style())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.rexcantor64.triton.api.language.Localized;
import com.rexcantor64.triton.spigot.SpigotTriton;
import com.rexcantor64.triton.utils.ComponentUtils;
import lombok.RequiredArgsConstructor;
import lombok.val;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Relational;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -47,7 +47,7 @@ public String onPlaceholderRequest(Player p, @NotNull String params) {
locale = triton.getPlayerManager().get(p.getUniqueId());
}
val component = triton.getTranslationManager().getTextComponentOr404(locale, params);
val text = LegacyComponentSerializer.legacySection().serialize(component);
val text = ComponentUtils.serializeToLegacy(component);

return PlaceholderAPI.setPlaceholders(p, text);
}
Expand Down

0 comments on commit bd70339

Please sign in to comment.