Skip to content

Commands

WizardlyBump17 edited this page Feb 12, 2022 · 3 revisions

Commands

The command system is provided by the com.wizardlybump17.wlib.command package in the commands module. To create a command is very simple. The only things you will need is your JavaPlugin and the class where the command is in.

Firstly, we need to create the command. For organizational reasons, we will create a new class for it.

public class Commands {
}

Yes. No need for any extends or implements. Now we need to create a method where the command will be executed. With the method, we need to use an annotation called @Command, in that annotation we need to specifiy how the command must be executed using the "execution" parameter. In the method, we MUST create 1 parameter for specify who can execute the command. The sender must implements CommandSender. The Bukkit ones are PlayerSender, ConsoleSender and GenericSender

public class Commands {
   
   @Command(execution = "command")
   public void command(PlayerSender sender) { //PlayerSender implements CommandSender, so he can be used here. If you want that everyone can execute the command, replace it with GenericSender
      player.sendMessage("§aHello world!"); //The CommandSender have some basic methods like sendMessage
   }
}

The last thing we need to do now is to create a new CommandManager for our plugin so the WLib can call the command. The only parameter is a CommandHolder Lets put it in the onEnable method at our JavaPlugin

@Override
public void onEnable() {
   new CommandManager(new BukkitCommandHolder(this) /* This is available in the core module */).register(new Commands() /* the object where the command is in */);
}

That's it! Also, you need to specify the command in the plugin.yml

But if we need to add command parameters? What if I want to add a child to an existent command? Lets do this at the same time!

public class Commands {
   
   @Command(execution = "command")
   public void command(PlayerSender sender) { //PlayerSender implements CommandSender, so he can be used here. If you want that everyone can execute the command, replace it with GenericSender
      sender.sendMessage("§aHello world!");
   }

   //our child
   //note that it have a permission now
   @Command(execution = "command yellow", permission = "command.hello")
   public void command(GenericSender sender) { //any CommandSender can execute it
      sender.sendMessage("§eHello world but yellow!");
   }
   
   @Command(execution = "command color <color>")
   public void color(GenericSender sender, String color) {
      sender.sendMessage(color.replace('&', '§') + "Hello world but with custom color!");
   }
}

See the 3rd command. It have 2 parameters. The 2nd is a String that is passed by the sender.
The arguments are passed to the method params in order so, order matters here. The commands module by itself allows you to use a lot of basic parameters such as the Java primitives (primitives only, the classes like Integer wont work), their arrays and UUID and String, while the core module adds some Bukkit objects
//TODO ArgsReader wiki

Clone this wiki locally