Skip to content

2. The CommandManager

Arpaesis edited this page Mar 8, 2019 · 15 revisions

In order to get started with Armada, you first need a CommandManager object:

CommandManager<Object, Object> commandManager = new CommandManager<>();

This commandManager object is what will allow you to register your commands. Do not worry about the type arguments for now; those will be important later.

Before creating commands, a prefix (if any at all) should be considered. You can set the prefix for the manager like so:

commandManager.setPrefix("!");

The command manager now has a set prefix. You'll have to use this prefix when calling commands.

Now that your command manager is instantiated and is ready to go, you can begin registering commands to it. Registering commands is straight-forward:

commandManager.register(new BasicCommand());

The BasicCommand is just an example type; your command class may have a different name.

Now that commands are registered in the manager, you need a place to derive String input from, so that the manager can act on it.

In the following example, a basic program has been created with an infinite do-while loop, where each iteration awaits input from the console:

private static final CommandManager<Object, Object> manager = new CommandManager<>();
	
public static void main(String[] arguments)
{

	manager.setPrefix("!");
	manager.register(new BasicCommand());

	Scanner scanner = new Scanner(System.in);

	String in;
	do
	{
		in = scanner.nextLine();
		manager.execute(new Object(), in);
	}
	while (!in.equalsIgnoreCase("END"));
	scanner.close();
}

Every iteration of the do-while loop will get input from the user. The manager will then process the input given from the console, and execute the command accordingly. How you get input is not important; what is important is that the input is given as a String.

The first parameter new Object() passed into the manager's execute is generic based; whatever the first generic argument of your command manager was, the command manager's execute method will expect that exact same type as the first parameter. In this case, the execute method expects an Object, so a new Object() parameter is given.

The first parameter that is passed in is not important unless your command's execution method makes use of it.

Clone this wiki locally