2020import java .util .concurrent .atomic .AtomicReference ;
2121import org .spongepowered .configurate .CommentedConfigurationNode ;
2222import org .spongepowered .configurate .ConfigurateException ;
23- import org .spongepowered .configurate .interfaces . InterfaceDefaultOptions ;
23+ import org .spongepowered .configurate .objectmapping . ObjectMapper ;
2424import org .spongepowered .configurate .objectmapping .meta .Processor ;
2525import org .spongepowered .configurate .yaml .NodeStyle ;
2626import org .spongepowered .configurate .yaml .YamlConfigurationLoader ;
3333 * @param <T> the type of the configuration object
3434 */
3535public record ConfigurationManager <T >(
36- Class <T > clazz ,
36+ Class <T > configClass ,
3737 YamlConfigurationLoader loader ,
3838 AtomicReference <T > config
3939) {
@@ -50,52 +50,59 @@ public record ConfigurationManager<T>(
5050 /**
5151 * Loads the configuration from the path and
5252 * creates a new {@link ConfigurationManager} instance.
53- * If the configuration file does not exist, it will create a new one with default values.
5453 *
55- * @param path the path to the directory containing the configuration file
56- * @param clazz the class type of the configuration object
57- * @param <T> the type of the configuration object
54+ * <p>If the configuration file does not exist, it'll create a new one with default values.</p>
55+ *
56+ * @param path the path to the directory containing the configuration file
57+ * @param configClass the class type of the configuration object
58+ * @param <T> the type of the configuration object
5859 * @return a {@link ConfigurationManager} instance containing the loaded configuration
5960 * @throws IOException if an error occurs while loading the configuration
6061 */
6162 public static <T > ConfigurationManager <T > load (
6263 Path path ,
63- final Class <T > clazz ,
64- final PlatformType platformType
64+ Class <T > configClass ,
65+ PlatformType platformType
6566 ) throws IOException {
6667 path = path .resolve ("config.yml" );
6768
68- final YamlConfigurationLoader loader = YamlConfigurationLoader .builder ()
69+ ObjectMapper .Factory mapperFactory = ObjectMapper .factoryBuilder ()
70+ .addProcessor (ExcludePlatform .class , excludePlatform (platformType ))
71+ .build ();
72+
73+ YamlConfigurationLoader loader = YamlConfigurationLoader .builder ()
6974 .path (path )
7075 .indent (2 )
7176 .nodeStyle (NodeStyle .BLOCK )
72- .defaultOptions (opts -> InterfaceDefaultOptions .addTo (opts ,
73- builder -> builder .addProcessor (ExcludePlatform .class ,
74- excludePlatform (platformType )))
75- .header (HEADER ))
77+ .defaultOptions (options -> options
78+ .header (HEADER )
79+ .serializers (builder ->
80+ builder .registerAnnotatedObjects (mapperFactory ))
81+ )
7682 .build ();
7783
78- final CommentedConfigurationNode root = loader .load ();
79- final T config = root .get (clazz );
84+ CommentedConfigurationNode root = loader .load ();
85+ T config = root .get (configClass );
8086
8187 if (Files .notExists (path )) {
8288 loader .save (root );
8389 }
8490
85- return new ConfigurationManager <>(clazz , loader , new AtomicReference <>(config ));
91+ return new ConfigurationManager <>(configClass , loader , new AtomicReference <>(config ));
8692 }
8793
8894 /**
8995 * Asynchronously reloads the configuration from disk.
90- * The current configuration object is updated with the newly loaded data.
96+ *
97+ * <p>The current configuration object is updated with the newly loaded data.</p>
9198 *
9299 * @return a {@link CompletableFuture} that completes when the reload is complete
93100 */
94101 public CompletableFuture <Void > reload () {
95102 return CompletableFuture .runAsync (() -> {
96103 try {
97- final CommentedConfigurationNode root = loader .load ();
98- config .set (root .get (clazz ));
104+ CommentedConfigurationNode root = loader .load ();
105+ config .set (root .get (configClass ));
99106 } catch (ConfigurateException e ) {
100107 throw new CompletionException ("Failed to load configuration" , e );
101108 }
@@ -112,7 +119,7 @@ public T get() {
112119 }
113120
114121 private static Processor .Factory <ExcludePlatform , Object > excludePlatform (
115- final PlatformType platformType
122+ PlatformType platformType
116123 ) {
117124 return (annotation , fieldType ) -> (value , destination ) -> {
118125 for (PlatformType platform : annotation .value ()) {
0 commit comments