001 package org.bukkit.command.defaults;
002
003 import java.util.ArrayList;
004 import java.util.Arrays;
005 import java.util.List;
006
007 import org.apache.commons.lang.Validate;
008 import org.bukkit.Bukkit;
009 import org.bukkit.ChatColor;
010 import org.bukkit.command.CommandSender;
011 import org.bukkit.plugin.Plugin;
012 import org.bukkit.plugin.PluginDescriptionFile;
013 import org.bukkit.util.StringUtil;
014
015 import com.google.common.collect.ImmutableList;
016
017 public class VersionCommand extends BukkitCommand {
018 public VersionCommand(String name) {
019 super(name);
020
021 this.description = "Gets the version of this server including any plugins in use";
022 this.usageMessage = "/version [plugin name]";
023 this.setPermission("bukkit.command.version");
024 this.setAliases(Arrays.asList("ver", "about"));
025 }
026
027 @Override
028 public boolean execute(CommandSender sender, String currentAlias, String[] args) {
029 if (!testPermission(sender)) return true;
030
031 if (args.length == 0) {
032 sender.sendMessage("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")");
033 } else {
034 StringBuilder name = new StringBuilder();
035
036 for (String arg : args) {
037 if (name.length() > 0) {
038 name.append(' ');
039 }
040
041 name.append(arg);
042 }
043
044 String pluginName = name.toString();
045 Plugin exactPlugin = Bukkit.getPluginManager().getPlugin(pluginName);
046 if (exactPlugin != null) {
047 describeToSender(exactPlugin, sender);
048 return true;
049 }
050
051 boolean found = false;
052 pluginName = pluginName.toLowerCase();
053 for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
054 if (plugin.getName().toLowerCase().contains(pluginName)) {
055 describeToSender(plugin, sender);
056 found = true;
057 }
058 }
059
060 if (!found) {
061 sender.sendMessage("This server is not running any plugin by that name.");
062 sender.sendMessage("Use /plugins to get a list of plugins.");
063 }
064 }
065 return true;
066 }
067
068 private void describeToSender(Plugin plugin, CommandSender sender) {
069 PluginDescriptionFile desc = plugin.getDescription();
070 sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
071
072 if (desc.getDescription() != null) {
073 sender.sendMessage(desc.getDescription());
074 }
075
076 if (desc.getWebsite() != null) {
077 sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
078 }
079
080 if (!desc.getAuthors().isEmpty()) {
081 if (desc.getAuthors().size() == 1) {
082 sender.sendMessage("Author: " + getAuthors(desc));
083 } else {
084 sender.sendMessage("Authors: " + getAuthors(desc));
085 }
086 }
087 }
088
089 private String getAuthors(final PluginDescriptionFile desc) {
090 StringBuilder result = new StringBuilder();
091 List<String> authors = desc.getAuthors();
092
093 for (int i = 0; i < authors.size(); i++) {
094 if (result.length() > 0) {
095 result.append(ChatColor.WHITE);
096
097 if (i < authors.size() - 1) {
098 result.append(", ");
099 } else {
100 result.append(" and ");
101 }
102 }
103
104 result.append(ChatColor.GREEN);
105 result.append(authors.get(i));
106 }
107
108 return result.toString();
109 }
110
111 @Override
112 public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
113 Validate.notNull(sender, "Sender cannot be null");
114 Validate.notNull(args, "Arguments cannot be null");
115 Validate.notNull(alias, "Alias cannot be null");
116
117 if (args.length == 1) {
118 List<String> completions = new ArrayList<String>();
119 String toComplete = args[0].toLowerCase();
120 for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
121 if (StringUtil.startsWithIgnoreCase(plugin.getName(), toComplete)) {
122 completions.add(plugin.getName());
123 }
124 }
125 return completions;
126 }
127 return ImmutableList.of();
128 }
129 }