Skip to main content

Getting Started

This tutorial guides you through adding the core module and bootstrapping your plugin's dependency injection environment.

1. Adding Dependencies

MittenLib is modular. At a minimum, you need the core module. We recommend using the Gradle Kotlin DSL.

repositories {
maven("https://repo.bristermitten.me/public/")
}

dependencies {
implementation("me.bristermitten:mittenlib-core:VERSION")

// You can add other modules as needed:
// implementation("me.bristermitten:mittenlib-annotation-processor:VERSION")
// implementation("me.bristermitten:mittenlib-commands:VERSION")
}

2. Setting up Guice

To use MittenLib, you need to create a Guice Injector in your main plugin class. MittenLib handles binding your plugin instance, the server, standard loggers, and other Bukkit internals automatically.

package me.bristermitten.mittenlib.docs.tutorial;

import com.google.inject.Guice;
import com.google.inject.Injector;
import me.bristermitten.mittenlib.MittenLib;
import org.bukkit.plugin.java.JavaPlugin;

public class MainPlugin extends JavaPlugin {
@Override
public void onEnable() {
// Initialize MittenLib with some default modules
MittenLib<JavaPlugin> mittenLib = MittenLib.withDefaults(this);

// Setup the Guice injector
Injector injector = mittenLib.setup();

// Instantiate your main application logic
MyService service = injector.getInstance(MyService.class);
service.start();
}
}

3. Using Dependency Injection

Once the injector is created, you can write clean, testable classes that request their dependencies via the constructor using @Inject.

MittenLib automatically binds common Spigot types such as Server and Plugin. See me.bristermitten.mittenlib.MittenLibCoreModule to see what exactly is bound.

package me.bristermitten.mittenlib.docs.tutorial;

import com.google.inject.Inject;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;

public class MyService {
private final JavaPlugin plugin;
private final Logger logger;

@Inject
public MyService(JavaPlugin plugin, Logger logger) {
this.plugin = plugin;
this.logger = logger;
}

public void start() {
logger.info("MyService started successfully for " + plugin.getName() + "!");
}
}

Next Steps

Now that you have the base library and dependency injection set up, you can explore MittenLib's other features:

  • Config System: Auto-generate configuration classes and load them seamlessly via Guice.
  • Other Modules: Check out the optional modules available to speed up other areas of your development.