diff options
6 files changed, 142 insertions, 110 deletions
diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index 85753e816..a277c5892 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -5,9 +5,7 @@ import com.earth2me.essentials.api.IItemDb; import java.util.*; import java.util.regex.Pattern; import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.EnchantmentStorageMeta; public class ItemDb implements IConf, IItemDb @@ -139,95 +137,7 @@ public class ItemDb implements IConf, IItemDb retval.setDurability(metaData); return retval; } - - public void addStringEnchantment(final User user, final boolean allowUnsafe, final ItemStack stack, final String string) throws Exception - { - final String[] split = splitPattern.split(string, 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 - { - try - { - if (stack.getType().equals(Material.ENCHANTED_BOOK)) - { - EnchantmentStorageMeta meta = (EnchantmentStorageMeta)stack.getItemMeta(); - if (level == 0) - { - meta.removeStoredEnchant(enchantment); - } - else - { - meta.addStoredEnchant(enchantment, level, allowUnsafe); - } - stack.setItemMeta(meta); - } - else // all other material types besides ENCHANTED_BOOK - { - if (level == 0) - { - stack.removeEnchantment(enchantment); - } - else - { - 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()); diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 4f0957f38..b2b100553 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -115,24 +115,25 @@ public class Kit } final String[] parts = d.split(" "); - final ItemStack stack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1); + final ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1); + final MetaItemStack stack = new MetaItemStack(parseStack); if (parts.length > 2) { for (int i = 2; i < parts.length; i++) { - ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, parts[i]); + stack.addStringEnchantment(null, allowUnsafe, parts[i]); } } final Map<Integer, ItemStack> overfilled; if (user.isAuthorized("essentials.oversizedstacks")) { - overfilled = InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack); + overfilled = InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack.getBase()); } else { - overfilled = InventoryWorkaround.addItems(user.getInventory(), stack); + overfilled = InventoryWorkaround.addItems(user.getInventory(), stack.getBase()); } for (ItemStack itemStack : overfilled.values()) { diff --git a/Essentials/src/com/earth2me/essentials/MetaItemStack.java b/Essentials/src/com/earth2me/essentials/MetaItemStack.java new file mode 100644 index 000000000..9aa064590 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/MetaItemStack.java @@ -0,0 +1,121 @@ +package com.earth2me.essentials; + +import static com.earth2me.essentials.I18n._; +import java.util.Locale; +import java.util.regex.Pattern; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; + + +public class MetaItemStack extends ItemStack +{ + private final transient Pattern splitPattern = Pattern.compile("[:+',;.]"); + private final ItemStack stack; + + public MetaItemStack(final ItemStack stack) + { + this.stack = stack; + } + + public ItemStack getBase() + { + return stack; + } + + public void addStringMeta(final User user, final boolean allowUnsafe, final String string) throws Exception + { + + } + + public void addStringEnchantment(final User user, final boolean allowUnsafe, final String string) throws Exception + { + final String[] split = splitPattern.split(string, 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, enchantment, level); + } + + public void addEnchantment(final User user, final boolean allowUnsafe, final Enchantment enchantment, final int level) throws Exception + { + try + { + if (stack.getType().equals(Material.ENCHANTED_BOOK)) + { + EnchantmentStorageMeta meta = (EnchantmentStorageMeta)stack.getItemMeta(); + if (level == 0) + { + meta.removeStoredEnchant(enchantment); + } + else + { + meta.addStoredEnchant(enchantment, level, allowUnsafe); + } + stack.setItemMeta(meta); + } + else // all other material types besides ENCHANTED_BOOK + { + if (level == 0) + { + stack.removeEnchantment(enchantment); + } + else + { + 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; + } + + +} diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java index 97abbc6db..461631282 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandenchant.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.Enchantments; import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.MetaItemStack; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import java.util.Locale; @@ -10,7 +11,6 @@ import java.util.Set; import java.util.TreeSet; import org.bukkit.Server; import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.ItemStack; public class Commandenchant extends EssentialsCommand @@ -24,7 +24,7 @@ public class Commandenchant extends EssentialsCommand @Override protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - final ItemStack stack = user.getItemInHand(); + final MetaItemStack stack = new MetaItemStack(user.getItemInHand()); if (stack == null) { throw new Exception(_("nothingInHand")); @@ -58,11 +58,11 @@ public class Commandenchant extends EssentialsCommand } final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments() && user.isAuthorized("essentials.enchant.allowunsafe"); - final Enchantment enchantment = ess.getItemDb().getEnchantment(user, args[0]); - ess.getItemDb().addEnchantment(user, allowUnsafe, stack, enchantment, level); + final Enchantment enchantment = stack.getEnchantment(user, args[0]); + stack.addEnchantment(user, allowUnsafe, enchantment, level); - user.getInventory().setItemInHand(stack); + user.getInventory().setItemInHand(stack.getBase()); user.updateInventory(); final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH); if (level == 0) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java index 94b4b6d6b..0fabf3afc 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgive.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgive.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.MetaItemStack; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import com.earth2me.essentials.craftbukkit.InventoryWorkaround; @@ -9,7 +10,6 @@ import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; public class Commandgive extends EssentialsCommand @@ -27,7 +27,7 @@ public class Commandgive extends EssentialsCommand throw new NotEnoughArgumentsException(); } - final ItemStack stack = ess.getItemDb().get(args[1]); + final MetaItemStack stack = new MetaItemStack(ess.getItemDb().get(args[1])); final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); if (sender instanceof Player @@ -78,7 +78,7 @@ public class Commandgive extends EssentialsCommand for (int i = Util.isInt(args[3]) ? 4 : 3; i < args.length; i++) { - ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]); + stack.addStringEnchantment(null, allowUnsafe, args[i]); } } @@ -91,11 +91,11 @@ public class Commandgive extends EssentialsCommand sender.sendMessage(_("giveSpawn", stack.getAmount(), itemName, giveTo.getDisplayName())); if (giveTo.isAuthorized("essentials.oversizedstacks")) { - InventoryWorkaround.addOversizedItems(giveTo.getInventory(), ess.getSettings().getOversizedStackSize(), stack); + InventoryWorkaround.addOversizedItems(giveTo.getInventory(), ess.getSettings().getOversizedStackSize(), stack.getBase()); } else { - InventoryWorkaround.addItems(giveTo.getInventory(), stack); + InventoryWorkaround.addItems(giveTo.getInventory(), stack.getBase()); } giveTo.updateInventory(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java index a071d8068..096da99d5 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commanditem.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commanditem.java @@ -1,12 +1,12 @@ package com.earth2me.essentials.commands; import static com.earth2me.essentials.I18n._; +import com.earth2me.essentials.MetaItemStack; import com.earth2me.essentials.User; import com.earth2me.essentials.craftbukkit.InventoryWorkaround; import java.util.Locale; import org.bukkit.Material; import org.bukkit.Server; -import org.bukkit.inventory.ItemStack; public class Commanditem extends EssentialsCommand @@ -23,7 +23,7 @@ public class Commanditem extends EssentialsCommand { throw new NotEnoughArgumentsException(); } - final ItemStack stack = ess.getItemDb().get(args[0]); + final MetaItemStack stack = new MetaItemStack(ess.getItemDb().get(args[0])); final String itemname = stack.getType().toString().toLowerCase(Locale.ENGLISH).replace("_", ""); if (ess.getSettings().permissionBasedItemSpawn() @@ -60,7 +60,7 @@ public class Commanditem extends EssentialsCommand for (int i = 2; i < args.length; i++) { - ess.getItemDb().addStringEnchantment(null, allowUnsafe, stack, args[i]); + stack.addStringEnchantment(null, allowUnsafe, args[i]); } } @@ -74,11 +74,11 @@ public class Commanditem extends EssentialsCommand user.sendMessage(_("itemSpawn", stack.getAmount(), displayName)); if (user.isAuthorized("essentials.oversizedstacks")) { - InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack); + InventoryWorkaround.addOversizedItems(user.getInventory(), ess.getSettings().getOversizedStackSize(), stack.getBase()); } else { - InventoryWorkaround.addItems(user.getInventory(), stack); + InventoryWorkaround.addItems(user.getInventory(), stack.getBase()); } user.updateInventory(); } |