Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
ClearCommand.java
1 package org.bukkit.command.defaults;
2 
3 import com.google.common.collect.ImmutableList;
4 import org.apache.commons.lang.Validate;
5 import org.bukkit.Bukkit;
6 import org.bukkit.ChatColor;
7 import org.bukkit.Material;
10 import org.bukkit.entity.Player;
11 import org.bukkit.util.StringUtil;
12 
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 
17 public class ClearCommand extends VanillaCommand {
18  private static List<String> materials;
19  static {
20  ArrayList<String> materialList = new ArrayList<String>();
21  for (Material material : Material.values()) {
22  materialList.add(material.name());
23  }
24  Collections.sort(materialList);
25  materials = ImmutableList.copyOf(materialList);
26  }
27 
28  public ClearCommand() {
29  super("clear");
30  this.description = "Clears the player's inventory. Can specify item and data filters too.";
31  this.usageMessage = "/clear <player> [item] [data]";
32  this.setPermission("bukkit.command.clear");
33  }
34 
35  @Override
36  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
37  if (!testPermission(sender)) return true;
38 
39  Player player = null;
40  if (args.length > 0) {
41  player = Bukkit.getPlayer(args[0]);
42  } else if (sender instanceof Player) {
43  player = (Player) sender;
44  }
45 
46  if (player != null) {
47  int id;
48 
49  if (args.length > 1 && !(args[1].equals("-1"))) {
50  Material material = Material.matchMaterial(args[1]);
51  if (material == null) {
52  sender.sendMessage(ChatColor.RED + "There's no item called " + args[1]);
53  return false;
54  }
55 
56  id = material.getId();
57  } else {
58  id = -1;
59  }
60 
61  int data = args.length >= 3 ? getInteger(sender, args[2], 0) : -1;
62  int count = player.getInventory().clear(id, data);
63 
64  Command.broadcastCommandMessage(sender, "Cleared the inventory of " + player.getDisplayName() + ", removing " + count + " items");
65  } else if (args.length == 0) {
66  sender.sendMessage(ChatColor.RED + "Please provide a player!");
67  } else {
68  sender.sendMessage(ChatColor.RED + "Can't find player " + args[0]);
69  }
70 
71  return true;
72  }
73 
74  @Override
75  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
76  Validate.notNull(sender, "Sender cannot be null");
77  Validate.notNull(args, "Arguments cannot be null");
78  Validate.notNull(alias, "Alias cannot be null");
79 
80  if (args.length == 1) {
81  return super.tabComplete(sender, alias, args);
82  }
83  if (args.length == 2) {
84  final String arg = args[1];
85  final List<String> materials = ClearCommand.materials;
86  List<String> completion = null;
87 
88  final int size = materials.size();
89  int i = Collections.binarySearch(materials, arg, String.CASE_INSENSITIVE_ORDER);
90 
91  if (i < 0) {
92  // Insertion (start) index
93  i = -1 - i;
94  }
95 
96  for ( ; i < size; i++) {
97  String material = materials.get(i);
98  if (StringUtil.startsWithIgnoreCase(material, arg)) {
99  if (completion == null) {
100  completion = new ArrayList<String>();
101  }
102  completion.add(material);
103  } else {
104  break;
105  }
106  }
107 
108  if (completion != null) {
109  return completion;
110  }
111  }
112  return ImmutableList.of();
113  }
114 }
boolean testPermission(CommandSender target)
PlayerInventory getInventory()
void sendMessage(String message)
void setPermission(String permission)
static Material matchMaterial(final String name)
Definition: Material.java:608
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50
static Player getPlayer(String name)
Definition: Bukkit.java:186