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).
This commit is contained in:
Chasing Code 2013-01-08 11:37:46 -08:00
parent 67b9a82e51
commit 2b57381fb8

View File

@ -170,6 +170,46 @@ 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 (stack.getType().equals(Material.ENCHANTED_BOOK))
{
try
{
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) stack.getItemMeta();
if (level == 0)
{
if (meta.hasStoredEnchant(enchantment))
{
meta.removeStoredEnchant(enchantment);
stack.setItemMeta(meta);
}
}
else
{
// 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)
{
throw new Exception("Enchantment " + enchantment.getName() + ": " + ex.getMessage(), ex);
}
}
else // all other material types besides ENCHANTED_BOOK
{
if (level == 0)
{
@ -194,6 +234,7 @@ public class ItemDb implements IConf, IItemDb
}
}
}
}
//TODO: Properly TL this
public Enchantment getEnchantment(final User user, final String name) throws Exception