Command guide - How to create a command
Jetrix has been designed to allow the addition of new commands like /who or /list easily. This document will show you the steps to create a simple command /hello that will just display the message "Hello World!".Write the command
Every command is represented by a Java class implementing the net.jetrix.commands.Command interface. Let's create our class, HelloCommand :
import java.util.*;
import net.jetrix.*;
import net.jetrix.messages.*;
import net.jetrix.commands.*;
public class HelloCommand implements Command
{
}
The Command interface defines the required methods that any command has
to implements. We will look at these methods one by one. The first one is the
getAliases() method, it returns an array of Strings containing the names
used to invoke the command. For our command we will use two names, "hello"
and "hi". The first alias in the array is the default name that will be
displayed in the /help list.
public String[] getAliases()
{
return new String[] { "hello", "hi" };
}
The next method is getUsage(Locale locale), it returns a String
describing the usage of the command. It is used when displaying the list of
commands available on the server with /help. You can ignore the
Locale parameter for now, it's used for internationalization purposes.
Our command has no parameter so it's pretty straight forward :
public String getUsage(Locale locale)
{
return "/hello";
}
The getDescription(Locale locale) method is also used for the command
listing, it returns a short description of the command :
public String getDescription(Locale locale)
{
return "Display 'Hello World!'";
}
The getAccessLevel() defines the minimal access level required to use
the command. To allow everyone to use the command it should return 0, to restrict
it to operators only it would return 1.
public int getAccessLevel()
{
return 0;
}
Now we reach the main part of the command, the execute(CommandMessage message)
method. This method is called when the command is executed. The message parameter
contains all the relevant information needed to process the command, that's the
source of the message (i.e. the user that issued the command) and the list of
parameters. Our Hello command will just create a text message and send it back to
the user :
public void execute(CommandMessage message)
{
Message hello = new PlineMessage("Hello World!");
message.getSource().sendMessage(hello);
}
Our command is complete, let's put all the pieces together :
import java.util.*;
import net.jetrix.*;
import net.jetrix.messages.*;
import net.jetrix.commands.*;
public class HelloCommand implements Command
{
public String[] getAliases()
{
return new String[] { "hello", "hi" };
}
public String getUsage(Locale locale)
{
return "/hello";
}
public String getDescription(Locale locale)
{
return "Display 'Hello World!'";
}
public int getAccessLevel()
{
return 0;
}
public void execute(CommandMessage message)
{
Message hello = new PlineMessage("Hello World!");
message.getSource().sendMessage(hello);
}
}
Compile the command
Save the code above in a HelloCommand.java file and copy the jetrix.jar file in the same directory (this jar is in the jetrix/lib directory of the jetrix distribution). Then compile the command with :javac -classpath jetrix.jar HelloCommand.java
Deploy the command
To make your class available to Jetrix just copy it into the jetrix/lib directory. Starting with Jetrix 0.1.1 any .class or .jar file in this directory is automatically loaded at startup. Then you need to declare your command by editing the config.xml file, under the <commands> element just add this :
<command class="HelloCommand"/>
Test the command !
Now we are ready to try the command ! Start Jetrix and log into the server. On typing /help you'll notice that the new command is automatically listed :
