sender.sendMessage("The player isn't holding an item");
} else {
String itemName = item.getType().toString().replaceAll("_", " ");
itemName = WordUtils.capitalizeFully(itemName);
Enchantment enchantment = getEnchantment(args[1].toUpperCase());
if (enchantment == null) {
sender.sendMessage(String.format("Enchantment does not exist: %s", args[1]));
} else {
String enchantmentName = enchantment.getName().replaceAll("_", " ");
enchantmentName = WordUtils.capitalizeFully(enchantmentName);
if (!force && !enchantment.canEnchantItem(item)) {
sender.sendMessage(String.format("%s cannot be applied to %s", enchantmentName, itemName));
} else {
int level = 1;
if (args.length > 2) {
Integer integer = getInteger(args[2]);
int minLevel = enchantment.getStartLevel();
int maxLevel = force ? Short.MAX_VALUE : enchantment.getMaxLevel();
if (integer != null) {
if (integer == 0) {
item.removeEnchantment(enchantment);
Command.broadcastCommandMessage(sender, String.format("Removed %s on %s's %s", enchantmentName, player.getName(), itemName));
return true;
}
if (integer < minLevel || integer > maxLevel) {
sender.sendMessage(String.format("Level for enchantment %s must be between %d and %d", enchantmentName, minLevel, maxLevel));
sender.sendMessage("Specify 0 for level to remove an enchantment");
return true;
}
level = integer;
}
if ("max".equals(args[2])) {
level = maxLevel;
}
}
Map<Enchantment, Integer> enchantments = item.getEnchantments();
boolean conflicts = false;
if (!force && !enchantments.isEmpty()) { // TODO: Improve this to use a "hasEnchantments" call
for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
Enchantment enchant = entry.getKey();
if (enchant.equals(enchantment)) continue;
if (enchant.conflictsWith(enchantment)) {
sender.sendMessage(String.format("Can't apply the enchantment %s on an item with the enchantment %s", enchantmentName, WordUtils.capitalizeFully(enchant.getName().replaceAll("_", " "))));
conflicts = true;
break;
}
}
}