Custom recipe in recipe book

Discussion in 'Spigot Plugin Development' started by SIndybad, Jun 8, 2017.

  1. In 1.12, is there any way we can add a custom recipe to a player's recipe book?
    Or is it client sided :(
     
  2. I think you can't do this with a plugin, but with a mod.
     
  3. I think custom recipes were pushed to 1.13. So maybe after that, but I doubt you can do it now.

    .. If you can, I'd love to know :)
     
  4. Maybe just try and create your own?
     
  5. You can add custom recipes to the player's recipeBook nbt data by editing a knowledge book's item meta - the custom recipe is added, but as yet does NOT display in the Recipe Book. Here's the method the TARDIS plugin uses:

    Code (Text):

    private boolean giveKnowledgeBook(CommandSender sender, String item, Player player) {
             String item_to_give = items.get(item);
             ItemStack book = new ItemStack(Material.KNOWLEDGE_BOOK, 1);
             KnowledgeBookMeta kbm = (KnowledgeBookMeta) book.getItemMeta();
             NamespacedKey nsk = new NamespacedKey(plugin, item_to_give.replace(" ", "_"));
             kbm.addRecipe(nsk);
             book.setItemMeta(kbm);
             player.getInventory().addItem(book);
             player.updateInventory();
             TARDISMessage.send(player, "GIVE_KNOWLEDGE", sender.getName(), item_to_give);
             return true;
    }
     
     
    • Like Like x 1
    • Winner Winner x 1
    • Useful Useful x 1
  6. Just thought I'd add that when making your custom recipe, you'll need to use the same NamespacedKey that you use to add it to the Knowledge book meta so eventually it will all work (theoretically) - again code from TARDIS:

    Code (Text):

    Set<String> shaped = plugin.getRecipesConfig().getConfigurationSection("shaped").getKeys(false);
    shaped.forEach((s) -> {
             plugin.getServer().addRecipe(makeRecipe(s));
    });

    private ShapedRecipe makeRecipe(String s) {
            /*
             * Get recipe data from yml file (code not included here...)
            */
            ItemStack is = new ItemStack(mat, amount);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(s);
            is.setItemMeta(im);
            NamespacedKey key = new NamespacedKey(plugin, s.replace(" ", "_"));
            ShapedRecipe r = new ShapedRecipe(key, is);
            /*
             * add ingredients to recipe (code not included here...)
            */
            return r;
    }
     
    You can check out the full code on GitHub
     
    • Winner Winner x 2
  7. Just a heads up - a recent commit to Spigot means NamespacedKeys MUST be lowercase... so
    Code (Text):
    NamespacedKey key = new NamespacedKey(plugin, s.replace(" ", "_"));
    Will need to become:
    Code (Text):
    NamespacedKey key = new NamespacedKey(plugin, s.replace(" ", "_").toLowerCase(Locale.ENGLISH));
     
    • Informative Informative x 1