Add legacy section parsing for prefix/suffix

This commit is contained in:
Vankka 2023-07-24 19:56:05 +03:00
parent cb5392fb81
commit 3c3977200f
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
3 changed files with 22 additions and 5 deletions

View File

@ -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);
}

View File

@ -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() {

View File

@ -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;
}
}