Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
EnchantCommand.java
1 package org.bukkit.command.defaults;
2 
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6 import java.util.Map;
7 
8 import org.apache.commons.lang.Validate;
9 import org.apache.commons.lang.WordUtils;
10 import com.google.common.collect.ImmutableList;
11 
12 import org.bukkit.Bukkit;
13 import org.bukkit.ChatColor;
14 import org.bukkit.Material;
15 import org.bukkit.command.Command;
18 import org.bukkit.entity.Player;
20 import org.bukkit.util.StringUtil;
21 
22 public class EnchantCommand extends VanillaCommand {
23  private static final List<String> ENCHANTMENT_NAMES = new ArrayList<String>();
24 
25  public EnchantCommand() {
26  super("enchant");
27  this.description = "Adds enchantments to the item the player is currently holding. Specify 0 for the level to remove an enchantment. Specify force to ignore normal enchantment restrictions";
28  this.usageMessage = "/enchant <player> <enchantment> [level|max|0] [force]";
29  this.setPermission("bukkit.command.enchant");
30  }
31 
32  @Override
33  public boolean execute(CommandSender sender, String commandLabel, String[] args) {
34  if (!testPermission(sender)) return true;
35  if (args.length < 2) {
36  sender.sendMessage(ChatColor.RED + "Usage: " + usageMessage);
37  return false;
38  }
39 
40  boolean force = false;
41  if (args.length > 2) {
42  force = args[args.length > 3 ? 3 : 2].equalsIgnoreCase("force");
43  }
44 
45  Player player = Bukkit.getPlayerExact(args[0]);
46  if (player == null) {
47  sender.sendMessage("Can't find player " + args[0]);
48  } else {
49  ItemStack item = player.getItemInHand();
50  if (item.getType() == Material.AIR) {
51  sender.sendMessage("The player isn't holding an item");
52  } else {
53  String itemName = item.getType().toString().replaceAll("_", " ");
54  itemName = WordUtils.capitalizeFully(itemName);
55 
56  Enchantment enchantment = getEnchantment(args[1].toUpperCase());
57  if (enchantment == null) {
58  sender.sendMessage(String.format("Enchantment does not exist: %s", args[1]));
59  } else {
60  String enchantmentName = enchantment.getName().replaceAll("_", " ");
61  enchantmentName = WordUtils.capitalizeFully(enchantmentName);
62 
63  if (!force && !enchantment.canEnchantItem(item)) {
64  sender.sendMessage(String.format("%s cannot be applied to %s", enchantmentName, itemName));
65  } else {
66  int level = 1;
67  if (args.length > 2) {
68  Integer integer = getInteger(args[2]);
69  int minLevel = enchantment.getStartLevel();
70  int maxLevel = force ? Short.MAX_VALUE : enchantment.getMaxLevel();
71 
72  if (integer != null) {
73  if (integer == 0) {
74  item.removeEnchantment(enchantment);
75  Command.broadcastCommandMessage(sender, String.format("Removed %s on %s's %s", enchantmentName, player.getName(), itemName));
76  return true;
77  }
78 
79  if (integer < minLevel || integer > maxLevel) {
80  sender.sendMessage(String.format("Level for enchantment %s must be between %d and %d", enchantmentName, minLevel, maxLevel));
81  sender.sendMessage("Specify 0 for level to remove an enchantment");
82  return true;
83  }
84 
85  level = integer;
86  }
87 
88  if ("max".equals(args[2])) {
89  level = maxLevel;
90  }
91  }
92 
93  Map<Enchantment, Integer> enchantments = item.getEnchantments();
94  boolean conflicts = false;
95 
96  if (!force && !enchantments.isEmpty()) { // TODO: Improve this to use a "hasEnchantments" call
97  for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
98  Enchantment enchant = entry.getKey();
99 
100  if (enchant.equals(enchantment)) continue;
101  if (enchant.conflictsWith(enchantment)) {
102  sender.sendMessage(String.format("Can't apply the enchantment %s on an item with the enchantment %s", enchantmentName, WordUtils.capitalizeFully(enchant.getName().replaceAll("_", " "))));
103  conflicts = true;
104  break;
105  }
106  }
107  }
108 
109  if (!conflicts) {
110  item.addUnsafeEnchantment(enchantment, level);
111 
112  Command.broadcastCommandMessage(sender, String.format("Applied %s (Lvl %d) on %s's %s", enchantmentName, level, player.getName(), itemName), false);
113  sender.sendMessage(String.format("Enchanting succeeded, applied %s (Lvl %d) onto your %s", enchantmentName, level, itemName));
114  }
115  }
116  }
117  }
118  }
119  return true;
120  }
121 
122  @Override
123  public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
124  Validate.notNull(sender, "Sender cannot be null");
125  Validate.notNull(args, "Arguments cannot be null");
126  Validate.notNull(alias, "Alias cannot be null");
127 
128  if (args.length == 1) {
129  return super.tabComplete(sender, alias, args);
130  }
131 
132  if (args.length == 2) {
133  return StringUtil.copyPartialMatches(args[1], ENCHANTMENT_NAMES, new ArrayList<String>(ENCHANTMENT_NAMES.size()));
134  }
135 
136  if (args.length == 3 || args.length == 4) {
137  if (!args[args.length - 2].equalsIgnoreCase("force")) {
138  return ImmutableList.of("force");
139  }
140  }
141 
142  return ImmutableList.of();
143  }
144 
145  private Enchantment getEnchantment(String lookup) {
146  Enchantment enchantment = Enchantment.getByName(lookup);
147 
148  if (enchantment == null) {
149  Integer id = getInteger(lookup);
150  if (id != null) {
151  enchantment = Enchantment.getById(id);
152  }
153  }
154 
155  return enchantment;
156  }
157 
158  public static void buildEnchantments() {
159  if (!ENCHANTMENT_NAMES.isEmpty()) {
160  throw new IllegalStateException("Enchantments have already been built!");
161  }
162 
163  for (Enchantment enchantment : Enchantment.values()) {
164  ENCHANTMENT_NAMES.add(enchantment.getName());
165  }
166 
167  Collections.sort(ENCHANTMENT_NAMES);
168  }
169 }
boolean testPermission(CommandSender target)
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
abstract boolean canEnchantItem(ItemStack item)
void sendMessage(String message)
void setPermission(String permission)
static Enchantment getByName(String name)
int removeEnchantment(Enchantment ench)
Definition: ItemStack.java:477
void addUnsafeEnchantment(Enchantment ench, int level)
Definition: ItemStack.java:466
Map< Enchantment, Integer > getEnchantments()
Definition: ItemStack.java:390
static Enchantment getById(int id)
static Player getPlayerExact(String name)
Definition: Bukkit.java:436
abstract boolean conflictsWith(Enchantment other)