From 460a2811e90bdc34b802a1cc250c86e79ba53efa Mon Sep 17 00:00:00 2001 From: Lyft <127234178+Lyfts@users.noreply.github.com> Date: Sat, 14 Sep 2024 01:02:58 +0200 Subject: [PATCH] make langkey pattern able to use qualified category names (#72) --- .../java/com/gtnewhorizon/gtnhlib/config/Config.java | 7 +++++++ .../gtnhlib/config/ConfigurationManager.java | 12 +++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java index 117ebd9..7fb45e1 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java @@ -56,6 +56,13 @@ @interface LangKeyPattern { String pattern() default "%mod.%cat.%field"; + + /** + * Whether subcategories should use their fully qualified name.
+ * Fully qualified: {@code category.category1.category2}
+ * Normal: {@code category2} + */ + boolean fullyQualified() default false; } @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java index 7e6ad3d..425d68f 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java @@ -25,6 +25,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.jetbrains.annotations.NotNull; import cpw.mods.fml.client.config.IConfigElement; import lombok.AccessLevel; @@ -139,7 +140,7 @@ private static void processSubCategory(Object instance, Configuration config, Fi subCategoryField.getType(), subCategoryField.getAnnotation(Config.LangKey.class), null, - subCat.getName()); + subCat); subCat.setComment(comment); subCat.setLanguageKey(langKey); @@ -206,7 +207,7 @@ private static void processConfigInternal(Class configClass, String category, configClass, field.getAnnotation(Config.LangKey.class), ConfigFieldParser.getFieldName(field), - category); + cat); ConfigFieldParser.loadField(instance, field, rawConfig, category, langKey); if (!requiresMcRestart) { @@ -223,7 +224,7 @@ private static void processConfigInternal(Class configClass, String category, } if (category.isEmpty()) return; - val langKey = getLangKey(configClass, configClass.getAnnotation(Config.LangKey.class), null, cat.getName()); + val langKey = getLangKey(configClass, configClass.getAnnotation(Config.LangKey.class), null, cat); cat.setLanguageKey(langKey); cat.setRequiresMcRestart(requiresMcRestart); cat.setRequiresWorldRestart(requiresWorldRestart); @@ -345,11 +346,11 @@ private static IConfigElementProxy getProxyElement(IConfigElement element, } private static String getLangKey(Class configClass, @Nullable Config.LangKey langKey, @Nullable String fieldName, - String categoryName) throws ConfigException { + @NotNull ConfigCategory category) throws ConfigException { if (langKey != null) return langKey.value(); Config.LangKeyPattern pattern = getClassOrBaseAnnotation(configClass, Config.LangKeyPattern.class); - String name = Optional.ofNullable(fieldName).orElse(categoryName); + String name = Optional.ofNullable(fieldName).orElse(category.getName()); if (pattern == null) return name; String patternStr = pattern.pattern(); @@ -361,6 +362,7 @@ private static String getLangKey(Class configClass, @Nullable Config.LangKey // Config annotation can't be null at this point assert cfg != null; + String categoryName = pattern.fullyQualified() ? category.getQualifiedName() : category.getName(); return buildKeyFromPattern(patternStr, cfg.modid(), cfg.filename(), categoryName, name); }