-
Notifications
You must be signed in to change notification settings - Fork 1
Using as a Soft Dependency
Dependency injection is currently not supported by the Kotlin API
Simple Config supports dependency injection to declare config files without relying on the Simple Config mod to be present at runtime.
By doing this, your players won't need to install Simple Config to use your mod, although they'll be restricted to your default values of all your config files.
- Declaring a config file with
@ConfigClass - Access the builder API with dependency injection
- Limitations
The easiest way to create a config file without depending on Simple Config is by using the declarative Annotation API.
To declare a config file, create a class for your config fields and annotate it with @ConfigClass. If you only include a single
mod in your jar file, you don't need to specify the mod ID in the @ConfigClass annotation. However, it's recommended you still do so.
The @ConfigClass annotation can also specify a config type. In addition, you may configure the default background texture for your config file and the color of the default category of your file.
@ConfigClass(modId=ExampleMod.MOD_ID, type=SimpleConfig.Type.CLIENT)
public class ClientConfig {
@Entry public static String str = "";
}Simple Config will search for all classes annotated with @ConfigClass when loading, and scan them to create config files with the Annotation API.
If you only access your config files through the fields of your config class, your mod will be able to avoid loading any of Simple Config's classes.
If Simple Config is not present at runtime, you'll still have access to the default values of the fields in your config class, and your mod will be able to run.
It's also possible to access the builder API without explicitly loading Simple Config classes. To do so, create a method build within your config class, which receives and returns the corresponding builder type, i.e.:
-
SimpleConfigBuilderfor the root config class -
ConfigCategoryBuilderfor categories -
ConfigGroupBuilderfor groups
@ConfigClass(modId=ExampleMod.MOD_ID, type=SimpleConfig.Type.CLIENT)
public class ClientConfig {
// The @Bind annotation is optional but can help prevent typos
@Bind public static SimpleConfigBuilder build(SimpleConfigBuilder builder) {
// Do not call buildAndRegister here, only add/configure entries/categories/groups
return builder
.add("str", string(""));
}
// We need to initialize the backing fields
@Bind public static String str = "";
}This method will be called to finish building the corresponding config component, before the rest of the class is scanned for annotations. By accessing the builder API indirectly like this, you avoid loading Simple Config's classes explicitly and your mod will be able to run without Simple Config installed.
Keep in mind however, that you'll need to properly initialize the baking fields of any entries declared with the builder API, since they won't be written if Simple Config is not present at runtime.
To be able to run without Simple Config installed, you can't use any of Simple Config's classes in your code.
In particular you won't be able to use the following classes:
-
ExtendedKeyBindand the entire extended key bind system (you should use regularKeyBindings instead) -
Range(used byrangeentries, you can usepairentries instead) -
Iconand its subclasses (in case you wanted to use it for your own GUIs) -
TextFormatterand its subclasses (in case you wanted to use it for your own GUIs) -
Rectangleand any other classes in theendorh.simpleconfig.api.ui.mathpackage
If you want to use any of this features, you should consider adding Simple Config as a full dependency.
If you have any doubts, feel free to drop by the official Discord Server.