Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
GiveCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7 
8 import org.apache.commons.lang.Validate;
9 import org.bukkit.Bukkit;
10 import org.bukkit.ChatColor;
11 import org.bukkit.Material;
12 import org.bukkit.command.Command;
14 import org.bukkit.entity.Player;
16 import org.bukkit.util.StringUtil;
17 
18 import com.google.common.base.Joiner;
19 import com.google.common.collect.ImmutableList;
20 
21 public class GiveCommand extends VanillaCommand {
22  private static List<String> materials;
23  static {
24  ArrayList<String> materialList = new ArrayList<String>();
25  for (Material material : Material.values()) {
26  materialList.add(material.name());
27  }
28  Collections.sort(materialList);
29  materials = ImmutableList.copyOf(materialList);
30  }
31 
32  public GiveCommand() {
33  super("give");
34  this.description = "Gives the specified player a certain amount of items";
35  this.usageMessage = "/give <player> <item> [amount [data]]";
36  this.setPermission("bukkit.command.give");
37  }
38 
39  @Override
40  public boolean execute(CommandSender sender, String currentAlias, String[] args) {
41  if (!testPermission(sender)) return true;
42  if ((args.length < 2)) {
43  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
44  return false;
45  }
46 
47  Player player = Bukkit.getPlayerExact(args[0]);
48 
49  if (player != null) {
50  Material material = Material.matchMaterial(args[1]);
51 
52  if (material == null) {
53  material = Bukkit.getUnsafe().getMaterialFromInternalName(args[1]);
54  }
55 
56  if (material != null) {
57  int amount = 1;
58  short data = 0;
59 
60  if (args.length >= 3) {
61  amount = this.getInteger(sender, args[2], 1, 64);
62 
63  if (args.length >= 4) {
64  try {
65  data = Short.parseShort(args[3]);
66  } catch (NumberFormatException ex) {}
67  }
68  }
69 
70  ItemStack stack = new ItemStack(material, amount, data);
71 
72  if (args.length >= 5) {
73  try {
74  stack = Bukkit.getUnsafe().modifyItemStack(stack, Joiner.on(' ').join(Arrays.asList(args).subList(4, args.length)));
75  } catch (Throwable t) {
76  player.sendMessage("Not a valid tag");
77  return true;
78  }
79  }
80 
81  player.getInventory().addItem(stack);
82 
83  Command.broadcastCommandMessage(sender, "Gave " + player.getName() + " some " + material.getId() + " (" + material + ")");
84  } else {
85  sender.sendMessage("There's no item called " + args[1]);
86  }
87  } else {
88  sender.sendMessage("Can't find player " + args[0]);
89  }
90 
91  return true;
92  }
93 
94  @Override
95  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
96  Validate.notNull(sender, "Sender cannot be null");
97  Validate.notNull(args, "Arguments cannot be null");
98  Validate.notNull(alias, "Alias cannot be null");
99 
100  if (args.length == 1) {
101  return super.tabComplete(sender, alias, args);
102  }
103  if (args.length == 2) {
104  final String arg = args[1];
105  final List<String> materials = GiveCommand.materials;
106  List<String> completion = new ArrayList<String>();
107 
108  final int size = materials.size();
109  int i = Collections.binarySearch(materials, arg, String.CASE_INSENSITIVE_ORDER);
110 
111  if (i < 0) {
112  // Insertion (start) index
113  i = -1 - i;
114  }
115 
116  for ( ; i < size; i++) {
117  String material = materials.get(i);
118  if (StringUtil.startsWithIgnoreCase(material, arg)) {
119  completion.add(material);
120  } else {
121  break;
122  }
123  }
124 
125  return Bukkit.getUnsafe().tabCompleteInternalMaterialName(arg, completion);
126  }
127  return ImmutableList.of();
128  }
129 }
boolean testPermission(CommandSender target)
PlayerInventory getInventory()
HashMap< Integer, ItemStack > addItem(ItemStack...items)
void sendMessage(String message)
void setPermission(String permission)
static Material matchMaterial(final String name)
Definition: Material.java:608
static UnsafeValues getUnsafe()
Definition: Bukkit.java:732
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50
static Player getPlayerExact(String name)
Definition: Bukkit.java:436