Skip to main content

Voiced Handlers

Voiced Handlers are server functionalities that enable players to use new commands in the format .command. These commands are often used for automating tasks or accessing specific windows.

To create a new voiced command, you have several options: you can copy an existing file, use the provided example as a template, or create a new class from scratch by implementing the IVoicedCommandHandler interface.

Regardless of the method chosen to create the class, you will need to implement the useVoicedCommand method to process the command. A single Java file can handle multiple commands, typically grouped based on similar functionalities.

There are two types of commands: simple commands and commands with parameters. As of now, there is no standard method for tokenizing parameters. Some techniques involve splitting by spaces, but if your tokens may contain spaces, splitting by commas could be a viable option. The choice of tokenization method is left to your discretion.

Now, let's explore the structure of a basic voiced command.

package com.l2jserver.datapack.handlers.voicedcommandhandlers;

import com.l2jserver.gameserver.handler.IVoicedCommandHandler;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;

public class BasicVoicedCommand implements IVoicedCommandHandler {
// These are the commands you want to include.
private static final String[] VOICED_COMMANDS = {
"simple",
"withparams"
};

@Override
public boolean useVoicedCommand(String command, L2PcInstance player, String params) {
switch (command) {
case "simple": {
player.sendMessage("Invoked simple voiced command simple!");
// Add the logic associated to this command below.

break;
}
case "withparams": {
final var parameters = params.split(" ");

// Just checking if at least one parameter has been provided.
if (parameters.length == 0) {
player.sendMessage("Invoked voiced command withparams without parameters!");
return false;
}

player.sendMessage("Invoked voiced command withparams, with first parameter " + parameters[0] + "!");
// Add the logic associated to this command below.

break;
}
}
return true;
}

@Override
public String[] getVoicedCommandList() {
return VOICED_COMMANDS;
}
}

The BasicVoicedCommand class handles two commands: simple and withparams. These commands are processed using a switch block, although an if-else-if structure could also be used. In the case of simple, a message is sent to the player along with a comment encouraging the addition of custom logic. For withparams, the parameters are parsed by splitting them on spaces. It's important to note that the params parameter can be either an empty string or the actual input provided by the player.

The code verifies if at least one parameter has been passed. If not, a message is displayed to the player, and the command returns false to indicate its failure. On the other hand, if one or more parameters have been provided, a different message is sent to the player, including the first parameter.

To complete the setup, you need to make modifications to the MasterHandler class located in the com.l2jserver.datapack.handlers package. Locate the VOICED_COMMAND_HANDLERS array within the class and add your class accordingly.

import com.l2jserver.datapack.handlers.voicedcommandhandlers.BasicVoicedCommand;

// ...

private static final Class<?>[] VOICED_COMMAND_HANDLERS = {
AutoLoot.class,
StatsVCmd.class,
// TODO: Add configuration options for this voiced commands:
// CastleVCmd.class,
// SetVCmd.class,
(customs().allowWedding() ? Wedding.class : null),
(customs().bankingEnabled() ? Banking.class : null),
(customs().chatAdmin() ? ChatAdmin.class : null),
(customs().multiLangEnable() && customs().multiLangVoiceCommand() ? Lang.class : null),
(customs().debugVoiceCommand() ? Debug.class : null),
(customs().allowChangePassword() ? ChangePassword.class : null),
BasicVoicedCommand.class,
};

// ...

Ensure that you import the BasicVoicedCommand class into the MasterHandler class as well.