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;
}
}
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)));
}
Aggregations