Bukkit/src/main/java/org/bukkit/command/defaults/VersionCommand.java
Score_Under 9fd9767a4a Add tab-completion API. Fixes BUKKIT-2181. Adds BUKKIT-2602
CommandMap contains a method that will auto-complete commands
appropriately. Before the first space, it searches for commands of which
the sender has permission. After the first space, it delegates to the
individual command.

Vanilla commands contain implementations to mimic vanilla
implementation. Exception would be give, that allows for name matching;
a feature we already allowed as part of the command is now supported for
auto-complete as well.

Plugin commands can get a tab completer set to delegate the completion
for. If no tab completer is set, it can check the executor to see if it
implements the tab completion interface. It will also attempt to chain
calls if null gets returned from these interfaces. Plugins also
implement the new TabCompleter interface, to add ease-of-use for plugin
developers, similar to the onCommand() method.

The default command implementation simply searches for player names.

To help facilitate command completion, a utility class was added with
two functions. One checks two strings, to see if the specified string
starts with (ignoring case) the second. The other method uses the first
to selectively copy elements from one collection to another.
2012-10-16 00:05:40 -05:00

130 lines
4.4 KiB
Java

package org.bukkit.command.defaults;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.util.StringUtil;
import com.google.common.collect.ImmutableList;
public class VersionCommand extends BukkitCommand {
public VersionCommand(String name) {
super(name);
this.description = "Gets the version of this server including any plugins in use";
this.usageMessage = "/version [plugin name]";
this.setPermission("bukkit.command.version");
this.setAliases(Arrays.asList("ver", "about"));
}
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender)) return true;
if (args.length == 0) {
sender.sendMessage("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")");
} else {
StringBuilder name = new StringBuilder();
for (String arg : args) {
if (name.length() > 0) {
name.append(' ');
}
name.append(arg);
}
String pluginName = name.toString();
Plugin exactPlugin = Bukkit.getPluginManager().getPlugin(pluginName);
if (exactPlugin != null) {
describeToSender(exactPlugin, sender);
return true;
}
boolean found = false;
pluginName = pluginName.toLowerCase();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (plugin.getName().toLowerCase().contains(pluginName)) {
describeToSender(plugin, sender);
found = true;
}
}
if (!found) {
sender.sendMessage("This server is not running any plugin by that name.");
sender.sendMessage("Use /plugins to get a list of plugins.");
}
}
return true;
}
private void describeToSender(Plugin plugin, CommandSender sender) {
PluginDescriptionFile desc = plugin.getDescription();
sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
if (desc.getDescription() != null) {
sender.sendMessage(desc.getDescription());
}
if (desc.getWebsite() != null) {
sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
}
if (!desc.getAuthors().isEmpty()) {
if (desc.getAuthors().size() == 1) {
sender.sendMessage("Author: " + getAuthors(desc));
} else {
sender.sendMessage("Authors: " + getAuthors(desc));
}
}
}
private String getAuthors(final PluginDescriptionFile desc) {
StringBuilder result = new StringBuilder();
List<String> authors = desc.getAuthors();
for (int i = 0; i < authors.size(); i++) {
if (result.length() > 0) {
result.append(ChatColor.WHITE);
if (i < authors.size() - 1) {
result.append(", ");
} else {
result.append(" and ");
}
}
result.append(ChatColor.GREEN);
result.append(authors.get(i));
}
return result.toString();
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
Validate.notNull(sender, "Sender cannot be null");
Validate.notNull(args, "Arguments cannot be null");
Validate.notNull(alias, "Alias cannot be null");
if (args.length == 1) {
List<String> completions = new ArrayList<String>();
String toComplete = args[0].toLowerCase();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (StringUtil.startsWithIgnoreCase(plugin.getName(), toComplete)) {
completions.add(plugin.getName());
}
}
return completions;
}
return ImmutableList.of();
}
}