Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
VersionCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.List;
6 
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.Bukkit;
9 import org.bukkit.ChatColor;
11 import org.bukkit.plugin.Plugin;
13 import org.bukkit.util.StringUtil;
14 
15 import com.google.common.collect.ImmutableList;
16 
17 public class VersionCommand extends BukkitCommand {
18  public VersionCommand(String name) {
19  super(name);
20 
21  this.description = "Gets the version of this server including any plugins in use";
22  this.usageMessage = "/version [plugin name]";
23  this.setPermission("bukkit.command.version");
24  this.setAliases(Arrays.asList("ver", "about"));
25  }
26 
27  @Override
28  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
29  if (!testPermission(sender)) return true;
30 
31  if (args.length == 0) {
32  sender.sendMessage("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")");
33  } else {
34  StringBuilder name = new StringBuilder();
35 
36  for (String arg : args) {
37  if (name.length() > 0) {
38  name.append(' ');
39  }
40 
41  name.append(arg);
42  }
43 
44  String pluginName = name.toString();
45  Plugin exactPlugin = Bukkit.getPluginManager().getPlugin(pluginName);
46  if (exactPlugin != null) {
47  describeToSender(exactPlugin, sender);
48  return true;
49  }
50 
51  boolean found = false;
52  pluginName = pluginName.toLowerCase();
53  for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
54  if (plugin.getName().toLowerCase().contains(pluginName)) {
55  describeToSender(plugin, sender);
56  found = true;
57  }
58  }
59 
60  if (!found) {
61  sender.sendMessage("This server is not running any plugin by that name.");
62  sender.sendMessage("Use /plugins to get a list of plugins.");
63  }
64  }
65  return true;
66  }
67 
68  private void describeToSender(Plugin plugin, CommandSender sender) {
69  PluginDescriptionFile desc = plugin.getDescription();
70  sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
71 
72  if (desc.getDescription() != null) {
73  sender.sendMessage(desc.getDescription());
74  }
75 
76  if (desc.getWebsite() != null) {
77  sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
78  }
79 
80  if (!desc.getAuthors().isEmpty()) {
81  if (desc.getAuthors().size() == 1) {
82  sender.sendMessage("Author: " + getAuthors(desc));
83  } else {
84  sender.sendMessage("Authors: " + getAuthors(desc));
85  }
86  }
87  }
88 
89  private String getAuthors(final PluginDescriptionFile desc) {
90  StringBuilder result = new StringBuilder();
91  List<String> authors = desc.getAuthors();
92 
93  for (int i = 0; i < authors.size(); i++) {
94  if (result.length() > 0) {
95  result.append(ChatColor.WHITE);
96 
97  if (i < authors.size() - 1) {
98  result.append(", ");
99  } 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 }
boolean testPermission(CommandSender target)
Plugin getPlugin(String name)
Command setAliases(List< String > aliases)
void sendMessage(String message)
void setPermission(String permission)
PluginDescriptionFile getDescription()
static PluginManager getPluginManager()
Definition: Bukkit.java:208
static String getBukkitVersion()
Definition: Bukkit.java:87
static String getName()
Definition: Bukkit.java:73
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50
static String getVersion()
Definition: Bukkit.java:80