@@ -18,62 +18,101 @@ public enum FileType{
1818 JSON , YAML
1919 }
2020
21- public static <T > T loadConfig (JavaPlugin plugin , Class <T > configClass , String fileName , ConfigManager .FileType fileType ) throws NoSuchMethodException , InvocationTargetException , InstantiationException , IllegalAccessException {
21+ /**
22+ * @param plugin An instance of your plugin
23+ * @param configClass A class reference to a Java class annotated with @Config containing your config values
24+ * @param <T> The generic type of the Config class
25+ * @return A new instance of the Config class to be used throughout your plugin
26+ */
27+ public static <T > T loadConfig (JavaPlugin plugin , Class <T > configClass ) {
2228
2329 T config = null ;
24- File messagesConfigFile = null ;
25- ObjectMapper mapper = null ;
26-
27- String fileNameA = configClass .getAnnotation (Config .class ).fileName ();
28- System .out .println ("Grabbed filename: " + fileNameA );
29-
30- if (fileType == ConfigManager .FileType .YAML ){
31- messagesConfigFile = new File (plugin .getDataFolder (), fileName + ".yml" );
32- mapper = new ObjectMapper (new YAMLFactory ()).configure (JsonGenerator .Feature .IGNORE_UNKNOWN , true ).configure (JsonParser .Feature .IGNORE_UNDEFINED , true ).configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
33- }else if (fileType == ConfigManager .FileType .JSON ){
34- messagesConfigFile = new File (plugin .getDataFolder (), fileName + ".json" );
35- mapper = new ObjectMapper (new JsonFactory ()).configure (JsonGenerator .Feature .IGNORE_UNKNOWN , true ).configure (JsonParser .Feature .IGNORE_UNDEFINED , true ).configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
36- }
3730
38- if (!messagesConfigFile .exists ()){
39- config = configClass .getConstructor ().newInstance ();
31+ Config configAnnotation = configClass .getAnnotation (Config .class );
4032
41- try {
42- mapper .writeValue (messagesConfigFile , config );
43- } catch (IOException e ) {
44- e .printStackTrace ();
33+ if (configAnnotation == null ) {
34+ plugin .getLogger ().severe ("The provided Configuration Java class was not annotated properly with @Config from SimpAPI. Therefore the config could not be loaded." );
35+ plugin .getPluginLoader ().disablePlugin (plugin );
36+ }else {
37+
38+ String fileName = configAnnotation .fileName ();
39+ FileType fileType = configAnnotation .fileType ();
40+
41+ File messagesConfigFile = getConfigFile (plugin , fileName , fileType );
42+ ObjectMapper mapper = getObjectMapper (fileType );
43+
44+ if (!messagesConfigFile .exists ()){
45+ try {
46+ config = configClass .getConstructor ().newInstance ();
47+ } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) {
48+ e .printStackTrace ();
49+ }
50+
51+ try {
52+ mapper .writeValue (messagesConfigFile , config );
53+ } catch (IOException e ) {
54+ e .printStackTrace ();
55+ }
56+ }else {
57+ //since it exists already, load the values into the object
58+ try {
59+ T t = mapper .readValue (messagesConfigFile , configClass );
60+ saveConfig (plugin , t );
61+ return t ;
62+ } catch (IOException e ) {
63+ e .printStackTrace ();
64+ }
4565 }
66+ }
67+
68+
69+ return config ;
70+ }
71+
72+ /**
73+ * @param plugin Your plugin class
74+ * @param configObject An instance of your Config class to use to save the contents of it to file
75+ */
76+ public static void saveConfig (JavaPlugin plugin , Object configObject ) {
77+
78+ Config configAnnotation = configObject .getClass ().getAnnotation (Config .class );
79+ if (configAnnotation == null ){
80+ plugin .getLogger ().severe ("The provided Configuration Java class was not annotated properly with @Config from SimpAPI. Therefore the config could not be saved." );
81+ plugin .getPluginLoader ().disablePlugin (plugin );
4682 }else {
47- //since it exists already, load the values into the object
83+
84+ String fileName = configAnnotation .fileName ();
85+ FileType fileType = configAnnotation .fileType ();
86+
87+ File messagesConfigFile = getConfigFile (plugin , fileName , fileType );
88+ ObjectMapper mapper = getObjectMapper (fileType );
89+
4890 try {
49- T t = mapper .readValue (messagesConfigFile , configClass );
50- saveConfig (plugin , t , fileName , ConfigManager .FileType .YAML );
51- return t ;
91+ mapper .writeValue (messagesConfigFile , configObject );
5292 } catch (IOException e ) {
5393 e .printStackTrace ();
5494 }
5595 }
56- return config ;
57- }
5896
59- public static void saveConfig ( JavaPlugin plugin , Object configObject , String fileName , ConfigManager . FileType fileType ) {
97+ }
6098
61- File messagesConfigFile = null ;
62- ObjectMapper mapper = null ;
63- if ( fileType == ConfigManager . FileType . YAML ){
64- messagesConfigFile = new File (plugin .getDataFolder (), fileName + ".yml" );
65- mapper = new ObjectMapper ( new YAMLFactory ()). configure ( JsonGenerator . Feature . IGNORE_UNKNOWN , true ). configure ( JsonParser . Feature . IGNORE_UNDEFINED , true ). configure ( DeserializationFeature . FAIL_ON_UNKNOWN_PROPERTIES , false );
66- } else if ( fileType == ConfigManager . FileType . JSON ){
67- messagesConfigFile = new File ( plugin . getDataFolder (), fileName + ".json" );
68- mapper = new ObjectMapper ( new JsonFactory ()). configure ( JsonGenerator . Feature . IGNORE_UNKNOWN , true ). configure ( JsonParser . Feature . IGNORE_UNDEFINED , true ). configure ( DeserializationFeature . FAIL_ON_UNKNOWN_PROPERTIES , false ) ;
99+ private static File getConfigFile ( JavaPlugin plugin , String fileName , FileType fileType ){
100+ switch ( fileType ) {
101+ case YAML :
102+ return new File (plugin .getDataFolder (), fileName + ".yml" );
103+ case JSON :
104+ return new File ( plugin . getDataFolder (), fileName + ".json" );
105+ default :
106+ return null ;
69107 }
108+ }
70109
71- try {
72- mapper .writeValue (messagesConfigFile , configObject );
73- } catch (IOException e ) {
74- e .printStackTrace ();
110+ private static ObjectMapper getObjectMapper (FileType fileType ) {
111+ if (fileType == FileType .YAML ){
112+ return new ObjectMapper (new YAMLFactory ()).configure (JsonGenerator .Feature .IGNORE_UNKNOWN , true ).configure (JsonParser .Feature .IGNORE_UNDEFINED , true ).configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
113+ }else {
114+ return new ObjectMapper (new JsonFactory ()).configure (JsonGenerator .Feature .IGNORE_UNKNOWN , true ).configure (JsonParser .Feature .IGNORE_UNDEFINED , true ).configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
75115 }
76-
77116 }
78117
79118}
0 commit comments