From 3c3977200f0e7e951744732341d46bcb99c105cd Mon Sep 17 00:00:00 2001 From: Vankka Date: Mon, 24 Jul 2023 19:56:05 +0300 Subject: [PATCH] Add legacy section parsing for prefix/suffix --- .../api/component/MinecraftComponentFactory.java | 9 ++++++++- .../common/component/ComponentFactory.java | 15 +++++++++++++-- .../common/permission/util/PermissionUtil.java | 3 +-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/com/discordsrv/api/component/MinecraftComponentFactory.java b/api/src/main/java/com/discordsrv/api/component/MinecraftComponentFactory.java index 6bab8799..1ace3056 100644 --- a/api/src/main/java/com/discordsrv/api/component/MinecraftComponentFactory.java +++ b/api/src/main/java/com/discordsrv/api/component/MinecraftComponentFactory.java @@ -40,5 +40,12 @@ public interface MinecraftComponentFactory { @NotNull MinecraftComponent empty(); - GameTextBuilder textBuilder(String content); + /** + * Creates a EnhancedLegacyText {@link GameTextBuilder} based on the given input. + * @param enhancedLegacyText the format + * @return the new builder + */ + @NotNull + GameTextBuilder textBuilder(@NotNull String enhancedLegacyText); + } diff --git a/common/src/main/java/com/discordsrv/common/component/ComponentFactory.java b/common/src/main/java/com/discordsrv/common/component/ComponentFactory.java index 418d34ce..60465bfa 100644 --- a/common/src/main/java/com/discordsrv/common/component/ComponentFactory.java +++ b/common/src/main/java/com/discordsrv/common/component/ComponentFactory.java @@ -25,13 +25,16 @@ import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.component.renderer.DiscordSRVMinecraftRenderer; import com.discordsrv.common.component.translation.Translation; import com.discordsrv.common.component.translation.TranslationRegistry; +import dev.vankka.enhancedlegacytext.EnhancedLegacyText; import dev.vankka.mcdiscordreserializer.discord.DiscordSerializer; import dev.vankka.mcdiscordreserializer.discord.DiscordSerializerOptions; import dev.vankka.mcdiscordreserializer.minecraft.MinecraftSerializer; import dev.vankka.mcdiscordreserializer.minecraft.MinecraftSerializerOptions; +import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.ansi.ColorLevel; import org.jetbrains.annotations.NotNull; @@ -103,8 +106,16 @@ public class ComponentFactory implements MinecraftComponentFactory { } @Override - public GameTextBuilder textBuilder(String content) { - return new EnhancedTextBuilderImpl(discordSRV, content); + public @NotNull GameTextBuilder textBuilder(@NotNull String enhancedLegacyText) { + return new EnhancedTextBuilderImpl(discordSRV, enhancedLegacyText); + } + + public @NotNull Component parse(@NotNull String textInput) { + if (textInput.contains(String.valueOf(LegacyComponentSerializer.SECTION_CHAR))) { + return LegacyComponentSerializer.legacySection().deserialize(textInput); + } + + return EnhancedLegacyText.get().parse(textInput); } public MinecraftSerializer minecraftSerializer() { diff --git a/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java b/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java index cba3f02f..5dec2ee7 100644 --- a/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java +++ b/common/src/main/java/com/discordsrv/common/permission/util/PermissionUtil.java @@ -20,7 +20,6 @@ package com.discordsrv.common.permission.util; import com.discordsrv.api.module.type.PermissionDataProvider; import com.discordsrv.common.DiscordSRV; -import com.discordsrv.common.component.util.ComponentUtil; import com.discordsrv.common.exception.MessageException; import net.kyori.adventure.text.Component; @@ -88,6 +87,6 @@ public final class PermissionUtil { } private static Component translate(DiscordSRV discordSRV, String data) { - return data != null ? ComponentUtil.fromAPI(discordSRV.componentFactory().textBuilder(data).build()) : null; + return data != null ? discordSRV.componentFactory().parse(data) : null; } }