4
4
import com .falsepattern .lib .internal .CoreLoadingPlugin ;
5
5
import cpw .mods .fml .client .event .ConfigChangedEvent ;
6
6
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 .*;
11
8
import net .minecraftforge .common .MinecraftForge ;
12
9
import net .minecraftforge .common .config .Configuration ;
13
10
11
+ import java .lang .reflect .Field ;
12
+ import java .lang .reflect .InvocationTargetException ;
14
13
import java .nio .file .Path ;
15
14
import java .util .*;
16
15
import java .util .regex .Pattern ;
16
+ import java .util .stream .Collectors ;
17
17
18
18
/**
19
19
* Class for controlling the loading of configuration files.
@@ -33,11 +33,15 @@ public class ConfigurationManager {
33
33
* Registers a configuration class to be loaded.
34
34
* @param config The class to register.
35
35
*/
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!" ));
38
38
val cfgSet = configs .computeIfAbsent (cfg .modid (), (ignored ) -> new HashSet <>());
39
39
cfgSet .add (config );
40
- processConfig (config );
40
+ try {
41
+ processConfig (config );
42
+ } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException e ) {
43
+ throw new ConfigException (e );
44
+ }
41
45
}
42
46
43
47
/**
@@ -55,20 +59,24 @@ public static void init() {
55
59
* @param event The event.
56
60
*/
57
61
@ SubscribeEvent
62
+ @ SneakyThrows
58
63
public void onConfigChanged (ConfigChangedEvent .OnConfigChangedEvent event ) {
59
64
val configClasses = configs .get (event .modID );
60
65
if (configClasses == null )
61
66
return ;
62
67
configClasses .forEach ((config ) -> {
63
68
try {
64
69
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 ;
67
75
}
68
76
});
69
77
}
70
78
71
- private static void processConfig (Class <?> configClass ) throws IllegalAccessException {
79
+ private static void processConfig (Class <?> configClass ) throws IllegalAccessException , NoSuchMethodException , InvocationTargetException , NoSuchFieldException , ConfigException {
72
80
val cfg = configClass .getAnnotation (Config .class );
73
81
val category = cfg .category ();
74
82
var configName = cfg .name ().trim ();
@@ -104,6 +112,19 @@ private static void processConfig(Class<?> configClass) throws IllegalAccessExce
104
112
val defaultValue = Optional .ofNullable (field .getAnnotation (Config .DefaultString .class )).map (Config .DefaultString ::value ).orElse ((String )field .get (null ));
105
113
val pattern = Optional .ofNullable (field .getAnnotation (Config .Pattern .class )).map (Config .Pattern ::value ).map (Pattern ::compile ).orElse (null );
106
114
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?" );
107
128
}
108
129
if (field .isAnnotationPresent (Config .RequiresMcRestart .class )) {
109
130
cat .setRequiresMcRestart (true );
@@ -114,4 +135,14 @@ private static void processConfig(Class<?> configClass) throws IllegalAccessExce
114
135
}
115
136
rawConfig .save ();
116
137
}
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
+ }
117
148
}
0 commit comments