Skip to content

Commit ed5557a

Browse files
committed
enums in configs
1 parent c9a1ed7 commit ed5557a

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

src/main/java/com/falsepattern/lib/config/Config.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@
8888
String value();
8989
}
9090

91+
@Retention(RetentionPolicy.RUNTIME)
92+
@Target(ElementType.FIELD)
93+
@interface DefaultEnum {
94+
String value();
95+
}
96+
9197
@Retention(RetentionPolicy.RUNTIME)
9298
@Target(ElementType.FIELD)
9399
@interface Name {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.falsepattern.lib.config;
2+
3+
import com.falsepattern.lib.StableAPI;
4+
5+
/**
6+
* A really basic wrapper for config to simplify handling them in external code.
7+
*/
8+
@StableAPI(since = "0.6.0")
9+
public class ConfigException extends Exception {
10+
11+
public ConfigException(String message) {
12+
super(message);
13+
}
14+
15+
public ConfigException(Throwable cause) {
16+
super(cause);
17+
}
18+
}

src/main/java/com/falsepattern/lib/config/ConfigurationManager.java

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import com.falsepattern.lib.internal.CoreLoadingPlugin;
55
import cpw.mods.fml.client.event.ConfigChangedEvent;
66
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
7-
import lombok.AccessLevel;
8-
import lombok.NoArgsConstructor;
9-
import lombok.val;
10-
import lombok.var;
7+
import lombok.*;
118
import net.minecraftforge.common.MinecraftForge;
129
import net.minecraftforge.common.config.Configuration;
1310

11+
import java.lang.reflect.Field;
12+
import java.lang.reflect.InvocationTargetException;
1413
import java.nio.file.Path;
1514
import java.util.*;
1615
import java.util.regex.Pattern;
16+
import java.util.stream.Collectors;
1717

1818
/**
1919
* Class for controlling the loading of configuration files.
@@ -33,11 +33,15 @@ public class ConfigurationManager {
3333
* Registers a configuration class to be loaded.
3434
* @param config The class to register.
3535
*/
36-
public static void registerConfig(Class<?> config) throws IllegalAccessException {
37-
val cfg = Optional.ofNullable(config.getAnnotation(Config.class)).orElseThrow(() -> new IllegalArgumentException("Class " + config.getName() + " does not have a @Config annotation!"));
36+
public static void registerConfig(Class<?> config) throws ConfigException {
37+
val cfg = Optional.ofNullable(config.getAnnotation(Config.class)).orElseThrow(() -> new ConfigException("Class " + config.getName() + " does not have a @Config annotation!"));
3838
val cfgSet = configs.computeIfAbsent(cfg.modid(), (ignored) -> new HashSet<>());
3939
cfgSet.add(config);
40-
processConfig(config);
40+
try {
41+
processConfig(config);
42+
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException e) {
43+
throw new ConfigException(e);
44+
}
4145
}
4246

4347
/**
@@ -55,20 +59,24 @@ public static void init() {
5559
* @param event The event.
5660
*/
5761
@SubscribeEvent
62+
@SneakyThrows
5863
public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) {
5964
val configClasses = configs.get(event.modID);
6065
if (configClasses == null)
6166
return;
6267
configClasses.forEach((config) -> {
6368
try {
6469
processConfig(config);
65-
} catch (IllegalAccessException e) {
66-
throw new RuntimeException(e);
70+
} catch (
71+
@SuppressWarnings("CaughtExceptionImmediatelyRethrown")
72+
IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException |
73+
ConfigException e) {
74+
throw e;
6775
}
6876
});
6977
}
7078

71-
private static void processConfig(Class<?> configClass) throws IllegalAccessException {
79+
private static void processConfig(Class<?> configClass) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, ConfigException {
7280
val cfg = configClass.getAnnotation(Config.class);
7381
val category = cfg.category();
7482
var configName = cfg.name().trim();
@@ -104,6 +112,19 @@ private static void processConfig(Class<?> configClass) throws IllegalAccessExce
104112
val defaultValue = Optional.ofNullable(field.getAnnotation(Config.DefaultString.class)).map(Config.DefaultString::value).orElse((String)field.get(null));
105113
val pattern = Optional.ofNullable(field.getAnnotation(Config.Pattern.class)).map(Config.Pattern::value).map(Pattern::compile).orElse(null);
106114
field.set(null, rawConfig.getString(name, category, defaultValue, comment, langKey, pattern));
115+
} else if (field.isEnumConstant()) {
116+
val enumClass = field.getType();
117+
val enumValues = Arrays.stream((Object[])enumClass.getDeclaredMethod("values").invoke(null)).map((obj) -> (Enum<?>)obj).collect(Collectors.toList());
118+
val defaultValue = (Enum<?>)
119+
Optional.ofNullable(field.getAnnotation(Config.DefaultEnum.class))
120+
.map(Config.DefaultEnum::value)
121+
.map((defName) -> extractField(enumClass, defName))
122+
.map(ConfigurationManager::extractValue)
123+
.orElse(field.get(null));
124+
val possibleValues = enumValues.stream().map(Enum::name).toArray(String[]::new);
125+
field.set(null, enumClass.getDeclaredField(rawConfig.getString(name, category, defaultValue.name(), comment, possibleValues, langKey)));
126+
} else {
127+
throw new ConfigException("Illegal config field: " + field.getName() + " in " + configClass.getName() + ": Unsupported type " + field.getType().getName() + "! Did you forget an @Ignore annotation?");
107128
}
108129
if (field.isAnnotationPresent(Config.RequiresMcRestart.class)) {
109130
cat.setRequiresMcRestart(true);
@@ -114,4 +135,14 @@ private static void processConfig(Class<?> configClass) throws IllegalAccessExce
114135
}
115136
rawConfig.save();
116137
}
138+
139+
@SneakyThrows
140+
private static Field extractField(Class<?> clazz, String field) {
141+
return clazz.getDeclaredField(field);
142+
}
143+
144+
@SneakyThrows
145+
private static Object extractValue(Field field) {
146+
return field.get(null);
147+
}
117148
}

0 commit comments

Comments
 (0)