Ansi 1.1.0-SNAPSHOT

This commit is contained in:
Vankka 2023-10-06 00:12:13 +03:00
parent 849a446d2a
commit c927c3cbe5
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
3 changed files with 8 additions and 43 deletions

View File

@ -85,12 +85,13 @@ dependencies {
// Logging // Logging
compileOnly(libs.log4j.core) compileOnly(libs.log4j.core)
// Adventure, MCDiscordReserializer, EnhancedLegacyText // Adventure, ANSI (version upgrade for serializer), MCDiscordReserializer, EnhancedLegacyText
runtimeDownloadApi(libs.adventure.api) runtimeDownloadApi(libs.adventure.api)
runtimeDownloadApi(libs.adventure.serializer.plain) runtimeDownloadApi(libs.adventure.serializer.plain)
runtimeDownloadApi(libs.adventure.serializer.legacy) runtimeDownloadApi(libs.adventure.serializer.legacy)
runtimeDownloadApi(libs.adventure.serializer.gson) runtimeDownloadApi(libs.adventure.serializer.gson)
runtimeDownloadApi(libs.adventure.serializer.ansi) runtimeDownloadApi(libs.adventure.serializer.ansi)
runtimeDownloadApi(libs.kyori.ansi)
runtimeDownloadApi(libs.mcdiscordreserializer) runtimeDownloadApi(libs.mcdiscordreserializer)
runtimeDownloadApi(libs.enhancedlegacytext) runtimeDownloadApi(libs.enhancedlegacytext)

View File

@ -33,15 +33,13 @@ import dev.vankka.mcdiscordreserializer.minecraft.MinecraftSerializerOptions;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.flattener.ComponentFlattener; import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer; import net.kyori.adventure.text.serializer.ansi.ANSIComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.kyori.ansi.ColorLevel; import net.kyori.ansi.ColorLevel;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.*; import java.util.Locale;
public class ComponentFactory implements MinecraftComponentFactory { public class ComponentFactory implements MinecraftComponentFactory {
@ -60,7 +58,6 @@ public class ComponentFactory implements MinecraftComponentFactory {
private final DiscordSerializer discordSerializer; private final DiscordSerializer discordSerializer;
private final PlainTextComponentSerializer plainSerializer; private final PlainTextComponentSerializer plainSerializer;
private final ANSIComponentSerializer ansiSerializer; private final ANSIComponentSerializer ansiSerializer;
private final ANSIComponentSerializerWrapper ansiWrapper = new ANSIComponentSerializerWrapper();
// Not the same as Adventure's TranslationRegistry // Not the same as Adventure's TranslationRegistry
private final TranslationRegistry translationRegistry = new TranslationRegistry(); private final TranslationRegistry translationRegistry = new TranslationRegistry();
@ -83,7 +80,7 @@ public class ComponentFactory implements MinecraftComponentFactory {
.flattener(flattener) .flattener(flattener)
.build(); .build();
this.ansiSerializer = ANSIComponentSerializer.builder() this.ansiSerializer = ANSIComponentSerializer.builder()
.colorLevel(ColorLevel.INDEXED_16) .colorLevel(ColorLevel.INDEXED_8)
.flattener(flattener) .flattener(flattener)
.build(); .build();
} }
@ -134,47 +131,11 @@ public class ComponentFactory implements MinecraftComponentFactory {
} }
public ANSIComponentSerializer ansiSerializer() { public ANSIComponentSerializer ansiSerializer() {
return ansiWrapper; return ansiSerializer;
} }
public TranslationRegistry translationRegistry() { public TranslationRegistry translationRegistry() {
return translationRegistry; return translationRegistry;
} }
private static final Set<TextColor> ANSI_SUPPORTED_COLORS = new HashSet<>(Arrays.asList(
NamedTextColor.BLACK,
NamedTextColor.DARK_BLUE,
NamedTextColor.DARK_GREEN,
NamedTextColor.DARK_AQUA,
NamedTextColor.DARK_RED,
NamedTextColor.DARK_PURPLE,
NamedTextColor.GOLD,
NamedTextColor.GRAY
));
/**
* "Fix" for Discord only supporting 8 colors.
* <a href="https://github.com/KyoriPowered/ansi/issues/35">KyoriPowered/ansi issue</a>
*/
private class ANSIComponentSerializerWrapper implements ANSIComponentSerializer {
@Override
public @NotNull String serialize(@NotNull Component component) {
return ansiSerializer.serialize(recursivelyCheckColor(component));
}
private Component recursivelyCheckColor(Component component) {
if (!ANSI_SUPPORTED_COLORS.contains(component.color())) {
component = component.color(null);
}
List<Component> children = component.children();
List<Component> newChildren = new ArrayList<>(children.size());
for (Component child : children) {
newChildren.add(recursivelyCheckColor(child));
}
return component.children(newChildren);
}
}
} }

View File

@ -126,6 +126,9 @@ dependencyResolutionManagement {
library('adventure-platform-bungee', 'net.kyori', 'adventure-platform-bungeecord').versionRef('adventure-platform') library('adventure-platform-bungee', 'net.kyori', 'adventure-platform-bungeecord').versionRef('adventure-platform')
library('adventure-serializer-bungee', 'net.kyori', 'adventure-text-serializer-bungeecord').versionRef('adventure-platform') library('adventure-serializer-bungee', 'net.kyori', 'adventure-text-serializer-bungeecord').versionRef('adventure-platform')
// Upgrade ansi (used by ansi serializer)
library('kyori-ansi', 'net.kyori', 'ansi').version('1.1.0-SNAPSHOT')
// MCDiscordReserializer & EnhancedLegacyText // MCDiscordReserializer & EnhancedLegacyText
library('mcdiscordreserializer', 'dev.vankka', 'mcdiscordreserializer').version('4.4.0-SNAPSHOT') library('mcdiscordreserializer', 'dev.vankka', 'mcdiscordreserializer').version('4.4.0-SNAPSHOT')
library('enhancedlegacytext', 'dev.vankka', 'enhancedlegacytext').version('2.0.0-SNAPSHOT') library('enhancedlegacytext', 'dev.vankka', 'enhancedlegacytext').version('2.0.0-SNAPSHOT')