Declare an inventory

This page describes how to declare and register an inventory in Java.

In CraftVentory, an inventory is declared using the InventoryDescriptor interface. This interface is then mapped into an InventoryProvider which is a bridge between an InventoryConfig and an in-game inventory.

Create an InventoryDescriptor

Using the InventoryDescriptor interface, you can declare some properties of your inventory: the configuration file to load, the inventory id, its hooks, paginations and enhancements. The following code shows a basic implementation of this interface:

public class CustomInventoryDescriptor implements InventoryDescriptor {

    public static final String INVENTORY_ID = "custom-inventory";
    private static final String INVENTORY_CONFIG_PATH = "menus/waypoint-icons-menu.yml";

    private final Plugin plugin;
    private final InventoryConfigDAO inventoryConfigDAO;

    public CustomInventoryDescriptor(Plugin plugin, InventoryConfigDAO inventoryConfigDAO) {
        this.plugin = plugin;
        this.inventoryConfigDAO = inventoryConfigDAO;
    }

    @Override
    public String getInventoryResourceFile() {
        // Path to the configuration file of the inventory in the plugin's resources.
        return INVENTORY_CONFIG_PATH;
    }

    @Override
    public Path getInventoryConfigFile() {
        // Path to the inventory configuration file in the plugin's folder.
        return Paths.get(this.plugin.getDataFolder() + File.separator + INVENTORY_CONFIG_PATH);
    }

    @Override
    public String getInventoryId() {
        // This id must be unique. t is also recommended to only use the 
        // following characters: A-Z, a-z, 0-9, -, _.
        return INVENTORY_ID;
    }

    @Override
    public InventoryConfigDAO getInventoryConfigDAO() {
        return this.inventoryConfigDAO;
    }
}

The interface also contains default methods you can redefine to register pagination providers, hooks and enhancements. These methods are not shown in the example above.

As these methods are not necessary for each inventory, the choice was make to declare them as default empty methods in the InventoryDescriptor interface. Thus, when using them, you don't need to make a call using the super keyword.

Create an InventoryProvider

An InventoryProvider encapsulates an InventoryDescriptor to create and provide an inventory. To be reused, inventory providers are registered in an InventoryService. The following code illustrates how to create and register a provider using a descriptor:

public class CraftVentoryPlugin extends JavaPlugin {

    private InventoryService inventoryService;

    @Override
    public void onEnable() {
        this.inventoryService = CraftVentoryLibrary.createInventoryService(this);
        this.loadInventoryProviders();
    }

    private void loadInventoryProviders() {

        ClickActionLoaderFactory<ConfigurationSection> factory =
                FastInventoryLibrary.createDefaultClickActionLoaderFactory();

        InventoryConfigDAO dao = FastInventoryLibrary.createDefaultConfigDAO(factory);

        // Register inventory providers here.
        this.inventoryService.createProvider(new CustomInventoryDescriptor(this, dao));
    }
}

Advanced concepts

Paginations

The page Pagination describes how to create a new pagination to paginate a large list of results in the inventory.

Placeholders

The page Placeholders describes how to create a new placeholder to be able to display custom values.

Enhancements

The page Enhancements describes how you can enhance some inventory properties manually using Java code.

Hooks

The page Hooks describes how you can execute Java code when events happen for an inventory.

Last updated