Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ListCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.List;
4 
5 import org.apache.commons.lang.Validate;
6 import org.bukkit.Bukkit;
8 import org.bukkit.entity.Player;
9 
10 import com.google.common.collect.ImmutableList;
11 
12 public class ListCommand extends VanillaCommand {
13  public ListCommand() {
14  super("list");
15  this.description = "Lists all online players";
16  this.usageMessage = "/list";
17  this.setPermission("bukkit.command.list");
18  }
19 
20  @Override
21  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
22  if (!testPermission(sender)) return true;
23 
24  StringBuilder online = new StringBuilder();
25 
26  Player[] players = Bukkit.getOnlinePlayers();
27 
28  for (Player player : players) {
29  // If a player is hidden from the sender don't show them in the list
30  if (sender instanceof Player && !((Player) sender).canSee(player))
31  continue;
32 
33  if (online.length() > 0) {
34  online.append(", ");
35  }
36 
37  online.append(player.getDisplayName());
38  }
39 
40  sender.sendMessage("There are " + players.length + "/" + Bukkit.getMaxPlayers() + " players online:\n" + online.toString());
41 
42  return true;
43  }
44 
45  @Override
46  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
47  Validate.notNull(sender, "Sender cannot be null");
48  Validate.notNull(args, "Arguments cannot be null");
49  Validate.notNull(alias, "Alias cannot be null");
50 
51  return ImmutableList.of();
52  }
53 }
boolean testPermission(CommandSender target)
static int getMaxPlayers()
Definition: Bukkit.java:101
void sendMessage(String message)
void setPermission(String permission)
static Player[] getOnlinePlayers()
Definition: Bukkit.java:94