diff options
author | snowleo <schneeleo@gmail.com> | 2011-11-27 08:57:21 +0100 |
---|---|---|
committer | snowleo <schneeleo@gmail.com> | 2011-11-27 08:57:21 +0100 |
commit | 098072517a2bd667e3adc681333192910b442dc5 (patch) | |
tree | 51b30658be0237c5f57625e1117cacdc17e5dda9 | |
parent | b8f9a4b96f8ebd6c41f9806e1189edefcbf5c490 (diff) | |
download | Essentials-098072517a2bd667e3adc681333192910b442dc5.tar Essentials-098072517a2bd667e3adc681333192910b442dc5.tar.gz Essentials-098072517a2bd667e3adc681333192910b442dc5.tar.lz Essentials-098072517a2bd667e3adc681333192910b442dc5.tar.xz Essentials-098072517a2bd667e3adc681333192910b442dc5.zip |
Added enchantment to /give and /item
Updated plugin.yml
5 files changed, 82 insertions, 15 deletions
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java index ed0a98fd1..42fc250e2 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java @@ -7,6 +7,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.regex.Pattern; import org.bukkit.Server; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -16,6 +17,7 @@ import static com.earth2me.essentials.I18n._; public class Commandenchant extends EssentialsCommand { private static final Map<String, Enchantment> ENCHANTMENTS = new HashMap<String, Enchantment>(); + private static final transient Pattern NUMPATTERN = Pattern.compile("\\d+"); static { @@ -89,27 +91,40 @@ public class Commandenchant extends EssentialsCommand level = -1; } } - Enchantment enchantment = Enchantment.getByName(args[0].toUpperCase(Locale.ENGLISH)); + Enchantment enchantment = getEnchantment(args[0], user); + if (level < enchantment.getStartLevel() || level > enchantment.getMaxLevel()) + { + level = enchantment.getMaxLevel(); + } + stack.addEnchantment(enchantment, level); + user.setItemInHand(stack); + user.updateInventory(); + final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); + user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' '))); + } + + public static Enchantment getEnchantment(final String name, final User user) throws Exception + { + + Enchantment enchantment; + if (NUMPATTERN.matcher(name).matches()) { + enchantment = Enchantment.getById(Integer.parseInt(name)); + } else { + enchantment = Enchantment.getByName(name.toUpperCase(Locale.ENGLISH)); + } if (enchantment == null) { - enchantment = ENCHANTMENTS.get(args[0].toLowerCase(Locale.ENGLISH)); + enchantment = ENCHANTMENTS.get(name.toLowerCase(Locale.ENGLISH)); } if (enchantment == null) { throw new Exception(_("enchantmentNotFound")); } final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); - if (!user.isAuthorized("essentials.enchant." + enchantmentName)) + if (user != null && !user.isAuthorized("essentials.enchant." + enchantmentName)) { throw new Exception(_("enchantmentPerm", enchantmentName)); } - if (level < enchantment.getStartLevel() || level > enchantment.getMaxLevel()) - { - level = enchantment.getMaxLevel(); - } - stack.addEnchantment(enchantment, level); - user.setItemInHand(stack); - user.updateInventory(); - user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' '))); + return enchantment; } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java index ebda30dd5..fb0b78c40 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java @@ -6,6 +6,7 @@ import org.bukkit.ChatColor; 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,6 +45,29 @@ public class Commandgive extends EssentialsCommand stack.setAmount(Integer.parseInt(args[2])); } + if (args.length > 3) + { + for (int i = 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(); + } + stack.addEnchantment(enchantment, level); + } + } + if (stack.getType() == Material.AIR) { throw new Exception(ChatColor.RED + "You can't give air."); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java index 3bd35353a..c17f851e4 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java @@ -5,6 +5,7 @@ import com.earth2me.essentials.User; import java.util.Locale; import org.bukkit.Material; import org.bukkit.Server; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -40,6 +41,29 @@ public class Commanditem extends EssentialsCommand stack.setAmount(Integer.parseInt(args[1])); } + 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(); + } + stack.addEnchantment(enchantment, level); + } + } + if (stack.getType() == Material.AIR) { throw new Exception(_("cantSpawnItem", "Air")); diff --git a/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java b/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java index c48204a06..7ed12b6d5 100644 --- a/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java +++ b/Essentials/src/com/earth2me/essentials/storage/BukkitConstructor.java @@ -129,7 +129,7 @@ public class BukkitConstructor extends Constructor for (int i = 2; i < split1.length; i++) { final String[] split3 = split1[0].split("[:+',;.]", 2); - if (split3.length != 2) + if (split3.length < 1) { continue; } @@ -148,7 +148,7 @@ public class BukkitConstructor extends Constructor continue; } int level = enchantment.getStartLevel(); - if (NUMPATTERN.matcher(split3[1]).matches()) + if (split3.length == 2 && NUMPATTERN.matcher(split3[1]).matches()) { level = Integer.parseInt(split3[1]); } diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index e3f500b9e..55b7a1c1b 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -79,6 +79,10 @@ commands: description: Manages the server economy. usage: /<command> <give|take|reset> <player> <amount> aliases: [economy,eeco,eeconomy] + enchant: + description: Enchants the item the user is holding. + usage: /<command> <enchantmentname> [level] + aliases: [enchantment] essentials: description: Reloads essentials. usage: /<command> @@ -104,7 +108,7 @@ commands: aliases: [mem,memory,egc,emem,ememory] give: description: Give a player an item. - usage: /<command> <player> <item|numeric> [amount] + usage: /<command> <player> <item|numeric> [amount <enchantmentname[:level]> ...] aliases: [egive] god: description: Enables your godly powers. @@ -140,7 +144,7 @@ commands: aliases: [einvsee] item: description: Spawn an item. - usage: /<command> <item|numeric> [amount] + usage: /<command> <item|numeric> [amount <enchantmentname[:level]> ...] aliases: [i,eitem] jails: description: List all jails. |