Essentials/Essentials/src/main/java/com/earth2me/essentials/commands/Commandenchant.java

91 lines
3.7 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.Enchantments;
2013-01-12 15:12:12 +01:00
import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.User;
2016-11-29 00:13:36 +01:00
import com.earth2me.essentials.craftbukkit.InventoryWorkaround;
2013-06-08 23:31:19 +02:00
import com.earth2me.essentials.utils.StringUtil;
import com.google.common.collect.Lists;
2013-03-16 12:01:56 +01:00
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.enchantments.Enchantment;
2013-01-12 18:05:05 +01:00
import org.bukkit.inventory.ItemStack;
2020-10-03 19:46:05 +02:00
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
2015-04-15 06:06:16 +02: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 06:06:16 +02:00
if (stack == null || stack.getType() == Material.AIR) {
throw new Exception(tl("nothingInHand"));
}
2015-04-15 06:06:16 +02:00
if (args.length == 0) {
final Set<String> usableEnchants = new TreeSet<>();
2020-10-03 19:46:05 +02:00
for (final 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 06:06:16 +02:00
}
}
throw new NotEnoughArgumentsException(tl("enchantments", StringUtil.joinList(usableEnchants.toArray())));
2015-04-15 06:06:16 +02:00
}
int level = 1;
2015-04-15 06:06:16 +02:00
if (args.length > 1) {
try {
level = Integer.parseInt(args[1]);
2020-10-03 19:46:05 +02:00
} catch (final NumberFormatException ex) {
throw new NotEnoughArgumentsException();
2015-04-15 06:06:16 +02:00
}
}
2015-04-15 06:06:16 +02: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-29 00:13:36 +01:00
InventoryWorkaround.setItemInMainHand(user.getBase(), metaStack.getItemStack());
2015-04-15 06:06:16 +02:00
user.getBase().updateInventory();
final String enchantName = enchantment.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
2015-04-15 06:06:16 +02:00
if (level == 0) {
user.sendMessage(tl("enchantmentRemoved", enchantName));
2015-04-15 06:06:16 +02:00
} else {
user.sendMessage(tl("enchantmentApplied", enchantName));
2015-04-15 06:06:16 +02: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) {
2020-10-03 19:46:05 +02:00
final Enchantment enchantment = Enchantments.getByName(args[0]);
if (enchantment == null) {
return Collections.emptyList();
}
2020-10-03 19:46:05 +02:00
final int min = enchantment.getStartLevel();
final int max = enchantment.getMaxLevel();
final List<String> options = Lists.newArrayList();
for (int i = min; i <= max; i++) {
options.add(Integer.toString(i));
}
return options;
} else {
return Collections.emptyList();
}
}
}