🛖
Fox Hut
  • Introduction
    • Setting up
    • CommandActor, @Command and @Subcommand
    • Creating your first command
    • Improving our greet command
  • Platforms
    • Bukkit / Spigot / Paper
    • BungeeCord
    • Velocity
    • Sponge
    • Fabric
    • Brigadier
    • Minestom
    • JDA
    • Command line
  • How-to
    • Creating variants of /teleport
    • Custom parameter types
    • Suggestions and auto-completion
    • Context parameters
    • Command permissions
    • Parameter validators
    • Command conditions
    • Response handlers
    • Cooldowns
    • Help commands
    • Annotation replacers
    • Orphan command
    • Exception handling
    • Hooks
    • Dependency injection
    • Visitors
    • Customizing the dispatcher and failure behavior
Powered by GitBook
On this page
  • Adding Dependencies
  • Injecting Dependencies

Was this helpful?

Edit on GitHub
  1. How-to

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
    }
    
    ...
}
PreviousHooksNextVisitors

Last updated 9 months ago

Was this helpful?