Skip to content

Using as a Soft Dependency

Endor H edited this page Dec 11, 2022 · 3 revisions

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

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.

Access the builder API with dependency injection

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.:

  • SimpleConfigBuilder for the root config class
  • ConfigCategoryBuilder for categories
  • ConfigGroupBuilder for 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.

Limitations

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:

  • ExtendedKeyBind and the entire extended key bind system (you should use regular KeyBindings instead)
  • Range (used by range entries, you can use pair entries instead)
  • Icon and its subclasses (in case you wanted to use it for your own GUIs)
  • TextFormatter and its subclasses (in case you wanted to use it for your own GUIs)
  • Rectangle and any other classes in the endorh.simpleconfig.api.ui.math package

If you want to use any of this features, you should consider adding Simple Config as a full dependency.

Clone this wiki locally