> For the complete documentation index, see [llms.txt](https://syrows-development.gitbook.io/craftventory/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://syrows-development.gitbook.io/craftventory/advanced-concepts/enhancements.md).

# Enhancements

### How does CraftVentory work?

To understand the enhancement concept and why it was implemented in CraftVentory, you first need to have a brief overview on how the library works. CraftVentory is built on three modules:

* **Configuration**: Responsible for loading an inventory from a configuration file and store this configuration as a set of Java objects.
* **Transform**: Bridge between the Configuration module and the Inventory module which is responsible for dynamically converting the configuration to a working inventory.
* **Inventory**: The working inventory that can be opened and viewed by players in-game, with events and actions.

The image below illustrates the links between all of these modules:

<figure><img src="/files/l2pDnUjOuKkmHKWCdxoO" alt=""><figcaption></figcaption></figure>

### The enhancement concept

During the **Transformation** process, each inventory property is converted by a specific **provider** which is responsible to do the transformation. This enables to **customize** the behavior dependently of the transformed property.

The **enhancement step** takes place during the **Transformation** process and is executed by a provider on the property it handles. It allows to add **custom Java code** to **customize** and **enhance** properties programatically **before** they are provided and **without changing** the original configuration.

Enhancements work with two specific objects:

* **Context:** Class that contains additional data that may be useful for the enhancement. See [this page](/craftventory/advanced-concepts/context.md) for more information.
* **DTO (Data Transfer Object):** Class that contains the inventory properties currently transformed. It provides getters and setters to modify some of these properties.

### Create an enhancement

To create a new enhancement, you first need to know the **name** and the **class** of the **DTO** it will manipulate. Below is the list of all the available DTO:

| Name                            | Class                   |
| ------------------------------- | ----------------------- |
| `TITLE`                         | `TitleDto`              |
| `INVENTORY_TYPE`                | `InventoryTypeDto`      |
| `INVENTORY_ITEM`                | `InventoryItemDto`      |
| `PAGINATION`                    | `PaginationDto`         |
| `PAGINATION_ITEM`               | `PaginationItemDto`     |
| `PAGINATION_PREVIOUS_PAGE_ITEM` | `PaginationPageItemDto` |
| `PAGINATION_NEXT_PAGE_ITEM`     | `PaginationPageItemDto` |

Enhancements are defined using the generic `Enhancement` class. The following code gives an example of enhancement for pagination items.

```java
Enhancement<PaginationItemDto> enhancement = new Enhancement<>() {

    @Override
    public void enhance(PaginationItemDto dto, Context context) {
        
        // We only want to enhance the pagination with id 'pagination-1'.
        if(!dto.getPaginationId().equals("pagination-1")) {
            return;
        }
        
        // Enhancing the pagination item. If the slot is even, then setting a diamond.
        // If it is odd, setting an emerald.
        Integer slot = context.getData(CommonContextKey.SLOT.name(), Integer.class);
        Material material = slot % 2 == 0 ? Material.DIAMOND : Material.EMERALD;
        
        dto.getItem().setType(material);
    }

    @Override
    public Class<PaginationItemDto> getDTOClass() {
        // The class of the DTO the enhancement manipulates.
        return PaginationItemDto.class;
    }

    @Override
    public String getId() {
        // The unique id of the enhancement.
        return "pagination-1-item-enhancement";
    }
};
```

### Register an enhancement

Enhancements are registered specifically for an inventory in its inventory descriptor and are then provided to its provider.&#x20;

The following code registers the enhancement created in the section above by redefining the `addEnhancements(EnhancementManager manager)` method of the `InventoryDescriptor` interface:

```java
public class CustomInventoryDescriptor implements InventoryDescriptor {

    ...

    @Override
    public void addEnhancements(EnhancementManager manager) {
    
        Enhancement<PaginationItemDto> enhancement = new Enhancement<>() {

            @Override
            public void enhance(PaginationItemDto dto, Context context) {

                if(!dto.getPaginationId().equals("pagination-1")) {
                    return;
                }

                Integer slot = context.getData(CommonContextKey.SLOT.name(), Integer.class);
                Material material = slot % 2 == 0 ? Material.DIAMOND : Material.EMERALD;

                dto.getItem().setType(material);
            }

            @Override
            public Class<PaginationItemDto> getDTOClass() {
                return PaginationItemDto.class;
            }

            @Override
            public String getId() {
                return UUID.randomUUID().toString();
            }
        };
        
        manager.addEnhancement(DtoNameEnum.PAGINATION_ITEM.name(), enhancement);
    }

    ...

}
```

{% hint style="info" %}
In this code, you must provide as first argument the name of the DTO used. This name can be found in the table defined in a previous section of this page.
{% endhint %}

**Result:**

<div align="left"><figure><img src="/files/i3hNmrEMNp76pzU3ZDHX" alt="" width="300"><figcaption></figcaption></figure></div>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://syrows-development.gitbook.io/craftventory/advanced-concepts/enhancements.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
