TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java

86 lines
3.5 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.Enchantments;
2013-01-12 14:12:12 +00:00
import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.User;
2016-11-28 23:13:36 +00:00
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.StringUtil;
import com.google.common.collect.Lists;
2013-03-16 11:01:56 +00:00
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.enchantments.Enchantment;
2013-01-12 17:05:05 +00:00
import org.bukkit.inventory.ItemStack;
import java.util.*;
2015-04-15 04:06:16 +00:00
import static com.earth2me.essentials.I18n.tl;
public class Commandenchant extends EssentialsCommand {
public Commandenchant() {
super("enchant");
}
//TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess);
@Override
protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
final ItemStack stack = user.getItemInHand();
2015-04-15 04:06:16 +00:00
if (stack == null || stack.getType() == Material.AIR) {
throw new Exception(tl("nothingInHand"));
}
2015-04-15 04:06:16 +00:00
if (args.length == 0) {
final Set<String> usableEnchants = new TreeSet<>();
2015-04-15 04:06:16 +00:00
for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet()) {
final String name = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
if (usableEnchants.contains(name) || (user.isAuthorized("essentials.enchantments." + name) && entry.getValue().canEnchantItem(stack))) {
usableEnchants.add(entry.getKey());
2015-04-15 04:06:16 +00:00
}
}
throw new NotEnoughArgumentsException(tl("enchantments", StringUtil.joinList(usableEnchants.toArray())));
2015-04-15 04:06:16 +00:00
}
int level = 1;
2015-04-15 04:06:16 +00:00
if (args.length > 1) {
try {
level = Integer.parseInt(args[1]);
} catch (NumberFormatException ex) {
throw new NotEnoughArgumentsException();
2015-04-15 04:06:16 +00:00
}
}
2015-04-15 04:06:16 +00:00
final MetaItemStack metaStack = new MetaItemStack(stack);
final Enchantment enchantment = metaStack.getEnchantment(user, args[0]);
metaStack.addEnchantment(user.getSource(), ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchantments.allowunsafe"), enchantment, level);
2016-11-28 23:13:36 +00:00
InventoryWorkaround.setItemInMainHand(user.getBase(), metaStack.getItemStack());
2015-04-15 04:06:16 +00:00
user.getBase().updateInventory();
final String enchantName = enchantment.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
2015-04-15 04:06:16 +00:00
if (level == 0) {
user.sendMessage(tl("enchantmentRemoved", enchantName));
2015-04-15 04:06:16 +00:00
} else {
user.sendMessage(tl("enchantmentApplied", enchantName));
2015-04-15 04:06:16 +00:00
}
}
@Override
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
if (args.length == 1) {
return new ArrayList<>(Enchantments.keySet());
} else if (args.length == 2) {
Enchantment enchantment = Enchantments.getByName(args[0]);
if (enchantment == null) {
return Collections.emptyList();
}
int min = enchantment.getStartLevel();
int max = enchantment.getMaxLevel();
List<String> options = Lists.newArrayList();
for (int i = min; i <= max; i++) {
options.add(Integer.toString(i));
}
return options;
} else {
return Collections.emptyList();
}
}
}