Essential provides a powerful API for Minecraft mod developers, offering features like command registration, notifications, GUI components, and utilities. This guide will help you integrate Essential’s API into your mod.
For players: If you’re looking to install Essential Mod as a player, see the Installation guide instead.
Add Essential to your build.gradle or build.gradle.kts file:
Gradle (Kotlin DSL)
Gradle (Groovy)
build.gradle.kts
repositories { maven("https://repo.essential.gg/repository/maven-public")}dependencies { // Use 'compileOnly' for runtime dependency (Essential required) compileOnly("gg.essential:essential-1.8.9-forge:+") // Or use 'implementation' to embed Essential // implementation("gg.essential:essential-1.8.9-forge:+")}
Essential’s Command API automatically handles argument parsing and type conversion:
Kotlin
Java
MyCommand.kt
import gg.essential.api.commands.Commandimport gg.essential.api.commands.DefaultHandlerimport gg.essential.api.EssentialAPIobject MyCommand : Command("mycommand") { @DefaultHandler fun handle() { // Send a notification when command is executed EssentialAPI.getNotifications().push( "My Mod", "Command executed successfully!" ) }}
MyCommand.java
import gg.essential.api.commands.Command;import gg.essential.api.commands.DefaultHandler;import gg.essential.api.EssentialAPI;public class MyCommand extends Command { public MyCommand() { super("mycommand"); } @DefaultHandler public void handle() { // Send a notification when command is executed EssentialAPI.getNotifications().push( "My Mod", "Command executed successfully!" ); }}
import gg.essential.api.EssentialAPI;// Get the API instanceEssentialAPI api = EssentialAPI.getInstance();// Or use static convenience methods:EssentialAPI.getNotifications();EssentialAPI.getCommandRegistry();EssentialAPI.getGuiUtil();
EssentialAPI.getNotifications().push( title = "Click me!", message = "This notification does something when clicked", action = { println("Notification clicked!") // Open a GUI, execute a command, etc. })
EssentialAPI.getNotifications().push( title = "Advanced Notification", message = "Fully customized", duration = 5f, configure = { // Set notification type (changes color) type = NotificationType.WARNING // Trim long messages trimMessage = true // Custom action onAction = { println("Clicked!") } // Custom close callback onClose = { println("Notification closed") } })
The Command API automatically generates usage messages when arguments are invalid. For example, if the user types /teleport abc, they’ll see: Usage: /teleport <distance>
import gg.essential.api.EssentialAPIimport net.minecraft.client.gui.GuiScreen// Queue a screen to openEssentialAPI.getGuiUtil().openScreen(myCustomScreen)// Get the currently open screenval currentScreen = EssentialAPI.getGuiUtil().openedScreen()
Essential provides utilities for interacting with Mojang’s API:
import gg.essential.api.EssentialAPIimport java.util.UUIDval mojangAPI = EssentialAPI.getMojangAPI()// Get UUID from usernameval uuid: UUID? = mojangAPI.getUUIDFromUsername("Notch")// Get username from UUIDval username: String? = mojangAPI.getUsernameFromUUID(uuid)// Get game profileval profile = mojangAPI.getProfile(uuid)
For larger projects, use Essential’s DI system for cleaner architecture:
import gg.essential.api.EssentialAPIimport gg.essential.api.gui.Notificationsclass MyService(private val notifications: Notifications) { fun doSomething() { notifications.push("Title", "Message") }}// Get service via DIval di = EssentialAPI.getDI()val notifications = di.get<Notifications>()val service = MyService(notifications)
Handle command errors gracefully
Always validate command input and provide helpful feedback:
@DefaultHandlerfun handle(amount: Int) { if (amount <= 0) { EssentialAPI.getNotifications().push( title = "Error", message = "Amount must be positive!", configure = { type = NotificationType.ERROR } ) return } // Process valid amount...}
Use notification types appropriately
Choose the right notification type for better user experience:
Here are some complete examples to help you get started:
Complete command with multiple argument types
CompleteCommand.kt
import gg.essential.api.commands.Commandimport gg.essential.api.commands.DefaultHandlerimport gg.essential.api.commands.SubCommandimport gg.essential.api.commands.DisplayNameimport gg.essential.api.EssentialAPIimport gg.essential.api.gui.NotificationTypeobject ItemCommand : Command("item", autoHelpSubcommand = true) { @DefaultHandler fun handleDefault() { EssentialAPI.getNotifications().push( "Item Command", "Use /item give or /item list" ) } @SubCommand("give") fun handleGive( @DisplayName("itemName") item: String, @DisplayName("count") amount: Int? ) { val count = amount ?: 1 if (count <= 0 || count > 64) { EssentialAPI.getNotifications().push( title = "Error", message = "Amount must be between 1 and 64", configure = { type = NotificationType.ERROR } ) return } // Give item logic here... EssentialAPI.getNotifications().push( title = "Item Given", message = "Gave $count x $item", configure = { type = NotificationType.INFO } ) } @SubCommand("list") fun handleList() { // List items logic... EssentialAPI.getNotifications().push( "Items", "Check chat for item list" ) }}
GUI integration example
CustomGUI.kt
import gg.essential.api.EssentialAPIimport gg.essential.api.gui.EssentialGUIimport gg.essential.elementa.ElementaVersionimport gg.essential.elementa.components.UITextimport gg.essential.elementa.dsl.*class MyCustomGUI : EssentialGUI( ElementaVersion.V2, "My Custom GUI", discordActivityDescription = "Managing settings") { init { // Add content to the GUI val text = UIText("Hello from Essential GUI!").constrain { x = CenterConstraint() y = CenterConstraint() } childOf content }}// Open the GUIfun openMyGUI() { EssentialAPI.getGuiUtil().openScreen(MyCustomGUI())}