Skip to content

Create Custom Recipes

Traqueur edited this page Oct 5, 2024 · 2 revisions

Create Custom Recipes - RecipesAPI


Overview

RecipesAPI allows you to create custom recipes easily using both Java code and YAML configuration files. This page provides examples of how to implement recipes in both formats, covering shaped, shapeless, and furnace recipes.


All Recipe Type

Here's a table that shows the available recipe types from RecipeType.java and the corresponding allowed and restricted methods of RecipeBuilder. This helps to understand which methods are valid for each recipe type based on the class and method restrictions.

Recipe Type Allowed Methods Restricted Methods
CRAFTING_SHAPED setName(), setResult(), setAmount(), addIngredient(), setPattern() setCookingTime(), setExperience()
CRAFTING_SHAPELESS setName(), setResult(), setAmount(), addIngredient() setPattern(), setCookingTime(), setExperience()
BLASTING setName(), setResult(), setAmount(), addIngredient(), setCookingTime(), setExperience() setPattern()
CAMPFIRE_COOKING setName(), setResult(), setAmount(), addIngredient(), setCookingTime(), setExperience() setPattern()
SMOKING setName(), setResult(), setAmount(), addIngredient(), setCookingTime(), setExperience() setPattern()
STONE_CUTTING setName(), setResult(), setAmount(), addIngredient() setPattern(), setCookingTime(), setExperience(), setCategory()
SMELTING setName(), setResult(), setAmount(), addIngredient(), setCookingTime(), setExperience() setPattern()
SMITHING_TRANSFORM setName(), setResult(), setAmount(), addIngredient() setPattern(), setCookingTime(), setExperience(), setGroup(), setCategory()

Explanation:

  • Allowed Methods: These methods can be used to configure the recipe based on the type.
  • Restricted Methods: These methods cannot be used or are irrelevant for the specific recipe type. If used, they will throw an exception.

Key Notes:

  1. CRAFTING_SHAPED:

    • Requires a pattern (setPattern()).
    • Cannot use cooking-related methods (setCookingTime(), setExperience()).
  2. CRAFTING_SHAPELESS:

    • No specific pattern required.
    • Same restrictions as CRAFTING_SHAPED for cooking-related methods.
  3. Smelting-related Types (BLASTING, CAMPFIRE_COOKING, SMOKING, SMELTING):

    • Allow cooking time (setCookingTime()) and experience (setExperience()).
    • No pattern required (setPattern()).
  4. STONE_CUTTING:

    • Restricted from using cooking, pattern, and category methods.
  5. SMITHING_TRANSFORM:

    • Only allows basic recipe setup (setName(), setResult(), setAmount(), addIngredient()).
    • Cannot use group or category options.

This table provides a clear guide on how to use RecipeBuilder effectively depending on the recipe type you're working with.


Java Implementation

Using Java, you can create recipes programmatically with the RecipeBuilder class. Below are examples of different types of recipes implemented in TestPlugin.java.

Example 1: Shapeless Recipe

A shapeless recipe doesn't have a specific arrangement for the ingredients. Here's how to create one:

ItemRecipe recipe = new RecipeBuilder()
        .setType(RecipeType.CRAFTING_SHAPELESS)   // Shapeless crafting recipe
        .setName("example-simple")                // Name of the recipe
        .setResult(new ItemStack(Material.DIAMOND)) // Output of the recipe
        .setAmount(64)                            // Amount of the result
        .addIngredient(Material.DIRT)             // Ingredient: Dirt
        .build();

recipesAPI.addRecipe(recipe);

In this example, players can craft 64 diamonds using any arrangement of dirt.

Example 2: Shaped Recipe

A shaped recipe requires the ingredients to be placed in a specific pattern.

ItemRecipe recipe2 = new RecipeBuilder()
        .setType(RecipeType.CRAFTING_SHAPED)      // Shaped crafting recipe
        .setName("example-shaped")                // Name of the recipe
        .setResult(new ItemStack(Material.DIAMOND)) // Output of the recipe
        .setAmount(64)                            // Amount of the result
        .setPattern("DDD", "DID", "DDD")          // Pattern: D = Dirt, I = Diamond
        .addIngredient(Material.DIRT, 'D')        // Ingredient: Dirt for 'D'
        .addIngredient(Material.DIAMOND, 'I')     // Ingredient: Diamond for 'I'
        .build();

recipesAPI.addRecipe(recipe2);

Here, the player must arrange the ingredients in a specific way to craft the diamonds.

Example 3: Smelting Recipe

A smelting recipe allows items to be created in furnaces. Here is how to implement it:

