44import com .falsepattern .lib .internal .CoreLoadingPlugin ;
55import cpw .mods .fml .client .event .ConfigChangedEvent ;
66import 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 .*;
118import net .minecraftforge .common .MinecraftForge ;
129import net .minecraftforge .common .config .Configuration ;
1310
11+ import java .lang .reflect .Field ;
12+ import java .lang .reflect .InvocationTargetException ;
1413import java .nio .file .Path ;
1514import java .util .*;
1615import 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