use of org.terasology.logic.console.commandSystem.ConsoleCommand in project Terasology by MovingBlocks.
the class CyclingTabCompletionEngine method complete.
@Override
public String complete(String rawCommand) {
if (rawCommand.length() <= 0) {
reset();
previousMessage = new Message("Type 'help' to list all commands.");
console.addMessage(previousMessage);
return null;
} else if (query == null) {
query = rawCommand;
}
String commandNameRaw = console.processCommandName(query);
Name commandName = new Name(commandNameRaw);
List<String> commandParameters = console.processParameters(query);
ConsoleCommand command = console.getCommand(commandName);
int suggestedIndex = commandParameters.size() + (query.charAt(query.length() - 1) == ' ' ? 1 : 0);
Set<String> matches = findMatches(commandName, commandParameters, command, suggestedIndex);
if (matches == null || matches.size() <= 0) {
return query;
}
if (previousMatches == null || !matches.equals(Sets.newHashSet(previousMatches))) {
reset(false);
if (matches.size() == 1) {
return generateResult(matches.iterator().next(), commandName, commandParameters, suggestedIndex);
}
/* if (matches.length > MAX_CYCLES) {
console.addMessage(new Message("Too many hits, please refine your search"));
return query;
}*/
// TODO Find out a better way to handle too many results while returning useful information
previousMatches = Lists.newArrayList(matches);
Collections.sort(previousMatches);
}
StringBuilder matchMessageString = new StringBuilder();
for (int i = 0; i < previousMatches.size(); i++) {
if (i > 0) {
matchMessageString.append(' ');
}
String match = previousMatches.get(i);
if (selectionIndex == i) {
match = FontColor.getColored(match, ConsoleColors.COMMAND);
}
matchMessageString.append(match);
}
Message matchMessage = new Message(matchMessageString.toString());
String suggestion = previousMatches.get(selectionIndex);
if (previousMessage != null) {
console.replaceMessage(previousMessage, matchMessage);
} else {
console.addMessage(matchMessage);
}
previousMessage = matchMessage;
selectionIndex = (selectionIndex + 1) % previousMatches.size();
return generateResult(suggestion, commandName, commandParameters, suggestedIndex);
}
use of org.terasology.logic.console.commandSystem.ConsoleCommand 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;
}
}
}
use of org.terasology.logic.console.commandSystem.ConsoleCommand in project Terasology by MovingBlocks.
the class CoreCommands method help.
/**
* Prints out short descriptions for all available commands, or a longer help text if a command is provided.
* @param commandName String containing command for which will be displayed help
* @return String containing short description of all commands or longer help text if command is provided
*/
@Command(shortDescription = "Prints out short descriptions for all available commands, or a longer help text if a command is provided.", requiredPermission = PermissionManager.NO_PERMISSION)
public String help(@CommandParam(value = "command", required = false, suggester = CommandNameSuggester.class) Name commandName) {
if (commandName == null) {
StringBuilder msg = new StringBuilder();
// Get all commands, with appropriate sorting
List<ConsoleCommand> commands = Ordering.natural().immutableSortedCopy(console.getCommands());
for (ConsoleCommand cmd : commands) {
if (!msg.toString().isEmpty()) {
msg.append(Console.NEW_LINE);
}
msg.append(FontColor.getColored(cmd.getUsage(), ConsoleColors.COMMAND));
msg.append(" - ");
msg.append(cmd.getDescription());
}
return msg.toString();
} else {
ConsoleCommand cmd = console.getCommand(commandName);
if (cmd == null) {
return "No help available for command '" + commandName + "'. Unknown command.";
} else {
StringBuilder msg = new StringBuilder();
msg.append("=====================================================================================================================");
msg.append(Console.NEW_LINE);
msg.append(cmd.getUsage());
msg.append(Console.NEW_LINE);
msg.append("=====================================================================================================================");
msg.append(Console.NEW_LINE);
if (!cmd.getHelpText().isEmpty()) {
msg.append(cmd.getHelpText());
msg.append(Console.NEW_LINE);
msg.append("=====================================================================================================================");
msg.append(Console.NEW_LINE);
} else if (!cmd.getDescription().isEmpty()) {
msg.append(cmd.getDescription());
msg.append(Console.NEW_LINE);
msg.append("=====================================================================================================================");
msg.append(Console.NEW_LINE);
}
if (!cmd.getRequiredPermission().isEmpty()) {
msg.append("Required permission level - " + cmd.getRequiredPermission());
msg.append(Console.NEW_LINE);
msg.append("=====================================================================================================================");
msg.append(Console.NEW_LINE);
}
return msg.toString();
}
}
}
use of org.terasology.logic.console.commandSystem.ConsoleCommand in project Terasology by MovingBlocks.
the class CommandNameSuggester method suggest.
@Override
public Set<Name> suggest(EntityRef sender, Object... resolvedParameters) {
Collection<ConsoleCommand> commands = console.getCommands();
Set<Name> suggestions = Sets.newHashSetWithExpectedSize(commands.size());
for (ConsoleCommand command : commands) {
suggestions.add(command.getName());
}
return suggestions;
}
Aggregations