-
Notifications
You must be signed in to change notification settings - Fork 0
Modular plugins
The modular plugin system was one of the first features we worked on when starting this project. Originally developed by Sxtanna, the modular plugin system allows us to create a system of "sub-plugins" within one plugin.
In this example, we will create a small "hello" command plugin. To begin we will create our main class:
package com.coalesce.example;
import com.coalesce.plugin.CoPlugin;
public final class HelloMain extends CoPlugin {
@Override
public void onPluginEnable() throws Exception {
}
@Override
public void onPluginDisable() throws Exception {
}
}Now that we have our main class, we can start the making of modules. In this example, I'm going to be using the command class we created in the Command tutorial.
package com.coalesce.example;
import com.coalesce.plugin.CoModule;
import com.coalesce.plugin.CoPlugin;
public class HelloCommand extends CoModule {
public HelloCommand(CoPlugin plugin) {
super(plugin, "Command Module");
}
@Override
protected void onEnable() throws Exception {
}
@Override
protected void onDisable() throws Exception {
}In order for a module to be declared a module it has to extend CoModule. Once extended, implement the overridden methods provided from the CoModule class. Next, we can add the default constructor, pass the instance of Main onto it and name the module. We will now start adding commands to the class.
package com.coalesce.example;
import com.coalesce.plugin.CoModule;
import com.coalesce.plugin.CoPlugin;
public class HelloCommand extends CoModule {
public HelloCommand(CoPlugin plugin) {
super(plugin, "Command Module");
}
@Override
protected void onEnable() throws Exception {
}
@Override
protected void onDisable() throws Exception {
}
private void hello(CommandContext context) {
context.send(ChatColor.ITALICS + "Hello!");
}
private void helloTab(TabContext context) {
context.completionAt(1, context.getSender().getName());
}
}After adding the command methods into this class the last thing we need to do is register the command, which is what we're going to do in the onEnable method of this module.
package com.coalesce.example;
import com.coalesce.plugin.CoModule;
import com.coalesce.plugin.CoPlugin;
public class HelloCommand extends CoModule {
public HelloCommand(CoPlugin plugin) {
super(plugin, "Command Module");
}
@Override
protected void onEnable() throws Exception {
CoCommand command = new CommandBuilder(plugin, "hello")
.executor(this::hello)
.completion(this::helloTab)
.usage("/hello")
.description("Greets you!")
.maxArgs(0)
.playerOnly()
.build();
plugin.addCommand(command);
}
@Override
protected void onDisable() throws Exception {
}
private void hello(CommandContext context) {
context.send(ChatColor.ITALICS + "Hello!");
}
private void helloTab(TabContext context) {
context.completionAt(1, context.getSender().getName());
}
}If you were to compile this plugin as it is now, the command would not show up. We need to register this CoModule, which in turn registers the command. To register the Module we will go back to our main class onPluginEnable and call the addModules method. In the method parameters, we add the new instance of the CoModule. After that, you can compile and run the plugin.
package com.coalesce.example;
import com.coalesce.plugin.CoPlugin;
public final class HelloMain extends CoPlugin {
@Override
public void onPluginEnable() throws Exception {
addModules(new HelloCommand(this));
}
@Override
public void onPluginDisable() throws Exception {
}
}Please do note, commands do not have to be put inside a module, they can simply be a class that doesn't extend anything if you'd like, this was just a quick example to show how the module system works and how to register the modules.