Skip to content

Commit

Permalink
Add a way of referencing modules by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jun 6, 2024
1 parent 1684ad3 commit 139565a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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!')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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.
*/
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/net/neoforged/camelot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static <T> ParameterType<T> get(String name, Class<T> type, Class<? exten

/**
* {@return an interned parameter}
* @apiNote must be called from within a module class
* <b>Note:</b> must be called from within a module class
*/
public static <T> ParameterType<T> get(String name, Class<T> type) {
var caller = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
Expand Down

0 comments on commit 139565a

Please sign in to comment.