2011-11-27 03:36:27 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2011-12-04 21:28:29 +00:00
|
|
|
import com.earth2me.essentials.Enchantments;
|
2013-01-12 14:12:12 +00:00
|
|
|
import com.earth2me.essentials.MetaItemStack;
|
2011-11-27 03:36:27 +00:00
|
|
|
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;
|
2017-06-11 00:17:43 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2013-03-16 11:01:56 +00:00
|
|
|
import org.bukkit.Material;
|
2011-11-27 03:36:27 +00:00
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.enchantments.Enchantment;
|
2013-01-12 17:05:05 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2011-11-27 03:36:27 +00:00
|
|
|
|
2020-08-11 18:09:22 +00:00
|
|
|
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 {
|
2016-11-20 20:23:01 +00:00
|
|
|
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"));
|
|
|
|
}
|
2020-08-11 18:09:22 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
if (args.length == 0) {
|
2020-08-11 18:09:22 +00:00
|
|
|
final Set<String> usableEnchants = new TreeSet<>();
|
2015-04-15 04:06:16 +00:00
|
|
|
for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet()) {
|
2020-08-11 18:09:22 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-08-11 18:09:22 +00:00
|
|
|
throw new NotEnoughArgumentsException(tl("enchantments", StringUtil.joinList(usableEnchants.toArray())));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
2011-11-27 03:36:27 +00:00
|
|
|
|
2018-11-19 21:11:25 +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) {
|
2020-08-11 18:09:22 +00:00
|
|
|
throw new NotEnoughArgumentsException();
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-27 03:36:27 +00:00
|
|
|
|
2015-04-15 04:06:16 +00:00
|
|
|
final MetaItemStack metaStack = new MetaItemStack(stack);
|
|
|
|
final Enchantment enchantment = metaStack.getEnchantment(user, args[0]);
|
2020-08-11 18:09:22 +00:00
|
|
|
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();
|
2020-08-11 18:09:22 +00:00
|
|
|
final String enchantName = enchantment.getName().toLowerCase(Locale.ENGLISH).replace('_', ' ');
|
2015-04-15 04:06:16 +00:00
|
|
|
if (level == 0) {
|
2020-08-11 18:09:22 +00:00
|
|
|
user.sendMessage(tl("enchantmentRemoved", enchantName));
|
2015-04-15 04:06:16 +00:00
|
|
|
} else {
|
2020-08-11 18:09:22 +00:00
|
|
|
user.sendMessage(tl("enchantmentApplied", enchantName));
|
2015-04-15 04:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 00:17:43 +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();
|
|
|
|
}
|
|
|
|
}
|
2011-11-27 03:36:27 +00:00
|
|
|
}
|