001 package org.bukkit.command.defaults; 002 003 import java.util.ArrayList; 004 import java.util.Collections; 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.OfflinePlayer; 011 import org.bukkit.command.Command; 012 import org.bukkit.command.CommandSender; 013 import org.bukkit.entity.Player; 014 import org.bukkit.util.StringUtil; 015 016 import com.google.common.collect.ImmutableList; 017 018 public class OpCommand extends VanillaCommand { 019 public OpCommand() { 020 super("op"); 021 this.description = "Gives the specified player operator status"; 022 this.usageMessage = "/op <player>"; 023 this.setPermission("bukkit.command.op.give"); 024 } 025 026 @Override 027 public boolean execute(CommandSender sender, String currentAlias, String[] args) { 028 if (!testPermission(sender)) return true; 029 if (args.length != 1 || args[0].length() == 0) { 030 sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage); 031 return false; 032 } 033 034 OfflinePlayer player = Bukkit.getOfflinePlayer(args[0]); 035 player.setOp(true); 036 037 Command.broadcastCommandMessage(sender, "Opped " + args[0]); 038 return true; 039 } 040 041 @Override 042 public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { 043 Validate.notNull(sender, "Sender cannot be null"); 044 Validate.notNull(args, "Arguments cannot be null"); 045 Validate.notNull(alias, "Alias cannot be null"); 046 047 if (args.length == 1) { 048 if (!(sender instanceof Player)) { 049 return ImmutableList.of(); 050 } 051 052 String lastWord = args[0]; 053 if (lastWord.length() == 0) { 054 return ImmutableList.of(); 055 } 056 057 Player senderPlayer = (Player) sender; 058 059 ArrayList<String> matchedPlayers = new ArrayList<String>(); 060 for (Player player : sender.getServer().getOnlinePlayers()) { 061 String name = player.getName(); 062 if (!senderPlayer.canSee(player) || player.isOp()) { 063 continue; 064 } 065 if (StringUtil.startsWithIgnoreCase(name, lastWord)) { 066 matchedPlayers.add(name); 067 } 068 } 069 070 Collections.sort(matchedPlayers, String.CASE_INSENSITIVE_ORDER); 071 return matchedPlayers; 072 } 073 return ImmutableList.of(); 074 } 075 }