# Enchanter

## ✨ Enchanter Class

A merchant that enchants items.

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

***

### 📦 Fields

| Access   | Field                | Type             | Description                                          |
| -------- | -------------------- | ---------------- | ---------------------------------------------------- |
| `public` | `enchantTargets`     | `EnchantTargets` | The types of items this enchanter can enchant.       |
| `public` | `supportedItemTypes` | `List<ItemType>` | A list of item types this enchanter can enchant.     |
| `public` | `supportedItems`     | `List<Item>`     | A list of specific items this enchanter can enchant. |

***

### 🧰 Methods

| Access   | Method                                                                           | Returns | Description                                                                  |
| -------- | -------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------- |
| `public` | `CanEnchant(ItemStack itemStack, ItemStack enchanter, bool invokeFailureReason)` | `bool`  | Checks if the given item stack can be enchanted with the provided enchanter. |
| `public` | `TryEnchant(ItemStack itemStack, ItemStack enchanter, bool applyCost = true)`    | `bool`  | Attempts to enchant an item stack. Returns true if successful.               |
| `public` | `Enchant(ItemStack itemStack, ItemStack enchanter, bool applyCost = true)`       | `void`  | Enchants an item stack 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 EnchantTargets`

Defines what this enchanter can enchant.

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

## Creating Enchanters

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

```csharp
Enchanter myEnchanter = ScriptableObject.CreateInstance<Enchanter>();
```

## Enchanting

An `Enchanter` requires a reference to an [Enchantable](https://stylishesper.gitbook.io/inventool/scripting/enchanting#enchantable-items) and [Enchanter](https://stylishesper.gitbook.io/inventool/scripting/enchanting#enchanter-items) item.

You can check if it's possible to enchant an item with the `CanEnchant` method. The first parameter is the enchantable item, the second is the enchanter item, and the third is a`bool` that determines if the failure reason should be invoked. This method will return `true` if the enchanter can enchant the enchantable item. Otherwise, false will be returned.

```csharp
bool result = myEnchanter.CanEnchant(myEnchantableItem, myEnchanterItem, true);
```

To officially enchant an item, use the `TryEnchant` method. This is similar to the `CanEnchant` method, except it will enchant the item if successful.

```csharp
bool result = myEnchanter.TryEnchant(myEnchantableItem, myEnchanterItem);
```
