diff options
5 files changed, 143 insertions, 169 deletions
diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index 10fd12937..820aea12d 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -4,6 +4,7 @@ import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.api.IItemDb; import java.util.*; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -135,18 +136,92 @@ public class ItemDb implements IConf, IItemDb return retval; } + public void addStringEnchantment(final User user, final boolean allowUnsafe, final ItemStack stack, final String string) throws Exception + { + final String[] split = string.split("[:+',;.]", 2); + if (split.length < 1) + { + return; + } + + Enchantment enchantment = getEnchantment(user, split[0]); + + int level = -1; + if (split.length > 1) + { + try + { + level = Integer.parseInt(split[1]); + } + catch (NumberFormatException ex) + { + level = -1; + } + } + + if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) + { + level = enchantment.getMaxLevel(); + } + addEnchantment(user, allowUnsafe, stack, enchantment, level); + } + + public void addEnchantment(final User user, final boolean allowUnsafe, final ItemStack stack, final Enchantment enchantment, final int level) throws Exception + { + if (level == 0) + { + stack.removeEnchantment(enchantment); + } + else + { + try + { + if (allowUnsafe) + { + stack.addUnsafeEnchantment(enchantment, level); + } + else + { + stack.addEnchantment(enchantment, level); + } + } + catch (Exception ex) + { + throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex); + } + } + } + + //TODO: Properly TL this + public Enchantment getEnchantment(final User user, final String name) throws Exception + { + final Enchantment enchantment = Enchantments.getByName(name); + if (enchantment == null) + { + throw new Exception(_("enchantmentNotFound") + ": " + name); + } + final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); + if (user != null && !user.isAuthorized("essentials.enchant." + enchantmentName)) + { + throw new Exception(_("enchantmentPerm", enchantmentName)); + } + return enchantment; + } + public String names(ItemStack item) { ItemData itemData = new ItemData(item.getTypeId(), item.getDurability()); List<String> nameList = names.get(itemData); - if (nameList == null) { - itemData = new ItemData(item.getTypeId(), (short) 0); + if (nameList == null) + { + itemData = new ItemData(item.getTypeId(), (short)0); nameList = names.get(itemData); - if (nameList == null) { + if (nameList == null) + { return null; } } - + if (nameList.size() > 15) { nameList = nameList.subList(0, 14); @@ -154,6 +229,7 @@ public class ItemDb implements IConf, IItemDb return Util.joinList(", ", nameList); } + class ItemData { final private int itemNo; @@ -198,6 +274,7 @@ public class ItemDb implements IConf, IItemDb } } + class LengthCompare implements java.util.Comparator<String> { public LengthCompare() diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 0611cafec..ef675990f 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -6,9 +6,7 @@ import com.earth2me.essentials.commands.NoChargeException; import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import java.util.*; import java.util.logging.Level; -import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -34,16 +32,16 @@ public class Kit { throw new Exception(_("kitError"), ex); } - + } - + public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception { if (user.isAuthorized("essentials.kit.exemptdelay")) { return; } - + final Calendar time = new GregorianCalendar(); // Take the current time, and remove the delay from it. @@ -56,7 +54,7 @@ public class Kit // When was the last kit used? final long lastTime = user.getKitTimestamp(kitName); - + if (lastTime < earliestLong || lastTime == 0L) { user.setKitTimestamp(kitName, time.getTimeInMillis()); @@ -81,15 +79,14 @@ public class Kit throw new NoChargeException(); } } - + public static List<String> getItems(final User user, final Map<String, Object> kit) throws Exception { if (kit == null) { throw new Exception(_("kitError2")); } - - + try { return (List<String>)kit.get("items"); @@ -100,69 +97,34 @@ public class Kit throw new Exception(_("kitErrorHelp"), e); } } - + public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception { try { boolean spew = false; + final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments(); for (String d : items) { - if (d.startsWith(ess.getSettings().getCurrencySymbol())) + if (d.startsWith(ess.getSettings().getCurrencySymbol())) { Double value = Double.parseDouble(d.substring(ess.getSettings().getCurrencySymbol().length()).trim()); Trade t = new Trade(value, ess); t.pay(user); continue; } - final String[] parts = d.split(" "); - final String[] item = parts[0].split("[:+',;.]", 2); - final int id = Material.getMaterial(Integer.parseInt(item[0])).getId(); - final short data = item.length > 1 ? Short.parseShort(item[1]) : 0; - final int amount = parts.length > 1 ? Integer.parseInt(parts[1]) : 1; - - final ItemStack stack = new ItemStack(id, amount, data); + + final String[] parts = d.split(" ", 3); + final ItemStack stack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1); + if (parts.length > 2) { for (int i = 2; i < parts.length; i++) { - final String[] split = parts[i].split("[:+',;.]", 2); - if (split.length < 1) - { - continue; - } - final Enchantment enchantment = Enchantments.getByName(split[0]); - if (enchantment == null) - { - throw new Exception("Enchantment not found: " + split[0]); - } - int level; - if (split.length > 1) - { - level = Integer.parseInt(split[1]); - } - else - { - level = enchantment.getMaxLevel(); - } - try - { - if (ess.getSettings().allowUnsafeEnchantments()) - { - stack.addUnsafeEnchantment(enchantment, level); - } - else - { - stack.addEnchantment(enchantment, level); - } - } - catch (Exception ex) - { - throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex); - } + ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, parts[i]); } } - + final Map<Integer, ItemStack> overfilled; if (user.isAuthorized("essentials.oversizedstacks")) { diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java index c338090aa..97abbc6db 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java @@ -43,6 +43,7 @@ public class Commandenchant extends EssentialsCommand } throw new NotEnoughArgumentsException(_("enchantments", Util.joinList(enchantmentslist.toArray()))); } + int level = -1; if (args.length > 1) { @@ -55,27 +56,12 @@ public class Commandenchant extends EssentialsCommand level = -1; } } - final Enchantment enchantment = getEnchantment(args[0], user); + final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe"); - if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel())) - { - level = enchantment.getMaxLevel(); - } - if (level == 0) - { - stack.removeEnchantment(enchantment); - } - else - { - if (allowUnsafe) - { - stack.addUnsafeEnchantment(enchantment, level); - } - else - { - stack.addEnchantment(enchantment, level); - } - } + final Enchantment enchantment = ess.getItemDb().getEnchantment(user, args[0]); + ess.getItemDb().addEnchantment(user, allowUnsafe, stack, enchantment, level); + + user.getInventory().setItemInHand(stack); user.updateInventory(); final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); @@ -88,20 +74,4 @@ public class Commandenchant extends EssentialsCommand user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' '))); } } - - public static Enchantment getEnchantment(final String name, final User user) throws Exception - { - - final Enchantment enchantment = Enchantments.getByName(name); - if (enchantment == null) - { - throw new Exception(_("enchantmentNotFound")); - } - final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); - if (user != null && !user.isAuthorized("essentials.enchant." + enchantmentName)) - { - throw new Exception(_("enchantmentPerm", enchantmentName)); - } - return enchantment; - } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java index c9dde4688..94b4b6d6b 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java @@ -8,7 +8,6 @@ import java.util.Locale; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.command.CommandSender; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -44,56 +43,42 @@ public class Commandgive extends EssentialsCommand final User giveTo = getPlayer(server, args, 0); - if (args.length > 3 && Util.isInt(args[2]) && Util.isInt(args[3])) + try { - stack.setAmount(Integer.parseInt(args[2])); - stack.setDurability(Short.parseShort(args[3])); - } - else if (args.length > 2 && Integer.parseInt(args[2]) > 0) - { - stack.setAmount(Integer.parseInt(args[2])); - } - else if (ess.getSettings().getDefaultStackSize() > 0) - { - stack.setAmount(ess.getSettings().getDefaultStackSize()); + if (args.length > 3 && Util.isInt(args[2]) && Util.isInt(args[3])) + { + stack.setAmount(Integer.parseInt(args[2])); + stack.setDurability(Short.parseShort(args[3])); + } + else if (args.length > 2 && Integer.parseInt(args[2]) > 0) + { + stack.setAmount(Integer.parseInt(args[2])); + } + else if (ess.getSettings().getDefaultStackSize() > 0) + { + stack.setAmount(ess.getSettings().getDefaultStackSize()); + } + else if (ess.getSettings().getOversizedStackSize() > 0 && giveTo.isAuthorized("essentials.oversizedstacks")) + { + stack.setAmount(ess.getSettings().getOversizedStackSize()); + } } - else if (ess.getSettings().getOversizedStackSize() > 0 && giveTo.isAuthorized("essentials.oversizedstacks")) + catch (NumberFormatException e) { - stack.setAmount(ess.getSettings().getOversizedStackSize()); + throw new NotEnoughArgumentsException(); } if (args.length > 3) { + boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments(); + if (allowUnsafe && sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.enchant.allowunsafe")) + { + allowUnsafe = false; + } + for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++) { - final String[] split = args[i].split("[:+',;.]", 2); - if (split.length < 1) - { - continue; - } - final Enchantment enchantment = Commandenchant.getEnchantment(split[0], sender instanceof Player ? ess.getUser(sender) : null); - int level; - if (split.length > 1) - { - level = Integer.parseInt(split[1]); - } - else - { - level = enchantment.getMaxLevel(); - } - boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments(); - if (allowUnsafe && sender instanceof Player && !ess.getUser(sender).isAuthorized("essentials.enchant.allowunsafe")) - { - allowUnsafe = false; - } - if (allowUnsafe) - { - stack.addUnsafeEnchantment(enchantment, level); - } - else - { - stack.addEnchantment(enchantment, level); - } + ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java index a5c59deb6..a071d8068 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java @@ -6,7 +6,6 @@ import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import java.util.Locale; import org.bukkit.Material; import org.bukkit.Server; -import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -16,7 +15,7 @@ public class Commanditem extends EssentialsCommand { super("item"); } - + @Override public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { @@ -25,7 +24,7 @@ public class Commanditem extends EssentialsCommand throw new NotEnoughArgumentsException(); } final ItemStack stack = ess.getItemDb().get(args[0]); - + final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); if (ess.getSettings().permissionBasedItemSpawn() ? (!user.isAuthorized("essentials.itemspawn.item-all") @@ -50,46 +49,27 @@ public class Commanditem extends EssentialsCommand { stack.setAmount(ess.getSettings().getOversizedStackSize()); } - if (args.length > 2) - { - for (int i = 2; i < args.length; i++) - { - final String[] split = args[i].split("[:+',;.]", 2); - if (split.length < 1) - { - continue; - } - final Enchantment enchantment = Commandenchant.getEnchantment(split[0], user); - int level; - if (split.length > 1) - { - level = Integer.parseInt(split[1]); - } - else - { - level = enchantment.getMaxLevel(); - } - if (ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe")) - { - stack.addUnsafeEnchantment(enchantment, level); - } - else - { - stack.addEnchantment(enchantment, level); - } - } - } } catch (NumberFormatException e) { throw new NotEnoughArgumentsException(); } - + if (args.length > 2) + { + final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe"); + + for (int i = 2; i < args.length; i++) + { + ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]); + } + } + + if (stack.getType() == Material.AIR) { throw new Exception(_("cantSpawnItem", "Air")); } - + final String displayName = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' '); user.sendMessage(_("itemSpawn", stack.getAmount(), displayName)); if (user.isAuthorized("essentials.oversizedstacks")) |