Search in sources :

Example 1 with CommandSuggestionException

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

the class CyclingTabCompletionEngine method findMatches.

private Set<String> findMatches(Name commandName, List<String> commandParameters, ConsoleCommand command, int suggestedIndex) {
    if (suggestedIndex <= 0) {
        updateCommandNamesIfNecessary();
        return StringUtility.wildcardMatch(commandName.toString(), commandNames, true);
    } else if (command == null) {
        return null;
    }
    List<String> finishedParameters = Lists.newArrayList();
    for (int i = 0; i < suggestedIndex - 1; i++) {
        finishedParameters.add(commandParameters.get(i));
    }
    String currentValue = commandParameters.size() >= suggestedIndex ? commandParameters.get(suggestedIndex - 1) : null;
    EntityRef sender = localPlayer.getClientEntity();
    try {
        return command.suggest(currentValue, finishedParameters, sender);
    } catch (CommandSuggestionException e) {
        String causeMessage = e.getLocalizedMessage();
        if (causeMessage == null) {
            Throwable cause = e.getCause();
            causeMessage = cause.getLocalizedMessage();
            if (causeMessage == null || causeMessage.isEmpty()) {
                causeMessage = cause.toString();
                if (causeMessage == null || causeMessage.isEmpty()) {
                    return null;
                }
            }
        }
        console.addMessage("Error when suggesting command: " + causeMessage, CoreMessageType.ERROR);
        return null;
    }
}
Also used : CommandSuggestionException(org.terasology.engine.logic.console.commandSystem.exceptions.CommandSuggestionException) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 2 with CommandSuggestionException

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

the class AbstractCommand method suggest.

@Override
public final Set<String> suggest(final String currentValue, List<String> rawParameters, EntityRef sender) throws CommandSuggestionException {
    // Generate an array to be used as a parameter in the 'suggest' method
    Object[] processedParameters;
    try {
        processedParameters = processParametersMethod(rawParameters, sender);
    } catch (CommandParameterParseException e) {
        String warning = "Invalid parameter '" + e.getParameter() + "'";
        String message = e.getMessage();
        if (message != null) {
            warning += ": " + message;
        }
        throw new CommandSuggestionException(warning);
    }
    // Get the suggested parameter to compare the result with
    CommandParameter suggestedParameter = null;
    Iterator<CommandParameter> paramIter = commandParameters.iterator();
    for (Object processedParameter : processedParameters) {
        if (sender.equals(processedParameter)) {
            continue;
        }
        if (processedParameter == null) {
            suggestedParameter = paramIter.next();
            break;
        }
        paramIter.next();
    }
    if (suggestedParameter == null) {
        return Sets.newHashSet();
    }
    Set<Object> result = null;
    result = suggestedParameter.suggest(sender, processedParameters);
    if (result == null) {
        return Sets.newHashSet();
    }
    Class<?> requiredClass = suggestedParameter.getType();
    for (Object resultComponent : result) {
        if (resultComponent == null && requiredClass.isPrimitive()) {
            throw new CommandSuggestionException("The 'suggest' method of command class " + getClass().getCanonicalName() + " returns a collection containing an invalid type. Required: " + requiredClass.getCanonicalName() + "; provided: null");
        } else if (resultComponent != null && !requiredClass.isAssignableFrom(resultComponent.getClass())) {
            throw new CommandSuggestionException("The 'suggest' method of command class " + getClass().getCanonicalName() + " returns a collection containing an invalid type. Required: " + requiredClass.getCanonicalName() + "; provided: " + resultComponent.getClass().getCanonicalName());
        }
    }
    Set<String> stringSuggestions = convertToString(result, suggestedParameter);
    // Only return results starting with currentValue
    return Sets.filter(stringSuggestions, input -> input != null && (currentValue == null || input.startsWith(currentValue)));
}
Also used : CommandSuggestionException(org.terasology.engine.logic.console.commandSystem.exceptions.CommandSuggestionException) SpecificAccessibleObject(org.terasology.engine.utilities.reflection.SpecificAccessibleObject) CommandParameterParseException(org.terasology.engine.logic.console.commandSystem.exceptions.CommandParameterParseException)

Aggregations

CommandSuggestionException (org.terasology.engine.logic.console.commandSystem.exceptions.CommandSuggestionException)2 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)1 CommandParameterParseException (org.terasology.engine.logic.console.commandSystem.exceptions.CommandParameterParseException)1 SpecificAccessibleObject (org.terasology.engine.utilities.reflection.SpecificAccessibleObject)1