Search in sources :

Example 1 with CommandExecutionException

use of org.terasology.logic.console.commandSystem.exceptions.CommandExecutionException in project Terasology by MovingBlocks.

the class ConsoleImpl method execute.

@Override
public boolean execute(Name commandName, List<String> params, EntityRef callingClient) {
    if (commandName.isEmpty()) {
        return false;
    }
    // get the command
    ConsoleCommand cmd = getCommand(commandName);
    // check if the command is loaded
    if (cmd == null) {
        addErrorMessage("Unknown command '" + commandName + "'");
        return false;
    }
    String requiredPermission = cmd.getRequiredPermission();
    if (!clientHasPermission(callingClient, requiredPermission)) {
        callingClient.send(new ErrorMessageEvent("You do not have enough permissions to execute this command (" + requiredPermission + ")."));
        return false;
    }
    if (params.size() < cmd.getRequiredParameterCount()) {
        callingClient.send(new ErrorMessageEvent("Please, provide required arguments marked by <>."));
        callingClient.send(new ConsoleMessageEvent(cmd.getUsage()));
        return false;
    }
    if (cmd.isRunOnServer() && !networkSystem.getMode().isAuthority()) {
        callingClient.send(new CommandEvent(commandName, params));
        return true;
    } else {
        try {
            String result = cmd.execute(params, callingClient);
            if (!Strings.isNullOrEmpty(result)) {
                callingClient.send(new ConsoleMessageEvent(result));
            }
            return true;
        } catch (CommandExecutionException e) {
            Throwable cause = e.getCause();
            String causeMessage;
            if (cause != null) {
                causeMessage = cause.getLocalizedMessage();
                if (Strings.isNullOrEmpty(causeMessage)) {
                    causeMessage = cause.toString();
                }
            } else {
                causeMessage = e.getLocalizedMessage();
            }
            logger.error("An error occurred while executing a command", e);
            if (!Strings.isNullOrEmpty(causeMessage)) {
                callingClient.send(new ErrorMessageEvent("An error occurred while executing command '" + cmd.getName() + "': " + causeMessage));
            }
            return false;
        }
    }
}
Also used : CommandExecutionException(org.terasology.logic.console.commandSystem.exceptions.CommandExecutionException) ConsoleCommand(org.terasology.logic.console.commandSystem.ConsoleCommand)

Aggregations

ConsoleCommand (org.terasology.logic.console.commandSystem.ConsoleCommand)1 CommandExecutionException (org.terasology.logic.console.commandSystem.exceptions.CommandExecutionException)1