# API

**Assembly:** `Spark.Crafting` **Interface:** `ICraftingPlugin` **Implementation:** `CraftingPlugin`

## Interface

```csharp
public interface ICraftingPlugin
{
    IReadOnlyList<CraftingRecipeEntry> GetAllRecipes();
    CraftingRecipeEntry FindRecipeById(string recipeId);
    CraftingRecipeEntry FindRecipeForItem(ItemEntry craftedItem);
    IReadOnlyList<CraftingRecipeEntry> FindRecipesForItem(ItemEntry craftedItem);
}
```

## Methods

### GetAllRecipes

```csharp
IReadOnlyList<CraftingRecipeEntry> GetAllRecipes()
```

Returns all crafting recipes in the database.

### FindRecipeById

```csharp
CraftingRecipeEntry FindRecipeById(string recipeId)
```

Returns a specific recipe by ID, or `null` if not found.

### FindRecipeForItem / FindRecipesForItem

```csharp
CraftingRecipeEntry FindRecipeForItem(ItemEntry craftedItem)
IReadOnlyList<CraftingRecipeEntry> FindRecipesForItem(ItemEntry craftedItem)
```

Find recipes that produce a given item. `FindRecipeForItem` returns the first match; `FindRecipesForItem` returns all matches.

## CraftingRecipeEntry

Extends `SparkDatabaseEntry`. Defines a crafting recipe.

| Field                | Type                        | Description                |
| -------------------- | --------------------------- | -------------------------- |
| `recipeCategory`     | CraftingRecipeCategoryEntry | Recipe category            |
| `craftedItem`        | ItemEntry                   | Item produced              |
| `craftDuration`      | float                       | Time to craft (seconds)    |
| `autoLearn`          | bool                        | Automatically known        |
| `requiredComponents` | List\<RecipeComponent>      | Materials needed           |
| `onCraftTriggers`    | List\<TriggerEntry>         | Triggers executed on craft |

### RecipeComponent

| Field    | Type      | Description       |
| -------- | --------- | ----------------- |
| `item`   | ItemEntry | Required material |
| `amount` | int       | Quantity needed   |

## CraftingRecipeCategoryEntry

Extends `SparkDatabaseEntry`. Groups recipes into categories for UI organization.

## Events

| Event                 | Description                     |
| --------------------- | ------------------------------- |
| `RecipeLockedEvent`   | Recipe locked for a character   |
| `RecipeUnlockedEvent` | Recipe unlocked for a character |
| `RecipeCraftedEvent`  | Recipe successfully crafted     |

## Commands

| Command            | Fields                                                 | Description     |
| ------------------ | ------------------------------------------------------ | --------------- |
| `CraftItemCommand` | `crafter` (GameObject), `recipe` (CraftingRecipeEntry) | Execute a craft |

## Save Data

### CraftingSaveData

Extends `SaveDataEntry`. Tracks which recipes are unlocked.

**Methods:**

```csharp
bool IsRecipeUnlocked(string recipeId)
void UnlockRecipe(string recipeId)
void LockRecipe(string recipeId)
void ClearUnlockedRecipes()
```

On `InitializeDefaults()`, all recipes with `autoLearn = true` are automatically unlocked.

## Usage

```csharp
var crafting = Spark.GetPlugin<ICraftingPlugin>();
if (crafting != null)
{
    // List all recipes
    var recipes = crafting.GetAllRecipes();

    // Find a specific recipe
    var recipe = crafting.FindRecipeById("iron_sword_recipe");

    // Find recipes that produce an item
    var swordRecipes = crafting.FindRecipesForItem(ironSwordEntry);

    // Craft via command
    Spark.Network?.ExecuteCommand(new CraftItemCommand
    {
        crafter = playerObject,
        recipe = recipe
    });
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sparkframework.dev/documentation/developer-guide/plugins/crafting/api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
