Shout an enchanted book walking in an anvil

Discussion in 'Plugin Development' started by Sebeleuu, Feb 24, 2020.

  1. Offline

    Sebeleuu

    Good evening, I would like to create a book that is generated randomly in a chest and I would like it to be enchanted and that it works in an anvil here is my existing code that works except that we cannot use the book in an anvil


    Code:
    if(Math.random() * 100 < 100) {
                    final ItemStack umbreaking = new ItemStack(Material.ENCHANTED_BOOK);
                    final ItemMeta umbreakingm = umbreaking.getItemMeta();
                    umbreakingm.setDisplayName("§8§l> §eLivre solidité");
                    umbreakingm.addEnchant(Enchantment.DURABILITY, 1, true);
                    umbreaking.setItemMeta(umbreakingm);
                    chestMenu.setItem(r.nextInt(chestMenu.getSize()), umbreaking);
                   
                }
    If someone could help me it would be nice! :)
     
  2. Offline

    Wick

    You need to add enchants through EnchantmentStorageMeta, not item meta, if you want to retain use in anvil.

    Code:
    EnchantmentStorageMeta enchantMeta = (EnchantmentStorageMeta) unbreaking.getItemMeta();
     

Share This Page