Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
OpCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 
7 import org.apache.commons.lang.Validate;
8 import org.bukkit.Bukkit;
9 import org.bukkit.ChatColor;
10 import org.bukkit.OfflinePlayer;
11 import org.bukkit.command.Command;
13 import org.bukkit.entity.Player;
14 import org.bukkit.util.StringUtil;
15 
16 import com.google.common.collect.ImmutableList;
17 
18 public class OpCommand extends VanillaCommand {
19  public OpCommand() {
20  super("op");
21  this.description = "Gives the specified player operator status";
22  this.usageMessage = "/op <player>";
23  this.setPermission("bukkit.command.op.give");
24  }
25 
26  @Override
27  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
28  if (!testPermission(sender)) return true;
29  if (args.length != 1 || args[0].length() == 0) {
30  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
31  return false;
32  }
33 
34  OfflinePlayer player = Bukkit.getOfflinePlayer(args[0]);
35  player.setOp(true);
36 
37  Command.broadcastCommandMessage(sender, "Opped " + args[0]);
38  return true;
39  }
40 
41  @Override
42  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
43  Validate.notNull(sender, "Sender cannot be null");
44  Validate.notNull(args, "Arguments cannot be null");
45  Validate.notNull(alias, "Alias cannot be null");
46 
47  if (args.length == 1) {
48  if (!(sender instanceof Player)) {
49  return ImmutableList.of();
50  }
51 
52  String lastWord = args[0];
53  if (lastWord.length() == 0) {
54  return ImmutableList.of();
55  }
56 
57  Player senderPlayer = (Player) sender;
58 
59  ArrayList<String> matchedPlayers = new ArrayList<String>();
60  for (Player player : sender.getServer().getOnlinePlayers()) {
61  String name = player.getName();
62  if (!senderPlayer.canSee(player) || player.isOp()) {
63  continue;
64  }
65  if (StringUtil.startsWithIgnoreCase(name, lastWord)) {
66  matchedPlayers.add(name);
67  }
68  }
69 
70  Collections.sort(matchedPlayers, String.CASE_INSENSITIVE_ORDER);
71  return matchedPlayers;
72  }
73  return ImmutableList.of();
74  }
75 }
boolean testPermission(CommandSender target)
void sendMessage(String message)
void setPermission(String permission)
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50
static OfflinePlayer getOfflinePlayer(String name)
Definition: Bukkit.java:421
Player[] getOnlinePlayers()
boolean canSee(Player player)