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:
Main class of your module should extend Module class of our plugin
publicclassMainextendsModule {}
Main class of Module also has own onEnable method, so your Main should look like this:
publicclassMainextendsModule { @OverridepublicvoidonEnable() {// Some code here }}
But Module also needs commands and listeners, right? So, you need to use this command system:
publicclassExampleCommandextendsBukkitCommand {publicBukkitCommand exampleCommand;publicExampleCommand(@NotNullString permission, @NotNullString name, @NotNullString description, @NotNullString usage, @NotNullList<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(); } } @Overridepublicbooleanexecute(@NotNullCommandSender commandSender, @NotNullString name, @NotNullString[] args) {// Code of command herereturntrue; }publicvoidregisterCommands() {if(exampleCommand !=null) exampleCommand.unregister(getCommandMap());registerTestCommand(); }privatevoidregisterTestCommand() {ArrayList<String> aliases =newArrayList<>();aliases.add("example"); // Our main command namealiases.add("ex"); // Aliases for main commandString usage ="/<command>"; // How to use this commandString description ="An example command"; // Description of this commandString permission ="example.command"; // And permission of this commandBukkitCommand command =newExampleCommand(permission,aliases.get(0), description, usage, aliases); exampleCommand = command; }publicCommandMapgetCommandMap() {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:
publicclassMainextendsModule { @OverridepublicvoidonEnable() {newExampleCommand( // 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 textcommandSender.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
Event listeners. They're default, but for now we found only one method of how to register them.
publicclassMainextendsModule { @OverridepublicvoidonEnable() {Bukkit.getPluginManager().registerEvents(newListener(),com.windstudio.discordwl.Main.getInstance()); // Instance of DiscordWhitelist plugin// So this listener will be registered// in DiscordWhitelist plugin. For now// this is only way :( }}
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 classname:name# Name of the moduleauthor:author# Author of the moduleversion:version# Version of the moduledescription:description# Description of the modulefolder:false# Should module have own folder inside "modules" directory named like module's name
For example:
main:dev.windstudio.examplemodule.Mainname:ExampleModuleauthor:WIND STUDIOversion:1.0description:Example of the module creationfolder:false