# 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 <mark style="color:green;">**modules**</mark> folder inside the main <mark style="color:green;">**plugin's**</mark> 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. <mark style="color:green;">**Main**</mark> class of your module should extend <mark style="color:green;">**Module**</mark> class of our plugin

```java
public class Main extends Module {

}
```

Main class of Module also has own <mark style="color:green;">**onEnable**</mark> method, so your <mark style="color:green;">**Main**</mark> should look like this:

<pre class="language-java"><code class="lang-java"><strong>public class Main extends Module {
</strong><strong>
</strong>    @Override
    public void onEnable() {
        // Some code here
    }
}
</code></pre>

2. But <mark style="color:green;">**Module**</mark> also needs commands and listeners, right? So, you need to use this command system:

```java
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 <mark style="color:green;">**Main**</mark> class and make some changes:

```java
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 <mark style="color:green;">**ColorManager**</mark> class from <mark style="color:purple;">**DiscordWhitelist**</mark> to color your text, or build something into one String. For example:

```java
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 example:
                                                              // /test player a b c d
                                                              // Using this method you can build "a b c d" into one String
```

3. Event listeners. They're default, but for now we found only one method of how to register them.

```java
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 :(
    }
}
```

4. Nice, but our module needs description. Without this it won't startup.

Find <mark style="color:green;">**resources**</mark> directory where usually <mark style="color:green;">**plugin.yml**</mark> is located in, and create there <mark style="color:green;">**module.yml**</mark>.\
This file should contain next information:

```yaml
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:

```yaml
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** :hearts:
