Modules

Modules expand the capabilities of the main plugin. They are practically a plugin themselves. That is why module can have own folder inside the modules folder inside the main plugin's folder. Because of this you can easily create any config or any other file and put it inside separate folder to store information for example. By default it's turned off, but below you can find how to enable it.

Creating a module is almost similar to plugin creation. So, to create addon you need follow this:

  1. Main class of your module should extend Module class of our plugin

public class Main extends Module {

}

Main class of Module also has own onEnable method, so your Main should look like this:

public class Main extends Module {

    @Override
    public void onEnable() {
        // Some code here
    }
}
  1. But Module also needs commands and listeners, right? So, you need to use this command system:

public class ExampleCommand extends BukkitCommand {
    public BukkitCommand exampleCommand;
    
    public ExampleCommand(@NotNull String permission, @NotNull String name, @NotNull String description, @NotNull String usage, @NotNull List<String> aliases) {
        super(name, description, usage, aliases);
        this.setName(name);
        this.setDescription(description);
        this.setUsage(usage);
        this.setAliases(aliases);
        this.setPermission(permission);
        try {
            Field f = Bukkit.getServer().getClass().getDeclaredField("commandMap");
            f.setAccessible(true);
            CommandMap commandMap = (CommandMap) f.get(Bukkit.getServer());
            commandMap.register(name, this);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public boolean execute(@NotNull CommandSender commandSender, @NotNull String name, @NotNull String[] args) {
        // Code of command here
        return true;
    }
    
    public void registerCommands() {
        if(exampleCommand != null) exampleCommand.unregister(getCommandMap());
        registerTestCommand();
    }

    private void registerTestCommand() {
        ArrayList<String> aliases = new ArrayList<>();
        aliases.add("example"); // Our main command name
        aliases.add("ex"); // Aliases for main command

        String usage = "/<command>"; // How to use this command
        String description = "An example command"; // Description of this command
        String permission = "example.command"; // And permission of this command

        BukkitCommand command = new ExampleCommand(permission, aliases.get(0), description, usage, aliases);
        exampleCommand = command;
    }
    
    public CommandMap getCommandMap() {
        CommandMap commandMap = null;
        try {
            Field f = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
            f.setAccessible(true);
            commandMap = (CommandMap) f.get(Bukkit.getPluginManager());
        } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException | SecurityException e) {
            e.printStackTrace();
        }
        return commandMap;
    }
}

So, you need to register this command. Go to your Main class and make some changes:

public class Main extends Module {

    @Override
    public void onEnable() {
        new ExampleCommand( // New class object
                "em.admin", // Permission of the command
                "example", // Name of the command(it will be also our command)
                "An example command", // Description of the command
                "example", // Usage of this command (/<command>)
                Collections.singletonList("ex")) // Alliases of the command, as List
                .registerCommands(); // Optional i think, method from ExampleCommand class
    }
}

Also you can use ColorManager class from DiscordWhitelist to color your text, or build something into one String. For example:

commandSender.sendMessage(ColorManager.translate("&aColored &ctext&f!")) // Will color this text
commandSender.sendMessage(ColorManager.buildString(args, 2)); // Will build args with spaces, etc. into one String, for exasmple:
                                                              // /test player a b c d
                                                              // Using this method you can build "a b c d" into one String
  1. Event listeners. They're default, but for now we found only one method of how to register them.

public class Main extends Module {

    @Override
    public void onEnable() {
        Bukkit.getPluginManager().registerEvents(
        new Listener(), 
        com.windstudio.discordwl.Main.getInstance()); // Instance of DiscordWhitelist plugin
                                                      // So this listener will be registered
                                                      // in DiscordWhitelist plugin. For now
                                                      // this is only way :(
    }
}
  1. Nice, but our module needs description. Without this it won't startup.

Find resources directory where usually plugin.yml is located in, and create there module.yml. This file should contain next information:

main: path # Path to Main class
name: name # Name of the module
author: author # Author of the module
version: version # Version of the module
description: description # Description of the module
folder: false # Should module have own folder inside "modules" directory named like module's name

For example:

main: dev.windstudio.examplemodule.Main
name: ExampleModule
author: WIND STUDIO
version: 1.0
description: Example of the module creation
folder: false

Now you made your own module for DiscordWhitelist ♥️

Last updated