diff --git a/config/src/main/groovy/net/neoforged/camelot/config/CamelotConfig.groovy b/config/src/main/groovy/net/neoforged/camelot/config/CamelotConfig.groovy index 122228f..dd37072 100644 --- a/config/src/main/groovy/net/neoforged/camelot/config/CamelotConfig.groovy +++ b/config/src/main/groovy/net/neoforged/camelot/config/CamelotConfig.groovy @@ -3,6 +3,7 @@ package net.neoforged.camelot.config import groovy.transform.CompileStatic import groovy.transform.stc.ClosureParams import groovy.transform.stc.FromString +import groovy.transform.stc.SimpleType import net.neoforged.camelot.config.module.ModuleConfiguration import java.nio.file.Files @@ -45,6 +46,15 @@ class CamelotConfig { ConfigUtils.configure(module(type), configurator) } + /** + * Configure a module. + * @param id the ID of the module + * @param configurator the closure that configures the module + */ + void module(String id, @DelegatesTo(value = ModuleConfiguration, strategy = Closure.DELEGATE_FIRST) @ClosureParams(value = SimpleType, options = 'net.neoforged.camelot.config.module.ModuleConfiguration') Closure configurator) { + ConfigUtils.configure(module(id), configurator) + } + /** * Get the module of the given type. * @param type the type of the module @@ -58,6 +68,19 @@ class CamelotConfig { return (T)conf } + /** + * Get the module with the given ID. + * @param id the ID of the module + * @return the module configuration + */ + ModuleConfiguration module(String id) { + final conf = modules.values().find { it.moduleId == id } + if (conf === null) { + throw new IllegalArgumentException("Unknown module with ID $id") + } + return conf + } + void validate() { if (!token) { throw new IllegalArgumentException('Bot API Token must be provided!') diff --git a/config/src/main/groovy/net/neoforged/camelot/config/module/ModuleConfiguration.groovy b/config/src/main/groovy/net/neoforged/camelot/config/module/ModuleConfiguration.groovy index e29045a..a76786d 100644 --- a/config/src/main/groovy/net/neoforged/camelot/config/module/ModuleConfiguration.groovy +++ b/config/src/main/groovy/net/neoforged/camelot/config/module/ModuleConfiguration.groovy @@ -8,11 +8,20 @@ import groovy.transform.CompileStatic */ @CompileStatic class ModuleConfiguration { + private String moduleId + /** * Whether this module should be enabled. */ boolean enabled = true + /** + * The ID of the module this configuration is for. + */ + String getModuleId() { + return moduleId + } + @CompileDynamic void validate() { properties.forEach { key, it -> @@ -26,6 +35,13 @@ class ModuleConfiguration { } } + void updateModuleId(String moduleId) { + if (this.@moduleId !== null) { + throw new IllegalStateException() + } + this.@moduleId = moduleId + } + /** * A module that cannot be configured or disabled. */ diff --git a/src/main/java/net/neoforged/camelot/BotMain.java b/src/main/java/net/neoforged/camelot/BotMain.java index 239ae0c..46dab96 100644 --- a/src/main/java/net/neoforged/camelot/BotMain.java +++ b/src/main/java/net/neoforged/camelot/BotMain.java @@ -222,7 +222,11 @@ public static void main(String[] args) { CamelotConfig.setInstance(new CamelotConfig( allModules.values().stream() - .map(module -> (ModuleConfiguration) newInstance(module.configType())) + .map(module -> { + var conf = (ModuleConfiguration) newInstance(module.configType()); + conf.updateModuleId(module.id()); + return conf; + }) .collect(Collectors.toMap( ModuleConfiguration::getClass, Function.identity(), diff --git a/src/main/java/net/neoforged/camelot/module/api/ParameterType.java b/src/main/java/net/neoforged/camelot/module/api/ParameterType.java index f326c8d..6455c7d 100644 --- a/src/main/java/net/neoforged/camelot/module/api/ParameterType.java +++ b/src/main/java/net/neoforged/camelot/module/api/ParameterType.java @@ -55,7 +55,7 @@ public static ParameterType get(String name, Class type, ClassNote: must be called from within a module class */ public static ParameterType get(String name, Class type) { var caller = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)