user11866276
user11866276

Reputation:

Bukkit - Add custom item with custom recipe

I want to create new item. I called it life crystal. I also created new recipe for this new item. My problem is when im trying to craft that item, i craft normal item without new display name and enchantments. My code looks like this.

 // Life Crystal
        ItemStack lifecrystal = new ItemStack(Material.DIAMOND);
        NamespacedKey lifecrystalKey = new NamespacedKey((Plugin) this, "lifecrystal");
        ShapedRecipe lifecrystalRecipe = new ShapedRecipe(lifecrystalKey, lifecrystal);

        ItemMeta lifecrystalLabel = lifecrystal.getItemMeta();
        lifecrystalLabel.setDisplayName(ChatColor.GOLD + "Life Crystal");
        lifecrystalLabel.addEnchant(Enchantment.BINDING_CURSE, 1, false);
        lifecrystal.setItemMeta(lifecrystalLabel);

        lifecrystalRecipe.shape(" E ", "LAL", "DRD");
        lifecrystalRecipe.setIngredient('E', Material.EMERALD);
        lifecrystalRecipe.setIngredient('L', Material.LAPIS_LAZULI);
        lifecrystalRecipe.setIngredient('A', Material.GOLDEN_APPLE);
        lifecrystalRecipe.setIngredient('R', Material.GOLD_INGOT);
        lifecrystalRecipe.setIngredient('D', Material.DIAMOND);
        Bukkit.addRecipe(lifecrystalRecipe);

Edit: My method .setDisplayName shows error : (method invocation set display name may produce null)

Upvotes: 0

Views: 3224

Answers (1)

user5735975
user5735975

Reputation:

You should first make the ItemStack with all the custom metadata, then make the recipe.

    ItemStack lifecrystal = new ItemStack(Material.DIAMOND);
    ItemMeta lifecrystalLabel = lifecrystal.getItemMeta();
    lifecrystalLabel.setDisplayName(ChatColor.GOLD + "Life Crystal");
    lifecrystalLabel.addEnchant(Enchantment.BINDING_CURSE, 1, false);
    lifecrystal.setItemMeta(lifecrystalLabel);

    NamespacedKey lifecrystalKey = new NamespacedKey((Plugin) this, "lifecrystal");
    ShapedRecipe lifecrystalRecipe = new ShapedRecipe(lifecrystalKey, lifecrystal);

    lifecrystalRecipe.shape(" E ", "LAL", "DRD");
    lifecrystalRecipe.setIngredient('E', Material.EMERALD);
    lifecrystalRecipe.setIngredient('L', Material.LAPIS_LAZULI);
    lifecrystalRecipe.setIngredient('A', Material.GOLDEN_APPLE);
    lifecrystalRecipe.setIngredient('R', Material.GOLD_INGOT);
    lifecrystalRecipe.setIngredient('D', Material.DIAMOND);
    Bukkit.addRecipe(lifecrystalRecipe);

Upvotes: 1

Related Questions