Skip to content

Commit

Permalink
Fixed component serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Aug 10, 2021
1 parent 97eeea8 commit 7216d0b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions eco-api/src/main/java/com/willfp/eco/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -48,6 +47,15 @@ public class StringUtils {
.add(Pattern.compile("<#" + "([A-Fa-f0-9]{6})" + ">"))
.build();

/**
* Legacy serializer.
*/
private static final LegacyComponentSerializer LEGACY_COMPONENT_SERIALIZER = LegacyComponentSerializer.builder()
.character('\u00a7')
.useUnusualXRepeatedCharacterHexFormat()
.hexColors()
.build();

/**
* Format a list of strings - converts Placeholders and Color codes.
*
Expand Down Expand Up @@ -245,14 +253,11 @@ public String removePrefix(@NotNull final String string,
* @return The JSON String.
*/
public String legacyToJson(@NotNull final String legacy) {
String json = GsonComponentSerializer.gson().serialize(
return GsonComponentSerializer.gson().serialize(
Component.empty().decoration(TextDecoration.ITALIC, false).append(
LegacyComponentSerializer.legacySection().deserialize(legacy)
LEGACY_COMPONENT_SERIALIZER.deserialize(legacy)
)
);

Bukkit.getLogger().info(legacy + " -> " + json);
return json;
}

/**
Expand All @@ -262,11 +267,8 @@ public String legacyToJson(@NotNull final String legacy) {
* @return The legacy string.
*/
public String jsonToLegacy(@NotNull final String json) {
String legacy = LegacyComponentSerializer.legacySection().serialize(
return LEGACY_COMPONENT_SERIALIZER.serialize(
GsonComponentSerializer.gson().deserialize(json)
);

Bukkit.getLogger().info(json + " -> " + legacy);
return legacy;
}
}

0 comments on commit 7216d0b

Please sign in to comment.