Skip to content

Commands

Jeremy Harton edited this page Apr 28, 2019 · 2 revisions

The vast majority of the basic commands are implemented in this form:

cmd_<name>(String arg, Client client) { ... }

* is replaced with the name of the command, which may be more or less descriptive as space and usability permits

The main method of translating a typed command into the action uses an if-elseif-else loop which gets longer and more cumbersome with every additional command you add to it. It amounts to a straight equality test if the string typed matches the command (or an alias of it). In order to avoid this, to an extent, an alternate mechanism is provided for extending the command set beyond the basics. That mechanism is an abstract class named Command. You can add a command to the system by writing a subclass of Command such as WhereCommand (a 'where' command in this system). Command has two abstract methods (subclasses must implement them). Their method signatures are as below:

public abstract void execute(String arg, Client client);

public abstract int getAccessLevel();

The first, execute(...), specifies the code that will form the command, while the other allows you to specify a permission level for this command. Once you have created and written this class, then you must add an instance of each of them to the commandMap. After that the command interpreter simply checks to see if your command in is the map (if no other command by that name exists) and executes it, checking against your permissions to see if you are allowed to use that command

Clone this wiki locally