}
}
public void execute(Player player, String message) {
String[] parts = message.trim().split(" ");
Slot item;
if (parts.length == 1) {
if ((item = getSessionItem(player, false)) == null) {
player.addTMessage(Color.GRAY, "You have to select an item to enchant first.");
player.addTMessage(Color.GRAY, "Use: %s%s ITEM", commandPrefix(), name);
} else {
chatItem(player, item);
}
} else if (parts[1].equals("add")) {
if ((item = getSessionItem(player)) == null) {
return;
}
boolean changed = false;
for (int i = 2; i < parts.length; i++) {
String[] ench = parts[i].split(":");
try {
Integer id = Integer.valueOf(ench[0]);
int level = 10;
if (ench.length >= 2) {
level = Integer.valueOf(ench[1]);
if (level < 1 || level > 10) {
player.addTMessage(Color.RED, "The enchantment level must be between 1 and 10");
continue;
}
}
if (!availableEnchantments(item).contains(id)) {
if (ENCHANTMENTS.containsKey(Integer.valueOf(id))) {
player.addTMessage(Color.RED, "%s is not available for the current item", ENCHANTMENTS.get(id));
} else {
player.addTMessage(Color.RED, "No enchantment with id %s exists.", id);
}
} else {
item.addEnchantment(new Enchantment(id, level));
changed = true;
}
} catch (NumberFormatException e) {
player.addTMessage(Color.RED, "%s is not a valid enchantment", parts[i]);
}
}
if (changed) {
chatItem(player, item);
}
} else if (parts[1].equals("remove")) {
if ((item = getSessionItem(player)) == null) {
return;
}
boolean changed = false;
for (int i = 2; i < parts.length; i++) {
try {
Integer id = Integer.valueOf(parts[i]);
if (!item.enchantedWith(id)) {
player.addTMessage(Color.RED, "The item was not enchanted with %s", (ENCHANTMENTS.containsKey(id) ? ENCHANTMENTS.get(id) : id));
} else {
item.removeEnchantment(id);
changed = true;
}
} catch (NumberFormatException e) {
player.addTMessage(Color.RED, "%s is not a valid enchantment", parts[i]);
}
}
if (changed) {
chatItem(player, item);
}
} else if (parts[1].equals("spawn") || parts[1].equals("give")) {
if ((item = getSessionItem(player)) == null) {
return;
}
Giver bot = new Giver(player);
bot.add(item);
try {
player.getServer().bots.connect(bot);
} catch (ConnectException e) {
player.addTMessage(Color.RED, "An unknown error occured");
}
} else {
int id = 0;
if (parts.length == 2) {
try {
id = Integer.valueOf(parts[1]);
} catch (NumberFormatException e) {
GiveAliasList alias = player.getServer().giveAliasList;
Item itemAlias = alias.getItemId(parts[1]);
if (itemAlias == null) {
Suggestion correctName = alias.findWithLevenshtein(parts[1]);
if (correctName.distance < 4) {
id = alias.getItemId(correctName.name).id;
}
} else {
id = itemAlias.id;
}
}
} else {
String name = WordUtils.capitalize(extractArgument(message));
if (ITEMS.containsValue(name)) {
for (Integer itemID : ITEMS.keySet()) {
if (ITEMS.get(itemID).equals(name)) {
id = itemID;
break;
}
}
}
}
if (id == 0) {
player.addTMessage(Color.RED, "Can't find item");
} else if (!ITEMS.containsKey(id)) {
player.addTMessage(Color.RED, "This item is not enchantable");
} else {
item = new Slot(id);
chatItem(player, item);
player.addTMessage(Color.GRAY, "You can now add enchantments with %s%s%s", commandPrefix(), name, " add ID:LEVEL");
sessions.put(player, item);
}
}