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:
publicclassCustomInventoryDescriptorimplementsInventoryDescriptor {publicstaticfinalString INVENTORY_ID ="custom-inventory";privatestaticfinalString INVENTORY_CONFIG_PATH ="menus/waypoint-icons-menu.yml";privatefinalPlugin plugin;privatefinalInventoryConfigDAO inventoryConfigDAO;publicCustomInventoryDescriptor(Plugin plugin,InventoryConfigDAO inventoryConfigDAO) {this.plugin= plugin;this.inventoryConfigDAO= inventoryConfigDAO; } @OverridepublicStringgetInventoryResourceFile() {// Path to the configuration file of the inventory in the plugin's resources.return INVENTORY_CONFIG_PATH; } @OverridepublicPathgetInventoryConfigFile() {// Path to the inventory configuration file in the plugin's folder.returnPaths.get(this.plugin.getDataFolder() +File.separator+ INVENTORY_CONFIG_PATH); } @OverridepublicStringgetInventoryId() {// This id must be unique. t is also recommended to only use the // following characters: A-Z, a-z, 0-9, -, _.return INVENTORY_ID; } @OverridepublicInventoryConfigDAOgetInventoryConfigDAO() {returnthis.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: