How do I create an enchanted book that can be used in an anvil?

Discussion in 'Plugin Development' started by Drkmaster83, Dec 27, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Drkmaster83

    I know how to use the regular meta methods that we have now, but I need to know how to change an item in hand from a book (or spawned in 403 (enchanted book)) into a yellow-named enchanted book that will legitimately work in an anvil. Is there any way to do this?

    EDIT: I know about EnchantmentStorageMeta, but I'm not quite sure how to use it. In addition, I know that you probably want me to do an if statement on whether or not their item in hand is a book/enchanted book... that's what I'm already doing. But, enchanting an enchanted book (spawned in) does not work anvil-wise...
     
  2. Offline

    fireblast709

    Get a book and set the EnchantmentStorageMeta
    Code:java
    1. EnchantmentStorageMeta esm = (EnchantmentStorageMeta)book.getItemMeta();
    2. // do stuff with the meta, check the javadocs for method
    3. book.setItemMeta(esm);
     
  3. Offline

    Drkmaster83

    When I did that, there was no noticeable change. It stayed the exact same way it was.
     
  4. Offline

    william9518

    What is the difference between
    meta.addEnchant()
    and
    meta.addStoredEnchant()?

    Code:
    public ItemStack addBookEnchantment(ItemStack item, Enchantment enchantment, int level){
            EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
            meta.addStoredEnchant(enchantment, level, true);
            item.setItemMeta(meta);
            return item;
        }
    and then do:
    Code:
    addBookEnchantment(item, enchantment, level);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  5. Offline

    fireblast709

    addStoredEnchant(Enchantment, int) is for the books. Adding actual enchantments to weapons is just .add(Unsafe)Enchantment(Enchantment, int) for the ItemStack and .addEnchant() for the ItemMeta
     
  6. Offline

    william9518

    thank you
     
  7. Offline

    Drkmaster83

    I used it the same way (except not from an external method) in my old attempts, I guess I was missing something :\
     
  8. Offline

    william9518

    use the external method... and this:
    Code:
    public static Enchantment getEnchantmentByCommonName(String name){
            name = name.toLowerCase();
            if(name.toLowerCase().equalsIgnoreCase("fire_protection")) return Enchantment.PROTECTION_FIRE;
            if(name.toLowerCase().equalsIgnoreCase("blast_protection")) return Enchantment.PROTECTION_EXPLOSIONS;
            if(name.toLowerCase().equalsIgnoreCase("projectile_protection")) return Enchantment.PROTECTION_PROJECTILE;
            if(name.toLowerCase().equalsIgnoreCase("protection")) return Enchantment.PROTECTION_ENVIRONMENTAL;
            if(name.toLowerCase().equalsIgnoreCase("feather_falling")) return Enchantment.PROTECTION_FALL;
            if(name.toLowerCase().equalsIgnoreCase("respiration")) return Enchantment.OXYGEN;
            if(name.toLowerCase().equalsIgnoreCase("aqua_affinity")) return Enchantment.WATER_WORKER;
            if(name.toLowerCase().equalsIgnoreCase("sharpness")) return Enchantment.DAMAGE_ALL;
            if(name.toLowerCase().equalsIgnoreCase("smite")) return Enchantment.DAMAGE_UNDEAD;
            if(name.toLowerCase().equalsIgnoreCase("bane_of_arthropods")) return Enchantment.DAMAGE_ARTHROPODS;
            if(name.toLowerCase().equalsIgnoreCase("knockback")) return Enchantment.KNOCKBACK;
            if(name.toLowerCase().equalsIgnoreCase("fire_aspect")) return Enchantment.FIRE_ASPECT;
            if(name.toLowerCase().equalsIgnoreCase("looting")) return Enchantment.LOOT_BONUS_MOBS;
            if(name.toLowerCase().equalsIgnoreCase("power")) return Enchantment.ARROW_DAMAGE;
            if(name.toLowerCase().equalsIgnoreCase("punch")) return Enchantment.ARROW_KNOCKBACK;
            if(name.toLowerCase().equalsIgnoreCase("flame")) return Enchantment.ARROW_FIRE;
            if(name.toLowerCase().equalsIgnoreCase("infinity")) return Enchantment.ARROW_INFINITE;
            if(name.toLowerCase().equalsIgnoreCase("efficiency")) return Enchantment.DIG_SPEED;
            if(name.toLowerCase().equalsIgnoreCase("unbreaking")) return Enchantment.DURABILITY;
            if(name.toLowerCase().equalsIgnoreCase("silk_touch")) return Enchantment.SILK_TOUCH;
            if(name.toLowerCase().equalsIgnoreCase("fortune")) return Enchantment.LOOT_BONUS_BLOCKS;
            if(name.toLowerCase().equalsIgnoreCase("thorns")) return Enchantment.THORNS;
            return null;
        }
    so you can use the common name instead of all those obscure names
     
Thread Status:
Not open for further replies.

Share This Page