How to get Enchantments from enchanted book

Discussion in 'Plugin Development' started by knugi, Feb 15, 2018.

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

    knugi

    Hello, how to get enchantments from enchanted book?
    I used this code:
    Code:
    public String getEnchantments(ItemStack item) {
            if (item.hasItemMeta()) {
                ItemMeta meta = item.getItemMeta();
                if (meta instanceof EnchantmentStorageMeta) {
                    StringBuilder sb = new StringBuilder();
                    for (Map.Entry<Enchantment, Integer> entry : ((EnchantmentStorageMeta) meta).getStoredEnchants().entrySet()) {
                        sb.append(entry.getKey().getName()).append(" ").append(entry.getValue());
                    }
                    return sb.toString();
                }
            }
            return "";
        }
    and:
    public void onInventoryClick(InventoryClickEvent e) {
            if(e.getCursor().getType()==Material.ENCHANTED_BOOK) {
                ItemStack book = e.getCurrentItem();
                Player p = (Player)e.getWhoClicked();
                p.sendMessage(getEnchantments(book));
              
            }
        }
    
    and ItemMeta from my ItemStack is null. Enchanted Book is from gamemode 1. Anyone book return null in itemmeta and EnchantmentStorageMeta :/
    Please help someone Thanks You
     
  2. Cast the itemmeta to (EnchantmentStorageMeta)

    Code:
            EnchantmentStorageMeta meta =(EnchantmentStorageMeta)book.getItemMeta();
            Map<Enchantment, Integer> enchants = meta.getEnchants();

    I hope that works for you :)
     
  3. Offline

    timtower Administrator Administrator Moderator

    @Proxygames14
     
  4. :rolleyes: Thanks, I didn't noticed that.

    This works fine for me :)
    BTW: your code works fine for me though;

    Code:
            ItemStack item = player.getInventory().getItemInMainHand();
    
            if(item.getItemMeta() instanceof EnchantmentStorageMeta) {
                EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                Bukkit.broadcastMessage(meta.getStoredEnchants().toString());
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 15, 2018
  5. Offline

    knugi

    @Proxygames14
    Yea, in the event PlayerInteractEvent with rightclick it works.
    But in event InventoryClickEvent get from current item it's not working
    Code:
        It work:
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            if(e.getAction()==Action.RIGHT_CLICK_AIR) {
                ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
             
                if(item.getItemMeta() instanceof EnchantmentStorageMeta) {
                    EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
                    Bukkit.broadcastMessage(meta.getStoredEnchants().toString());
                    return;
                }
                Bukkit.broadcastMessage("item doesn't have enchants");
            }
        }
    This code isn't work
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if(e.getCursor().getType()==Material.ENCHANTED_BOOK) {
                ItemStack book = e.getCurrentItem();
                Player p = (Player)e.getWhoClicked();
                if(book.getItemMeta() instanceof EnchantmentStorageMeta) {
                    EnchantmentStorageMeta meta = (EnchantmentStorageMeta) book.getItemMeta();
                    Bukkit.broadcastMessage(meta.getStoredEnchants().toString());
                    return;
                }
                Bukkit.broadcastMessage("item doesn't have enchants");
            }
        }
    In e.g 2 broadcast "item doesn't have enchants" and i not know what's happend :/

    //Edit. Okey, my bad.
    I used e.getCurrentItem() and this not work.
    e.getCursos() it's good.
    Thanks all for help :)
     
    Last edited: Feb 15, 2018
Thread Status:
Not open for further replies.

Share This Page