001 package org.bukkit.command.defaults; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import org.apache.commons.lang.Validate; 007 import org.bukkit.Bukkit; 008 import org.bukkit.ChatColor; 009 import org.bukkit.OfflinePlayer; 010 import org.bukkit.command.Command; 011 import org.bukkit.command.CommandSender; 012 import org.bukkit.util.StringUtil; 013 014 import com.google.common.collect.ImmutableList; 015 016 public class WhitelistCommand extends VanillaCommand { 017 private static final List<String> WHITELIST_SUBCOMMANDS = ImmutableList.of("add", "remove", "on", "off", "list", "reload"); 018 019 public WhitelistCommand() { 020 super("whitelist"); 021 this.description = "Manages the list of players allowed to use this server"; 022 this.usageMessage = "/whitelist (add|remove) <player>\n/whitelist (on|off|list|reload)"; 023 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"); 024 } 025 026 @Override 027 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 028 if (!testPermission(sender)) return true; 029 030 if (args.length == 1) { 031 if (args[0].equalsIgnoreCase("reload")) { 032 if (badPerm(sender, "reload")) return true; 033 034 Bukkit.reloadWhitelist(); 035 Command.broadcastCommandMessage(sender, "Reloaded white-list from file"); 036 return true; 037 } else if (args[0].equalsIgnoreCase("on")) { 038 if (badPerm(sender, "enable")) return true; 039 040 Bukkit.setWhitelist(true); 041 Command.broadcastCommandMessage(sender, "Turned on white-listing"); 042 return true; 043 } else if (args[0].equalsIgnoreCase("off")) { 044 if (badPerm(sender, "disable")) return true; 045 046 Bukkit.setWhitelist(false); 047 Command.broadcastCommandMessage(sender, "Turned off white-listing"); 048 return true; 049 } else if (args[0].equalsIgnoreCase("list")) { 050 if (badPerm(sender, "list")) return true; 051 052 StringBuilder result = new StringBuilder(); 053 054 for (OfflinePlayer player : Bukkit.getWhitelistedPlayers()) { 055 if (result.length() > 0) { 056 result.append(", "); 057 } 058 059 result.append(player.getName()); 060 } 061 062 sender.sendMessage("White-listed players: " + result.toString()); 063 return true; 064 } 065 } else if (args.length == 2) { 066 if (args[0].equalsIgnoreCase("add")) { 067 if (badPerm(sender, "add")) return true; 068 069 Bukkit.getOfflinePlayer(args[1]).setWhitelisted(true); 070 071 Command.broadcastCommandMessage(sender, "Added " + args[1] + " to white-list"); 072 return true; 073 } else if (args[0].equalsIgnoreCase("remove")) { 074 if (badPerm(sender, "remove")) return true; 075 076 Bukkit.getOfflinePlayer(args[1]).setWhitelisted(false); 077 078 Command.broadcastCommandMessage(sender, "Removed " + args[1] + " from white-list"); 079 return true; 080 } 081 } 082 083 sender.sendMessage(ChatColor.RED + "Correct command usage:\n" + usageMessage); 084 return false; 085 } 086 087 private boolean badPerm(CommandSender sender, String perm) { 088 if (!sender.hasPermission("bukkit.command.whitelist." + perm)) { 089 sender.sendMessage(ChatColor.RED + "You do not have permission to perform this action."); 090 return true; 091 } 092 093 return false; 094 } 095 096 @Override 097 public List<String> tabComplete(CommandSender sender, String alias, String[] args) { 098 Validate.notNull(sender, "Sender cannot be null"); 099 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 }