Search in sources :

Example 1 with CommandParameterParseException

use of org.terasology.engine.logic.console.commandSystem.exceptions.CommandParameterParseException 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

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