# Craftsman

## 🛠️ Craftsman Class

A merchant that crafts items.

> **Namespace:** `Esper.Inventool.Merchants`\
> **Access:** `public`\
> **Type:** `class`\
> **Inherits:** `Merchant`

***

### 📦 Fields

| Access   | Field                | Type             | Description                                        |
| -------- | -------------------- | ---------------- | -------------------------------------------------- |
| `public` | `craftTargets`       | `CraftTargets`   | The types of items this craftsman can craft.       |
| `public` | `supportedItemTypes` | `List<ItemType>` | A list of item types this craftsman can craft.     |
| `public` | `supportedItems`     | `List<Item>`     | A list of specific items this craftsman can craft. |

***

### 🧰 Methods

| Access   | Method                                                | Returns | Description                                                 |
| -------- | ----------------------------------------------------- | ------- | ----------------------------------------------------------- |
| `public` | `CanCraft(Crafter crafter, bool invokeFailureReason)` | `bool`  | Checks if the item can be crafted by this craftsman.        |
| `public` | `TryCraft(Crafter crafter, bool applyCost = true)`    | `bool`  | Attempts to craft an item. Returns true if successful.      |
| `public` | `Craft(Crafter crafter, bool applyCost = true)`       | `void`  | Crafts an item directly.                                    |
| `public` | `Clean()`                                             | `void`  | Cleans the object by removing deleted items and item types. |
| `public` | `Save()` *(Editor only)*                              | `void`  | Saves the object.                                           |

***

### 🧩 Nested Types

#### `public enum CraftTargets`

Defines what this craftsman can craft.

| Name                | Description                                                            |
| ------------------- | ---------------------------------------------------------------------- |
| `Everything`        | Can craft any item.                                                    |
| `SpecificItemTypes` | Can only craft items of the item types listed in `supportedItemTypes`. |
| `SpecificItems`     | Can only craft items listed in `supportedItems`.                       |

***

## Creating Craftsmen

It's not recommended to create craftsmen at runtime, but it is possible. `Craftsman` is a scriptable object which cannot be created as an asset at runtime for later use.

```csharp
Craftsman myCraftsman = ScriptableObject.CreateInstance<Craftsman>();
```

## Crafting

A `Craftsman` requires a reference to a [crafter](https://stylishesper.gitbook.io/inventool/scripting/api/crafting/crafter "mention") to help craft items.

You can check if it's possible to craft an item with the `CanCraft` method. The first parameter is the `Crafter` and the second is a `bool` that determines if the failure reason should be invoked. This method will return `true` if the item in the `Crafter` can be crafted by the `Craftsman` and if the `Crafter`'s `CanCraft` method returns `true`. Otherwise, false will be returned.

```csharp
bool result = myCraftsman.CanCraft(myCrafter, true);
```

To officially craft an item, use the `TryCraft` method. This is similar to the `CanCraft` method, except it will add the item to the player's inventory if successful.

```csharp
bool result = myCraftsman.TryCraft(myCrafter);
```
