Skip to content
Alexis Chân Gridel edited this page Jul 31, 2022 · 4 revisions

GuicyBukkit is just a simple tool to use Guice with Spigot/Bukkit.

If you do not know how Guice works, please take a look at Guice's documentation.

Why GuicyBukkit?

Using Guice with Bukkit plugins was pretty nice, however, there were still some boilerplate code lying around just to make it "work" with Bukkit. Such as registering listeners, or even adding commands.

GuicyBukkit removes all of that boilerplate code and gives you an unified way to register your Bukkit dependencies.

Adding GuicyBukkit into your plugin

If you need help adding the dependency, visit the Using GuicyBukkit page.

The PluginModule

When using plain old Guice, the most common way to create a module was to inherit AbstractModule. Now, you can use PluginModule, which extends AbstractModule and adds some methods to register Bukkit related stuff, here's an example:

 public class MyNicePluginModule extends PluginModule {
     // We need a MyNicePlugin instance to create this module, and inject it.
     public MyNicePluginModule(MyNicePlugin plugin) {
         super(plugin);
     }

     @Override
     protected void configureBindings() {
         bind(SomeService.class).to(SomeServiceImpl.class);
     }

     @Override
     protected void configureListeners(Multibinder<Listener> binder) {
         binder.addBinding().to(UsefulListener.class);
     }

     @Override
     protected void configureCommands(Multibinder<CommandConfigurator> binder) {
         binder.addBinding().to(HelloCommand.class);
     }
 }
  • The MyNicePluginModule(MyNicePlugin plugin) constructor takes a MyNicePlugin, that will be registered and available as a dependency to inject.
  • The configureBindings() method is pretty much where you would write your bindings instead of configure().
  • The configureListeners(Multibinder<Listener> binder) method has a binder to put all your listeners to.
  • The configureCommands(Multibinder<CommandConfigurator> binder) method has a binder to put all your commands to... But what is a CommandConfigurator? Let's figure it out!

The CommandConfigurator

The CommandConfigurator interface is pretty simple:

public interface CommandConfigurator {
    String getCommandName();
    void configureCommand(PluginCommand command);
}

It configures a command of name getCommandName, and once that command has been found, it can be configured by using configureCommand. This way, commands can be registered the way you want.

There is also an AnnotatedCommandConfigurator, which overrides getCommandName to instead get the value of the @CommandName annotation on the current type.

Finally, there is the SelfConfiguredCommandExecutor, which is a simple CommandExecutor wrapper, which also implements AnnotatedCommandConfigurator. Here is an example:

 @CommandName("hello")
 public class HelloCommand extends SelfConfiguredCommandExecutor {
     @Override
     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
         sender.sendMessage("Hello, " + sender.getName() + "!");
         return true;
     }
 }

Once registered, the command /hello will greet the player!

Registering listeners and commands into the plugin

We now have all listeners and commands in the MyNicePluginModule. It's time to register them in the plugin, by creating a new instance of PluginDependencies using an Injector!

public class MyNicePlugin extends JavaPlugin {
    @Override
    public void onLoad() {
        Injector injector = Guice.createInjector(new MyNicePluginModule(this));
        PluginDependencies.fromInjector(injector).registerAll(this);
    }
}

We first create an injector, with a MyNicePluginModule using the instance of the plugin. Then, PluginDependencies.fromInjector will grab an instance of PluginDependencies from the ìnjector. Once that is done, we finally register all the listeners and commands using registerAll.

Shading your plugin to include the library

To properly use GuicyBukkit, you need to "shade" your plugin that will be allowing you to create a jar with all of its dependencies within.

If you are using Maven :

https://maven.apache.org/plugins/maven-shade-plugin/

If you are using Gradle :

https://imperceptiblethoughts.com/shadow/introduction/

Note: Don't forget to relocate

That's it!

That's pretty much all GuicyBukkit is about. If you need some help, create an issue on the repository, or post a thread on spigot/bukkit forums.