Skip to content

Commit de75812

Browse files
committed
better enum validation
1 parent fe49dd1 commit de75812

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.falsepattern.lib.StableAPI;
44
import com.falsepattern.lib.internal.CoreLoadingPlugin;
5+
import com.falsepattern.lib.internal.FalsePatternLib;
56
import cpw.mods.fml.client.event.ConfigChangedEvent;
67
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
78
import lombok.*;
@@ -118,7 +119,21 @@ private static void processConfig(Class<?> configClass) throws IllegalAccessExce
118119
.map(ConfigurationManager::extractValue)
119120
.orElse(field.get(null));
120121
val possibleValues = enumValues.stream().map(Enum::name).toArray(String[]::new);
121-
field.set(null, fieldClass.getDeclaredField(rawConfig.getString(name, category, defaultValue.name(), comment, possibleValues, langKey)).get(null));
122+
var value = rawConfig.getString(name, category, defaultValue.name(), comment + "\nPossible values: " + Arrays.toString(possibleValues) + "\n", possibleValues, langKey);
123+
124+
try {
125+
if (!Arrays.asList(possibleValues).contains(value)) {
126+
throw new NoSuchFieldException();
127+
}
128+
val enumField = fieldClass.getDeclaredField(value);
129+
if (!enumField.isEnumConstant()) {
130+
throw new NoSuchFieldException();
131+
}
132+
field.set(null, enumField.get(null));
133+
} catch (NoSuchFieldException e) {
134+
FalsePatternLib.getLog().warn("Invalid value " + value + " for enum configuration field " + field.getName() + " of type " + fieldClass.getName() + " in config class " + configClass.getName() + "! Using default value of " + defaultValue + "!");
135+
field.set(null, defaultValue);
136+
}
122137
} else {
123138
throw new ConfigException("Illegal config field: " + field.getName() + " in " + configClass.getName() + ": Unsupported type " + fieldClass.getName() + "! Did you forget an @Ignore annotation?");
124139
}

0 commit comments

Comments
 (0)