Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a few config issues #73

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
* {@code %field} - field name <b>(required)</b> <br>
* </p>
* Default pattern: {@code %mod.%cat.%field}. Categories use the pattern without {@code %field}. Can be overridden
* for fields with {@link Config.LangKey}.
* for fields with {@link Config.LangKey}. <br>
* The generated keys can be printed to log by setting the {@code -Dgtnhlib.printkeys=true} JVM flag.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
Expand All @@ -66,7 +67,7 @@
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Target({ ElementType.FIELD, ElementType.TYPE })
@interface Comment {

String[] value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public void load(@Nullable Object instance, @Nullable String defValueString, Fie
stringValues[i] = Double.toString(defaultValue[i]);
}
comment = comment + " [default: " + Arrays.toString(stringValues) + "]";
double[] value = config.get(category, name, defaultValue, comment).getDoubleList();
double[] value = config.get(category, name, defaultValue, comment).setLanguageKey(langKey).getDoubleList();

field.set(instance, value);
}
Expand Down Expand Up @@ -484,7 +484,7 @@ public void load(@Nullable Object instance, @Nullable String defValueString, Fie
stringValues[i] = Integer.toString(defaultValue[i]);
}
comment = comment + " [default: " + Arrays.toString(stringValues) + "]";
int[] value = config.get(category, name, defaultValue, comment).getIntList();
int[] value = config.get(category, name, defaultValue, comment).setLanguageKey(langKey).getIntList();

