Overview
Essential uses Dependency Injection (DI) to provide instances of its API interfaces. The library Kodein is used to power Essential’s DI system. DI provides a cleaner alternative to static method calls and allows you to easily access Essential’s APIs throughout your codebase.Basic Usage
Kotlin
Use theget() function to retrieve instances from the DI container:
get() function is a utility that quickly retrieves an instance from the dependency injection framework. The generic type T is the class that is looked up.
Signature:
Java
For Java, use the static methods onEssentialAPI:
The DI Class
TheDI abstract class provides access to Essential’s dependency injection engine.
Accessing DI
Adding Custom Modules
You can customize Essential’s DI by making your own types available and usable in your project using theaddModule() method.
DI Implementation
Essential’sDI class implements Kodein’s DIAware interface, giving it access to all of Kodein’s dependency injection features.
Checking DI Initialization
You can check if Essential’s DI has been initialized:Available Types
The following types are available through Essential’s DI system:EssentialAPI- Main API interfaceCommandRegistry- Command registrationNotifications- Notification systemEssentialConfig- Essential configurationGuiUtil- GUI utilitiesMinecraftUtils- Minecraft utilitiesShutdownHookUtil- Shutdown hooksImageCache- Image cachingTrustedHostsUtil- Trusted hostsEssentialComponentFactory- Elementa componentsMojangAPI- Mojang API wrapperOnboardingData- TOS/onboarding dataDI- The DI system itself
Usage Examples
Simple DI Usage
Custom Module with Dependencies
Multiple API Access
When to Use DI vs Static Access
Use DI When:
- Building larger applications with multiple components
- You want to inject dependencies into your classes
- You need testability and mock support
- You prefer constructor injection patterns
Use Static Access When:
- Making quick one-off API calls
- Working in Java without DI framework
- You prefer explicit static method calls
- Simplicity is preferred over flexibility
Source Reference
- DI Class:
api/src/main/kotlin/gg/essential/api/DI.kt - DI Utilities:
api/src/main/kotlin/gg/essential/api/utils/di.kt - Package:
gg.essential.api - External: Kodein DI Documentation