diff options
author | Chasing Code <chasingcode@gmail.com> | 2013-01-08 11:37:46 -0800 |
---|---|---|
committer | Chasing Code <chasingcode@gmail.com> | 2013-01-08 11:37:46 -0800 |
commit | 2b57381fb86c000fb880e9f056109754162ed151 (patch) | |
tree | c03a040da069fc757ec3e0d89a134ff20740fd97 | |
parent | 67b9a82e51432713dc2dfc1635055f03d258a08d (diff) | |
download | Essentials-2b57381fb86c000fb880e9f056109754162ed151.tar Essentials-2b57381fb86c000fb880e9f056109754162ed151.tar.gz Essentials-2b57381fb86c000fb880e9f056109754162ed151.tar.lz Essentials-2b57381fb86c000fb880e9f056109754162ed151.tar.xz Essentials-2b57381fb86c000fb880e9f056109754162ed151.zip |
Store enchanments in Enchanted Books
Change enchant command to store the enchantment instead of applying it when used on an Enchanted Book. If an enchantment is already present, replace it (only one allowed on Enchanted Books).
-rw-r--r-- | Essentials/src/com/earth2me/essentials/ItemDb.java | 57 |
1 files changed, 49 insertions, 8 deletions
diff --git a/Essentials/src/com/earth2me/essentials/ItemDb.java b/Essentials/src/com/earth2me/essentials/ItemDb.java index 80c54a5a2..9dad836c7 100644 --- a/Essentials/src/com/earth2me/essentials/ItemDb.java +++ b/Essentials/src/com/earth2me/essentials/ItemDb.java @@ -171,21 +171,37 @@ public class ItemDb implements IConf, IItemDb 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 + if (stack.getType().equals(Material.ENCHANTED_BOOK)) { try { - if (allowUnsafe) + EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta(); + + if (level == 0) { - stack.addUnsafeEnchantment(enchantment, level); + if (meta.hasStoredEnchant(enchantment)) + { + meta.removeStoredEnchant(enchantment); + stack.setItemMeta(meta); + } } else { - stack.addEnchantment(enchantment, level); + // Enchanted Books only allowed to have one enchantment + if (meta.hasStoredEnchants()) + { + // Although there should be only one, don't make assumptions + Iterator<Map.Entry<Enchantment, Integer>> entries = meta.getStoredEnchants().entrySet().iterator(); + while (entries.hasNext()) + { + Map.Entry<Enchantment, Integer> entry = entries.next(); + Enchantment ench = entry.getKey(); + meta.removeStoredEnchant(ench); + } + } + + meta.addStoredEnchant(enchantment, level, allowUnsafe); + stack.setItemMeta(meta); } } catch (Exception ex) @@ -193,6 +209,31 @@ public class ItemDb implements IConf, IItemDb throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex); } } + else // all other material types besides ENCHANTED_BOOK + { + 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 |