field.set(instance, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ConfigurationManager {
private static final Map<String, Configuration> configs = new HashMap<>();
private static final Map<Configuration, Map<String, Set<Class<?>>>> configToCategoryClassMap = new HashMap<>();
private static final String[] langKeyPlaceholders = new String[] { "%mod", "%file", "%cat", "%field" };
private static final Boolean PRINT_KEYS = Boolean.getBoolean("gtnhlib.printkeys");

private static final ConfigurationManager instance = new ConfigurationManager();

Expand All @@ -58,7 +59,7 @@ public static void registerConfig(Class<?> configClass) throws ConfigException {
init();
val cfg = Optional.ofNullable(configClass.getAnnotation(Config.class)).orElseThrow(
() -> new ConfigException("Class " + configClass.getName() + " does not have a @Config annotation!"));
val category = cfg.category().trim();
val category = cfg.category().trim().toLowerCase();
val modid = cfg.modid();
val filename = Optional.of(cfg.filename().trim()).filter(s -> !s.isEmpty()).orElse(modid);

Expand Down Expand Up @@ -92,7 +93,7 @@ public static void save(Class<?>... configClasses) {
try {
val cfg = Optional.ofNullable(clazz.getAnnotation(Config.class)).orElseThrow(
() -> new ConfigException("Class " + clazz.getName() + " does not have a @Config annotation!"));
val category = cfg.category().trim();
val category = cfg.category().trim().toLowerCase();
Configuration rawConfig = configs.get(getConfigKey(cfg));
save(clazz, null, rawConfig, category);
savedConfigs.add(rawConfig);
Expand Down Expand Up @@ -131,19 +132,15 @@ private static void save(Class<?> configClass, Object instance, Configuration ra

private static void processSubCategory(Object instance, Configuration config, Field subCategoryField,
String category) throws ConfigException {
var comment = Optional.ofNullable(subCategoryField.getAnnotation(Config.Comment.class))
.map(Config.Comment::value).map((lines) -> String.join("\n", lines)).orElse("");
val name = ConfigFieldParser.getFieldName(subCategoryField);
val cat = (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + name.toLowerCase();
ConfigCategory subCat = config.getCategory(cat);
val langKey = getLangKey(
subCategoryField.getType(),
subCategoryField.getAnnotation(Config.LangKey.class),
null,
subCat);

subCat.setComment(comment);
subCat.setLanguageKey(langKey);

Optional.ofNullable(subCategoryField.getAnnotation(Config.Comment.class)).map(Config.Comment::value)
.map((lines) -> String.join("\n", lines)).ifPresent(subCat::setComment);
Optional.ofNullable(subCategoryField.getAnnotation(Config.LangKey.class)).map(Config.LangKey::value)
.ifPresent(subCat::setLanguageKey);

if (subCategoryField.isAnnotationPresent(Config.RequiresMcRestart.class)) {
subCat.setRequiresMcRestart(true);
}
Expand Down Expand Up @@ -224,10 +221,15 @@ private static void processConfigInternal(Class<?> configClass, String category,
}

if (category.isEmpty()) return;
val langKey = getLangKey(configClass, configClass.getAnnotation(Config.LangKey.class), null, cat);
cat.setLanguageKey(langKey);
if (cat.getLanguagekey().equals(category)) {
val langKey = getLangKey(configClass, configClass.getAnnotation(Config.LangKey.class), null, cat);
cat.setLanguageKey(langKey);
}
cat.setRequiresMcRestart(requiresMcRestart);
cat.setRequiresWorldRestart(requiresWorldRestart);

Optional.ofNullable(configClass.getAnnotation(Config.Comment.class)).map(Config.Comment::value)
.map((lines) -> String.join("\n", lines)).ifPresent(cat::setComment);
}

/**
Expand Down Expand Up @@ -257,7 +259,9 @@ public static List<IConfigElement> getConfigElements(Class<?> configClass, boole
() -> new ConfigException("Class " + configClass.getName() + " does not have a @Config annotation!"));
val rawConfig = Optional.ofNullable(configs.get(getConfigKey(cfg))).map(
(conf) -> Optional.ofNullable(configToCategoryClassMap.get(conf))
.map((map) -> map.get(cfg.category().trim()).contains(configClass)).orElse(false) ? conf : null)
.map((map) -> map.get(cfg.category().trim().toLowerCase()).contains(configClass)).orElse(false)
? conf
: null)
.orElseThrow(
() -> new ConfigException("Tried to get config elements for non-registered config class!"));

Expand Down Expand Up @@ -350,7 +354,7 @@ private static String getLangKey(Class<?> configClass, @Nullable Config.LangKey
if (langKey != null) return langKey.value();

Config.LangKeyPattern pattern = getClassOrBaseAnnotation(configClass, Config.LangKeyPattern.class);
String name = Optional.ofNullable(fieldName).orElse(category.getName());
String name = Optional.ofNullable(fieldName).orElse(category.getQualifiedName());
if (pattern == null) return name;
String patternStr = pattern.pattern();

Expand All @@ -363,7 +367,17 @@ private static String getLangKey(Class<?> configClass, @Nullable Config.LangKey
assert cfg != null;

String categoryName = pattern.fullyQualified() ? category.getQualifiedName() : category.getName();
return buildKeyFromPattern(patternStr, cfg.modid(), cfg.filename(), categoryName, name);
if (fieldName == null) name = categoryName;
String key = buildKeyFromPattern(patternStr, cfg.modid(), cfg.filename(), categoryName, name);

if (PRINT_KEYS) {
if (fieldName != null) {
LOGGER.info("Lang key for field {} in category {}: {}", fieldName, category.getName(), key);
} else {
LOGGER.info("Lang key for category {}: {}", category.getName(), key);
}
}
return key;
}

private static String buildKeyFromPattern(String pattern, String modId, String fileName, String categoryName,
Expand Down Expand Up @@ -393,13 +407,13 @@ private static String buildKeyFromPattern(String pattern, String modId, String f
private static @Nullable <A extends Annotation> A getClassOrBaseAnnotation(Class<?> clazz,
Class<A> annotationClass) {
A annotation = clazz.getAnnotation(annotationClass);
if (annotation != null || !clazz.isMemberClass()) return annotation;

while (clazz.isMemberClass()) {
while (annotation == null && clazz.isMemberClass()) {
clazz = clazz.getDeclaringClass();
annotation = clazz.getAnnotation(annotationClass);
}

return clazz.getAnnotation(annotationClass);
return annotation;
}

private static boolean isFieldSubCategory(@Nullable Field field) {
Expand Down