Skip to content

Console Server commands

Brov3r edited this page Aug 9, 2024 · 3 revisions

Console/server commands

Avrix allows you to create custom commands in a similar way to how it is implemented in the game. They are implemented exclusively on the server side.

Commands creation

To create a command, you need to create a separate class, in our case Example, and extends the Command abstract class:

@CommandName("example")
@CommandAccessLevel(AccessLevel.NONE) // You don't have to set the value, by default AccessLevel.NONE
@CommandExecutionScope(CommandScope.CHAT) // You don't have to set the value, by default CommandScope.BOTH
@CommandDescription("Command description")
public class ExampleCommand extends Command {
    /**
     * Performing a chat command action
     *
     * @param playerConnection {@link UdpConnection}, if called from the console, the connection will return as {@code null}
     * @param args             arguments of the received command
     */
    @Override
    public String onInvoke(UdpConnection playerConnection, String[] args) {
        return "[###] Test command. Args: " + Arrays.toString(args);
    }
}

CommandName - is the name of the command that will be used in the chat/console.

CommandAccessLevel - required access level of the caller, selection from enum AccessLevel.

CommandExecutionScope - the place where the command should be executed server console/chat, selection from the CommandScope enum.

CommandDescription - description of the command, what it does.

Note

The command is called using / if this is a server chat, and without it if this is a console.

All these annotations are integral and without them an exception will be thrown when registering the command. To register a command, you need to sign it at the entry point:

/**
 * Main entry point of the example plugin
 */
public class Main extends Plugin {
    /**
     * Constructs a new {@link Plugin} with the specified metadata.
     * Metadata is transferred when the plugin is loaded into the game context.
     *
     * @param metadata The {@link Metadata} associated with this plugin.
     */
    public Main(Metadata metadata) {
        super(metadata);
    }

    /**
     * Called when the plugin is initialized.
     * <p>
     * Implementing classes should override this method to provide the initialization logic.
     */
    @Override
    public void onInitialize() {
        CommandsManager.addCommand(new TestCommand());
    }
}
Clone this wiki locally