CraftVentory
  • Welcome
  • get started
    • Inventory configuration
      • Item actions
    • Initialize the library
    • Declare an inventory
    • Open a inventory
  • Advanced concepts
    • Placeholders
    • Pagination
    • Data storage
    • Enhancements
    • Hooks
    • Custom item actions
    • Context
    • I18n
Powered by GitBook
On this page
  • How does CraftVentory work?
  • The enhancement concept
  • Create an enhancement
  • Register an enhancement
Export as PDF
  1. Advanced concepts

Enhancements

This page describes the enhancement concept to customize inventories programmatically.

PreviousData storageNextHooks

Last updated 6 months ago

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:

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 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.

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.

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

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);
    }

    ...

}

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.

Result: