Skip to content

Writing an Extension

Entrypoint

The entrypoint of the Extension API is the so called Extension interface, which your extensions "entry class" must implement:

public class MyExtension implements Extension<?> {}

Extension.Data

Furthermore, each entry class must override the Extension#init(T) method, which will be called when JDA-Commands loads the extension. It can be used to configure extension specific options with help of an own implementation of Extension.Data.

public class MyExtension implements Extension<Void> {

    @Override
    public void init(Void data) {
        // doing nothing if not needed
    }
}

public class MyExtension implements Extension<MyExtensionData> {

    @Override
    public void init(MyExtensionData data) { //(1)!
        if (data != null) {
            doSomeConfig(data.someOption());
        }
    }

    @Override
    public @NotNull Class<MyExtensionData> dataType() {
        return MyExtensionData.class;
    }
}

public record MyExtensionData(String someOption) implements Extension.Data {}
  1. If no instance of MyExtensionData is passed by the user, this argument will be set null.

Providing Implementations

Currently, extensions support providing custom PropertyProviders of properties with category Property.Source#EXTENSION. You can take a look at JDACProperty to know what properties can be provided by extensions.

To provide custom JDACPropertyProviders your have to implement the Extension#properties() method. This method returns a collection of all JDACPropertyProviders that an extension provides. Take a look here to know how to use them.

public class MyExtension implements Extension<MyExtensionData> {

    @Override
    public void init(MyExtensionData data) {
        if (data != null) {
            doSomeConfig(data.someOption());
        }
    }

    @Override
    public @NotNull Collection<JDACPropertyProvider<?>> properties() {
        return List.of(new JDACPropertyProvider(
                Property.CLASS_FINDER,
                Priority.of(200), // pick an appropriated priority
                Foo.class,
                ctx -> List.of(new CustomClassFinder(ctx.get(JDACProperty.PACKAGES)))
        ));
    }

    @Override
    public @NotNull Class<MyExtensionData> dataType() {
        return MyExtensionData.class;
    }
}

public record MyExtensionData(String someOption) implements Extension.Data {}

Registration

Custom extensions are found with help of Javas ServiceLoader API.

To register the above MyExtension we have to create a file in our resources\META-INF directory called io.github.kaktushose.jdac.property.extension.Extension.

src
└── main
    └── resources
        └── META-INF
            └── io.github.kaktushose.jdac.property.extension.Extension

The full class name of our class MyExtension (e.g. my.package.MyExtension) must be the content of this file.

Example

io.github.kaktushose.jdac.property.extension.Extension
my.package.MyExtension

The extension can now be found and loaded by JDA-Commands.