Dependency injection
This page explains how to use dependency injection, which provides a way to inject dependencies (instances of objects) into command classes
Lamp supports a simple form of dependency injection, allowing you to manage dependencies with ease. This feature simplifies the process of injecting dependencies into your command classes.
Adding Dependencies
Dependencies are added to the Lamp instance using the builder.dependency(...) method. You can provide dependencies as either a supplier or an object.
Lamp lamp = Lamp.builder()
.dependency(questManager) // Adding a constant dependency
.build();val lamp = Lamp.builder()
.dependency(questManager)
.build()Injecting Dependencies
To inject dependencies into fields, use the @Dependency annotation. This annotation tells the framework to inject the specified dependency into the annotated field.
@CommandPermission("quests.command")
public class QuestCommands {
@Dependency
private QuestManager questManager; // Dependency will be injected here
@Command("quest create")
public void createQuest(CommandSender sender, String name, String description) {
// Use questManager to create a quest
}
...
}@CommandPermission("quests.command")
class QuestCommands {
@Dependency
private lateinit var questManager: QuestManager // Dependency will be injected here
@Command("quest create")
fun createQuest(sender: CommandSender, name: String, description: String) {
// Use questManager to create a quest
}
...
}
Last updated
Was this helpful?