Recipe Example

Discussion in 'Wiki Discussion' started by jflory7, May 22, 2015.

  1. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Recipe Example

    Custom Recipes

    Creating custom recipes and registering them with Bukkit.



    In this code snippet, we will show you how to create custom items with a custom recipe. We will be making an emerald sword, with the diamond sword as the base item.


    What you'll need(top)

    Today, we will need a class extending JavaPlugin, with the onEnable method.

    Variables(top)

    Now lets start. We will need in total 3 variables inside of the onEnable method.
    Code (Java):
    // Our custom variable which we will be changing around.
    ItemStack item = new ItemStack(Material.DIAMOND_SWORD);

    // The meta of the diamond sword where we can change the name, and properties of the item.
    ItemMeta meta = item.getItemMeta();

    // We will initialise the next variable after changing the properties of the sword
    Here we modify the properties of the sword to make them stand out from a normal diamond sword.
    Code (Java):
    // This sets the name of the item.
    // Instead of the ┬ž symbol, you can use ChatColor.<color>
    meta.setDisplayName("┬žaEmerald Sword");

    // Set the meta of the sword to the edited meta.
    item.setItemMeta(meta);

    // Add the custom enchantment to make the emerald sword special
    // In this case, we're adding the permission that modifies the damage value on level 5
    // Level 5 is represented by the second parameter. You can change this to anything compatible with a sword
    item.addEnchantment(Enchantment.DAMAGE_ALL, 5);

    Creating the recipe(top)

    Here is where all of the fun happens. This is where our third variable is created and used.
    Code (Java):
    // create a NamespacedKey for your recipe
    NamespacedKey key = new NamespacedKey(this, "emerald_sword");

    // Create our custom recipe variable
    ShapedRecipe recipe = new ShapedRecipe(key, item);

    // Here we will set the places. E and S can represent anything, and the letters can be anything. Beware; this is case sensitive.
    recipe.shape(" E ", " E ", " S ");

    // Set what the letters represent.
    // E = Emerald, S = Stick
    recipe.setIngredient('E', Material.EMERALD);
    recipe.setIngredient('S', Material.STICK);

    // Finally, add the recipe to the bukkit recipes
    Bukkit.addRecipe(recipe);

    All done!(top)

    Now you've finished creating your custom recipe.
    All together it should look like this:
    Code (Java):
       public void onEnable() {
            // Our custom variable which we will be changing around.
            ItemStack item = new ItemStack(Material.DIAMOND_SWORD);

            // The meta of the diamond sword where we can change the name, and properties of the item.
            ItemMeta meta = item.getItemMeta();

            // We will initialise the next variable after changing the properties of the sword
     
            // This sets the name of the item.
            meta.setDisplayName(ChatColor.GREEN + "Emerald Sword");

            // Set the meta of the sword to the edited meta.
            item.setItemMeta(meta);

            // Add the custom enchantment to make the emerald sword special
            // In this case, we're adding the permission that modifies the damage value on level 5
            // Level 5 is represented by the second parameter. You can change this to anything compatible with a sword
            item.addEnchantment(Enchantment.DAMAGE_ALL, 5);
     
       
          // create a NamespacedKey for your recipe
          NamespacedKey key = new NamespacedKey(this, "emerald_sword");

          // Create our custom recipe variable
          ShapedRecipe recipe = new ShapedRecipe(key, item);

            // Here we will set the places. E and S can represent anything, and the letters can be anything. Beware; this is case sensitive.
            recipe.shape(" E ", " E ", " S ");

            // Set what the letters represent.
            // E = Emerald, S = Stick
            recipe.setIngredient('E', Material.EMERALD);
            recipe.setIngredient('S', Material.STICK);

            // Finally, add the recipe to the bukkit recipes
            Bukkit.addRecipe(recipe);
        }
    And make sure all of your imports are from org.bukkit.
    Here is the correct list of imports:
    Code (Java):
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    And make sure your main class with this code in the onEnable method extends JavaPlugin from org.bukkit.plugin.java.JavaPlugin;

    Original source from md-5's small plugins repo on github:
    https://github.com/md-5/SmallPlugin...word/src/main/java/net/md_5/EmeraldSword.java

    Original article:
    This will create a simple recipe for an "Emerald Sword"
    Code (Java):
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    public class EmeraldSword extends JavaPlugin {
       @Override
       public void onEnable() {
          //create an ItemStack, this shall be the final output
          ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
          ItemMeta itemMeta = sword.getItemMeta();
     
          //set the display name of the item.
          itemMeta.setDisplayName(ChatColor.GREEN + "Emerald Sword");

          //make sure to set the ItemMeta back to the ItemStack!
          sword.setItemMeta(itemMeta);
          sword.addEnchantment(Enchantment.DAMAGE_ALL, 5);

          //create a shaped recipe, for shapeless recipe use ShapelessRecipe
          ShapedRecipe recipe = new ShapedRecipe(sword);

          //E and S just marks the material, any letter is okay. A space means no item in that slot.
          recipe.shape(" E ", " E ", " S ");
          recipe.setIngredient('E', Material.EMERALD);
          recipe.setIngredient('S', Material.STICK);
          Bukkit.addRecipe(recipe);
        }
    }
    Original Source:
    https://github.com/md-5/SmallPlugin...word/src/main/java/net/md_5/EmeraldSword.java