Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
WhitelistCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import org.apache.commons.lang.Validate;
7 import org.bukkit.Bukkit;
8 import org.bukkit.ChatColor;
10 import org.bukkit.command.Command;
12 import org.bukkit.util.StringUtil;
13 
14 import com.google.common.collect.ImmutableList;
15 
16 public class WhitelistCommand extends VanillaCommand {
17  private static final List<String> WHITELIST_SUBCOMMANDS = ImmutableList.of("add", "remove", "on", "off", "list", "reload");
18 
19  public WhitelistCommand() {
20  super("whitelist");
21  this.description = "Manages the list of players allowed to use this server";
22  this.usageMessage = "/whitelist (add|remove) <player>\n/whitelist (on|off|list|reload)";
23  this.setPermission("bukkit.command.whitelist.reload;bukkit.command.whitelist.enable;bukkit.command.whitelist.disable;bukkit.command.whitelist.list;bukkit.command.whitelist.add;bukkit.command.whitelist.remove");
24  }
25 
26  @Override
27  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
28  if (!testPermission(sender)) return true;
29 
30  if (args.length == 1) {
31  if (args[0].equalsIgnoreCase("reload")) {
32  if (badPerm(sender, "reload")) return true;
33 
35  Command.broadcastCommandMessage(sender, "Reloaded white-list from file");
36  return true;
37  } else if (args[0].equalsIgnoreCase("on")) {
38  if (badPerm(sender, "enable")) return true;
39 
40  Bukkit.setWhitelist(true);
41  Command.broadcastCommandMessage(sender, "Turned on white-listing");
42  return true;
43  } else if (args[0].equalsIgnoreCase("off")) {
44  if (badPerm(sender, "disable")) return true;
45 
46  Bukkit.setWhitelist(false);
47  Command.broadcastCommandMessage(sender, "Turned off white-listing");
48  return true;
49  } else if (args[0].equalsIgnoreCase("list")) {
50  if (badPerm(sender, "list")) return true;
51 
52  StringBuilder result = new StringBuilder();
53 
54  for (OfflinePlayer player : Bukkit.getWhitelistedPlayers()) {
55  if (result.length() > 0) {
56  result.append(", ");
57  }
58 
59  result.append(player.getName());
60  }
61 
62  sender.sendMessage("White-listed players: " + result.toString());
63  return true;
64  }
65  } else if (args.length == 2) {
66  if (args[0].equalsIgnoreCase("add")) {
67  if (badPerm(sender, "add")) return true;
68 
69  Bukkit.getOfflinePlayer(args[1]).setWhitelisted(true);
70 
71  Command.broadcastCommandMessage(sender, "Added " + args[1] + " to white-list");
72  return true;
73  } else if (args[0].equalsIgnoreCase("remove")) {
74  if (badPerm(sender, "remove")) return true;
75 
76  Bukkit.getOfflinePlayer(args[1]).setWhitelisted(false);
77 
78  Command.broadcastCommandMessage(sender, "Removed " + args[1] + " from white-list");
79  return true;
80  }
81  }
82 
83  sender.sendMessage(ChatColor.RED + "Correct command usage:\n" + usageMessage);
84  return false;
85  }
86 
87  private boolean badPerm(CommandSender sender, String perm) {
88  if (!sender.hasPermission("bukkit.command.whitelist." + perm)) {
89  sender.sendMessage(ChatColor.RED + "You do not have permission to perform this action.");
90  return true;
91  }
92 
93  return false;
94  }
95 
96  @Override
97  public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
98  Validate.notNull(sender, "Sender cannot be null");
99  Validate.notNull(args, "Arguments cannot be null");
100  Validate.notNull(alias, "Alias cannot be null");
101 
102  if (args.length == 1) {
103  return StringUtil.copyPartialMatches(args[0], WHITELIST_SUBCOMMANDS, new ArrayList<String>(WHITELIST_SUBCOMMANDS.size()));
104  } else if (args.length == 2) {
105  if (args[0].equalsIgnoreCase("add")) {
106  List<String> completions = new ArrayList<String>();
107  for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
108  String name = player.getName();
109  if (StringUtil.startsWithIgnoreCase(name, args[1]) && !player.isWhitelisted()) {
110  completions.add(name);
111  }
112  }
113  return completions;
114  } else if (args[0].equalsIgnoreCase("remove")) {
115  List<String> completions = new ArrayList<String>();
116  for (OfflinePlayer player : Bukkit.getWhitelistedPlayers()) {
117  String name = player.getName();
118  if (StringUtil.startsWithIgnoreCase(name, args[1])) {
119  completions.add(name);
120  }
121  }
122  return completions;
123  }
124  }
125  return ImmutableList.of();
126  }
127 }
boolean testPermission(CommandSender target)
void setWhitelisted(boolean value)
static Set< OfflinePlayer > getWhitelistedPlayers()
Definition: Bukkit.java:485
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
static void setWhitelist(boolean value)
Definition: Bukkit.java:478
boolean hasPermission(String name)
static void reloadWhitelist()
Definition: Bukkit.java:492
void sendMessage(String message)
void setPermission(String permission)
static OfflinePlayer[] getOfflinePlayers()
Definition: Bukkit.java:583
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50
static OfflinePlayer getOfflinePlayer(String name)
Definition: Bukkit.java:421