ItemRecipe recipe4 = new RecipeBuilder()
        .setType(RecipeType.SMELTING)             // Smelting recipe
        .setName("example-furnace")               // Name of the recipe
        .setResult(new ItemStack(Material.DIAMOND)) // Output of the recipe
        .setAmount(64)                            // Amount of the result
        .addIngredient(ingredient, true)          // Ingredient with metadata
        .setCookingTime(10)                       // Cooking time in ticks
        .build();

recipesAPI.addRecipe(recipe4);

In this case, a custom ingredient is smelted to produce diamonds.


YAML Implementation

RecipesAPI also allows you to define custom recipes through YAML files. The result of the recipe is an ItemStack encoded in Base64 format. This allows for handling custom items with unique properties such as enchantments, custom names, and lore.

Here’s an example of a basic example.yml configuration for a shaped recipe:

Example YAML Configuration (Shaped Recipe)

# Type of the recipe
# CRAFTING_SHAPED, CRAFTING_SHAPELESS, SMELTING, SMITHING_TRANSFORM
# SMOKING, BLASTING, CAMPFIRE_COOKING, STONE_CUTTING
type: CRAFTING_SHAPED

# Not for Smithing recipes
group: example

# Not for Smithing recipes
# FOOD, BLOCKS, MISC for smelting recipes
# BUILDING, REDSTONE, EQUIPMENT, MISC for crafting recipes
category: MISC

pattern:
  - "##"
  - "##"

result:
  item: "H4sIAAAAAAAA/02QPUsDQRCGJ5dEC4vE2Ala2dhsKkEIKYSgHFwSIYWC1V4cjsvt3m72Zo+LhWCjhY2F1lpY5rf4G8RaC21s3XxAnGYG5pl33pnpF1QzA9vKRCy0SRITsxQLFit2ZrjWaH5OTz6/G6rqQSmAsuSaoBaMeM6bM7DZ5bpVaCdxMFSSRUpFApkrpUpdEgKHxHwpLfFQoIP3BmhiLuIrvDxWRsIyPPACqCQ4yQgaC3nB06jZD0dOoRXAWs6FxWwM11AptDWwdTHH2AxjS+zh7fy5nu0LD6DQTrVM4LXbBKWcoEITjXY2v7bo7P67OU5zTEmZCfMJ5YD4MHEnba4W+ClhhKbx8fL6e3N36J7hQ3VuqTBQX3E9K0M0t9OnnY3H9/ulj9qYYL3jH3X7vc4fK871uHEBAAA="
  amount: 64

ingredients:
  - item: "material:stone"
    sign: '#'

YAML Fields Explained:

  • type: The type of recipe (CRAFTING_SHAPED in this case).
  • group: Used to group similar recipes (not used for Smithing recipes).
  • category: Defines where the recipe appears in the crafting UI (MISC, BUILDING, etc.).
  • pattern: The crafting grid pattern. Here, # represents stone blocks.
  • result: Specifies the item created (material:diamond) and the amount (64).
  • ingredients: A list of the ingredients used in the recipe. Each item is mapped to a symbol in the pattern (# for stone).

Example for Smelting Recipe (YAML)

For smelting, you can also define the recipe like this:

type: SMELTING
group: example
category: MISC
result:
  item: "H4sIAAAAAAAA/02QPUsDQRCGJ5dEC4vE2Ala2dhsKkEIKYSgHFwSIYWC1V4cjsvt3m72Zo+LhWCjhY2F1lpY5rf4G8RaC21s3XxAnGYG5pl33pnpF1QzA9vKRCy0SRITsxQLFit2ZrjWaH5OTz6/G6rqQSmAsuSaoBaMeM6bM7DZ5bpVaCdxMFSSRUpFApkrpUpdEgKHxHwpLfFQoIP3BmhiLuIrvDxWRsIyPPACqCQ4yQgaC3nB06jZD0dOoRXAWs6FxWwM11AptDWwdTHH2AxjS+zh7fy5nu0LD6DQTrVM4LXbBKWcoEITjXY2v7bo7P67OU5zTEmZCfMJ5YD4MHEnba4W+ClhhKbx8fL6e3N36J7hQ3VuqTBQX3E9K0M0t9OnnY3H9/ulj9qYYL3jH3X7vc4fK871uHEBAAA="
  amount: 64
ingredients:
  - item: "material:stone"
cooking-time: 100
experience: 10
  • cooking-time: Time required for smelting (in ticks).
  • experience: Experience gained when the item is smelted.

Conclusion

With RecipesAPI, you can create recipes both programmatically in Java or via YAML configuration files. Java allows for more flexibility, especially when dealing with complex ingredients, while YAML provides a simpler way to manage recipes without writing additional code.