From 6dcb2defa59747ac3229aafb3fe8c06a6cc20ebc Mon Sep 17 00:00:00 2001 From: JFronny Date: Sun, 28 Aug 2022 13:07:34 +0200 Subject: [PATCH] [translate] Adjust config implementation for new features --- .../libjf/config/plugin/StreamAction.java | 1 + .../libjf/config/api/v1/type/Type.java | 9 +++-- .../ui/tiny/entry/EntryInfoWidgetBuilder.java | 13 +++++--- .../libjf/translate/api/TranslateService.java | 33 +++++++------------ .../libjf/translate/impl/TranslateConfig.java | 17 +++++----- .../impl/google/GoogleTranslateService.java | 3 +- .../libretranslate/LibreTranslateService.java | 5 +-- .../impl/noop/NoopTranslateService.java | 3 +- .../assets/libjf-translate-v0/lang/en_us.json | 6 ++-- 9 files changed, 47 insertions(+), 43 deletions(-) diff --git a/libjf-config-compiler-plugin/src/main/java/io/gitlab/jfronny/libjf/config/plugin/StreamAction.java b/libjf-config-compiler-plugin/src/main/java/io/gitlab/jfronny/libjf/config/plugin/StreamAction.java index 12d70d9..55853bd 100644 --- a/libjf-config-compiler-plugin/src/main/java/io/gitlab/jfronny/libjf/config/plugin/StreamAction.java +++ b/libjf-config-compiler-plugin/src/main/java/io/gitlab/jfronny/libjf/config/plugin/StreamAction.java @@ -153,6 +153,7 @@ public class StreamAction { final ConfigInjectClassTransformer transformer = new ConfigInjectClassTransformer(modId, writer, knownConfigClasses); reader.accept(transformer, ClassReader.EXPAND_FRAMES); klazz = writer.toByteArray(); + System.out.println("Injected config registration into " + path); } catch (NotAConfigClassException notAConfigClass) { // Use original bytes } diff --git a/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/api/v1/type/Type.java b/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/api/v1/type/Type.java index 76547be..bdf3cae 100644 --- a/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/api/v1/type/Type.java +++ b/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/api/v1/type/Type.java @@ -14,7 +14,7 @@ public sealed interface Type { else if (klazz == String.class) return TString.INSTANCE; else if (klazz == boolean.class || klazz == Boolean.class) return TBool.INSTANCE; else if (klazz instanceof Class k && k.isEnum()) return new TEnum<>(k); - else return new TUnknown<>(klazz); + else return new TUnknown(klazz); } default boolean isInt() { @@ -38,6 +38,11 @@ public sealed interface Type { default boolean isEnum() { return false; } + @SuppressWarnings("unchecked") + default TEnum asEnum() { + if (this instanceof TEnum c) return (TEnum) c; + throw new ClassCastException("This type is not an enum"); + } @Nullable java.lang.reflect.Type asClass(); @@ -189,7 +194,7 @@ public sealed interface Type { } } - final record TUnknown(java.lang.reflect.Type klazz) implements Type { + final record TUnknown(java.lang.reflect.Type klazz) implements Type { @Override public @Nullable java.lang.reflect.Type asClass() { return klazz; diff --git a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/entry/EntryInfoWidgetBuilder.java b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/entry/EntryInfoWidgetBuilder.java index 5b64aed..5eb85f1 100644 --- a/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/entry/EntryInfoWidgetBuilder.java +++ b/libjf-config-ui-tiny-v1/src/client/java/io/gitlab/jfronny/libjf/config/impl/ui/tiny/entry/EntryInfoWidgetBuilder.java @@ -15,8 +15,7 @@ import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.text.Text; import net.minecraft.util.Formatting; -import java.util.ArrayList; -import java.util.List; +import java.util.*; import java.util.function.Function; import java.util.regex.Pattern; @@ -48,11 +47,17 @@ public class EntryInfoWidgetBuilder { value -> !(Boolean) value, value -> Text.literal((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED)); } else if (type.isEnum()) { - T[] values = ((Type.TEnum)type).options(); + T[] values = type.asEnum().options(); factory = toggle(info, state, value -> { int index = indexOf(values, value) + 1; return values[index >= values.length ? 0 : index]; - }, value -> Text.translatable(config.getTranslationPrefix() + "enum." + type.getName() + "." + state.cachedValue)); + }, value -> { + if (type.asClass() == null) { + return Text.translatable(config.getTranslationPrefix() + info.getName() + "." + state.cachedValue); + } else { + return Text.translatable(config.getTranslationPrefix() + "enum." + type.getName() + "." + state.cachedValue); + } + }); } else { LibJf.LOGGER.error("Unsupported entry type in " + info.getName() + ": " + type.getName() + " - not displaying config control"); factory = ((screenWidth, textRenderer, done) -> new WidgetFactory.Widget(() -> {}, new ButtonWidget(-10, 0, 0, 0, Text.of(""), null))); diff --git a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/api/TranslateService.java b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/api/TranslateService.java index a383610..e369d1f 100644 --- a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/api/TranslateService.java +++ b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/api/TranslateService.java @@ -16,25 +16,13 @@ public interface TranslateService { * @return configured translation service */ static TranslateService getConfigured() { - return switch (TranslateConfig.translationService) { - case Noop -> NoopTranslateService.INSTANCE; - case Google -> { - try { - yield GoogleTranslateService.get(); - } catch (URISyntaxException | IOException e) { - LibJf.LOGGER.error("Could not create google translate service"); - yield NoopTranslateService.INSTANCE; - } - } - case LibreTranslate -> { - try { - yield LibreTranslateService.get(TranslateConfig.libreTranslateHost); - } catch (TranslateException e) { - LibJf.LOGGER.error("Could not use the specified LibreTranslate host, using NOOP", e); - yield NoopTranslateService.INSTANCE; - } - } - }; + return getAvailable().stream() + .filter(svc -> svc.getName().equals(TranslateConfig.translationService)) + .findFirst() + .orElseGet(() -> { + LibJf.LOGGER.error("Falling back to NOOP translate service"); + return NoopTranslateService.INSTANCE; + }); } /** @@ -45,12 +33,15 @@ public interface TranslateService { List> result = new LinkedList<>(); try { result.add(GoogleTranslateService.get()); - } catch (URISyntaxException | IOException ignored) { + } catch (URISyntaxException | IOException e) { + LibJf.LOGGER.error("Could not create Google Translate service", e); } try { result.add(LibreTranslateService.get(TranslateConfig.libreTranslateHost)); - } catch (TranslateException ignored) { + } catch (TranslateException e) { + LibJf.LOGGER.error("Could not create LibreTranslate service", e); } + if (result.isEmpty()) result.add(NoopTranslateService.INSTANCE); return List.copyOf(result); } diff --git a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/TranslateConfig.java b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/TranslateConfig.java index adc1cf7..9235d97 100644 --- a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/TranslateConfig.java +++ b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/TranslateConfig.java @@ -1,15 +1,18 @@ package io.gitlab.jfronny.libjf.translate.impl; -import io.gitlab.jfronny.libjf.config.api.v1.*; +import io.gitlab.jfronny.libjf.config.api.v1.JfCustomConfig; import io.gitlab.jfronny.libjf.config.api.v1.dsl.DSL; +import io.gitlab.jfronny.libjf.translate.api.TranslateService; +import io.gitlab.jfronny.libjf.translate.impl.google.GoogleTranslateService; +import io.gitlab.jfronny.libjf.translate.impl.libretranslate.LibreTranslateService; public class TranslateConfig implements JfCustomConfig { - public static Translator translationService = Translator.Google; + public static String translationService = GoogleTranslateService.NAME; public static String libreTranslateHost = "https://translate.argosopentech.com"; public static void ensureValid() { - if (translationService == null) translationService = Translator.Google; - if (translationService == Translator.LibreTranslate && libreTranslateHost == null || libreTranslateHost.isBlank()) + if (translationService == null) translationService = GoogleTranslateService.NAME; + if (translationService.equals(LibreTranslateService.NAME) && libreTranslateHost == null || libreTranslateHost.isBlank()) libreTranslateHost = "https://translate.argosopentech.com"; } @@ -17,13 +20,9 @@ public class TranslateConfig implements JfCustomConfig { public void register(DSL.Defaulted dsl) { } - public enum Translator { - Google, LibreTranslate, Noop - } - static { DSL.create("libjf-translate-v1").register(b -> b - .value("translationService", translationService, Translator.class, () -> translationService, v -> translationService = v) + .value("translationService", translationService, TranslateService.getAvailable().stream().map(TranslateService::getName).toArray(String[]::new), () -> translationService, v -> translationService = v) .value("libreTranslateHost", libreTranslateHost, () -> libreTranslateHost, v -> libreTranslateHost = v) .addVerifier(TranslateConfig::ensureValid) ); diff --git a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java index 1061eaf..8145cd1 100644 --- a/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java +++ b/libjf-translate-v1/src/main/java/io/gitlab/jfronny/libjf/translate/impl/google/GoogleTranslateService.java @@ -12,6 +12,7 @@ import java.util.*; import java.util.regex.*; public class GoogleTranslateService implements TranslateService { + public static final String NAME = "Google"; private static GoogleTranslateService INSTANCE; private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)", Pattern.MULTILINE); private static final Pattern LANGUAGE_KEY = Pattern.compile("", Pattern.MULTILINE); @@ -83,7 +84,7 @@ public class GoogleTranslateService implements TranslateService { + public static final String NAME = "LibreTranslate"; private static final Type languageListType = new TypeToken>(){}.getType(); private static final Type translateDetectResultListType = new TypeToken>(){}.getType(); private static final LibreTranslateLanguage autoDetect = new LibreTranslateLanguage("auto", "AUTO_DETECT"); @@ -101,6 +102,6 @@ public class LibreTranslateService implements TranslateService { + public static final String NAME = "Noop"; public static final NoopTranslateService INSTANCE = new NoopTranslateService(); private NoopTranslateService() { } @@ -32,6 +33,6 @@ public class NoopTranslateService implements TranslateService { @Override public String getName() { - return "Noop"; + return NAME; } } diff --git a/libjf-translate-v1/src/main/resources/assets/libjf-translate-v0/lang/en_us.json b/libjf-translate-v1/src/main/resources/assets/libjf-translate-v0/lang/en_us.json index b413f8b..916c8b4 100644 --- a/libjf-translate-v1/src/main/resources/assets/libjf-translate-v0/lang/en_us.json +++ b/libjf-translate-v1/src/main/resources/assets/libjf-translate-v0/lang/en_us.json @@ -4,7 +4,7 @@ "libjf-translate-v1.jfconfig.translationService.tooltip": "The service to use for translation. Other mods may access services directly, but this should be used", "libjf-translate-v1.jfconfig.libreTranslateHost": "LibreTranslate Host", "libjf-translate-v1.jfconfig.libreTranslateHost.tooltip": "The host of LibreTranslate to use if that is selected", - "libjf-translate-v1.jfconfig.enum.Translator.Google": "Google", - "libjf-translate-v1.jfconfig.enum.Translator.LibreTranslate": "LibreTranslate", - "libjf-translate-v1.jfconfig.enum.Translator.Noop": "NOOP" + "libjf-translate-v1.jfconfig.translationService.Google": "Google", + "libjf-translate-v1.jfconfig.translationService.LibreTranslate": "LibreTranslate", + "libjf-translate-v1.jfconfig.translationService.Noop": "NOOP" }