Skip to content

Commit

Permalink
make langkey pattern able to use qualified category names (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Sep 13, 2024
1 parent 6a39dbe commit 460a281
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
@interface LangKeyPattern {

String pattern() default "%mod.%cat.%field";

/**
* Whether subcategories should use their fully qualified name.<br>
* Fully qualified: {@code category.category1.category2} <br>
* Normal: {@code category2}
*/
boolean fullyQualified() default false;
}

@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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();

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

Expand Down

0 comments on commit 460a281

Please sign in to comment.