Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
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.
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:
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.
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:
The page Pagination describes how to create a new pagination to paginate a large list of results in the inventory.
The page Placeholders describes how to create a new placeholder to be able to display custom values.
The page Enhancements describes how you can enhance some inventory properties manually using Java code.
The page Hooks describes how you can execute Java code when events happen for an inventory.
This page describes how to configure an inventory in YAML.
CraftVentory enables developers to create fully customizable inventories from configuration files. YAML is the default file format supported by the library for this purpose.
Each inventory must be defined in a separate configuration file. It is represented by a set of properties which are listed below:
An inventory is composed of a set of items. CraftVentory supports two ways to configure items in configuration files:
Using the default Bukkit YAML representation that supports complex ItemStack
configurations
Using the custom representation that only supports simple item configurations
An item is represented by a set of required properties. All these properties are listed below, with comments on what is configurable and supported.
Welcome to the CraftVentory wiki.
CraftVentory is a Java / Spigot library that facilitates the development of Minecraft inventories. It enables developers to define fully configurable inventories in configuration files and provide them with Java code in their plugins.
This library was developed to facilitate the development of in-game inventories. Indeed, making them fully configurable is an important feature for server administrators but this process is very tedious without an appropriate tool. CraftVentory solves this problem by providing a lot of built-in features to help developers implement fully customizable inventories very easily.
CraftVentory comes with the following features:
Fully customizable inventories / items / paginations from configuration files (YAML support).
Fully customizable actions when clicking on items like sending messages / sounds, executing commands, inventory navigation, etc.
Paginations to paginate a large list of results in an inventory.
Placeholders to display custom values in texts (inventory title, item name / lore, etc.).
Inventory history to enable players to easily navigate between inventories (home, backward, forward).
I18n support.
Enhancements to dynamically modify inventory properties with Java code.
Hooks to execute custom Java code for an inventory when specific events happen.
To use CraftVentory, you can directly include the JAR file as a dependency of your plugin. This JAR file can be downloaded on the Releases page of the GitHub repository of the project.
The library can also be included in your project by using a dependency manager like Maven or Gradle.
After being added as a dependency, you must initialize the library by following this tutorial.
Configuration :
This page describes how to create and register custom placeholders.
A placeholder is used to display a value in a text. The library provides default placeholders but also enables developers to create their own placeholders to display their custom values in texts.
A placeholder is defined by creating a class that implements the Placeholder
interface. The following code represents a placeholder for the player's ping:
When using the Placeholder
interface, several methods must be implemented:
String getName()
Returns the name of the placeholder. It is recommended to use the format %placeholder_name%
.
String getValue(Context context)
Returns the value the placeholder for the provided context.
boolean accept(Context context)
Check that the placeholder can be applied using the provided context.
Note: Some of these methods relies on a
Context
that is explained in the Context page.
To be used in an inventory, a placeholder must be registered in its InventoryDescriptor
. This can be done by redefining the addPlaceholders(PlaceholderManager manager)
method. The following code registers the placeholder created in the previous step of this tutorial:
The library enables developers to paginate items in an inventory and it is then a common use case to need to display some values of what is paginated. This can be done using placeholders.
The process is nearly the same as the previous example. However, the only difference is that, as inventories may contain several paginations, you must ensure that the placeholder is only applied on items of the targeted pagination.
This is done by taking the pagination id as parameter in the class constructor and comparing it in the accept(Context context)
method. The following code gives an example for a pagination that paginates a list of Integer
:
This page describes how to initialize the library.
To use the library and create custom inventories, it first must be initialized by defining a set of reusable Java objects. This is done by using the CraftVentoryLibrary
class that provides several methods to use the default behaviors implemented in the library.
An InventoryService
is a class with two purposes:
Register, store and retrieve inventory providers
Store information about players with an opened inventory and manage inventories' life cycle
An instance of this class with the default behavior provided by the library can be created by using the following code:
Note: We recommand to store this object as an instance variable in the main class of your plugin (eq. the class that extends
JavaPlugin
).
An InventoryConfigDAO
is a class used to load an inventory from a configuration file. By default, the library provides an instance of this class to load inventories from YAML files. The following code uses the library built-in methods to create a new instance of InventoryConfigDAO
:
Note:
ClickActionLoaderFactory
is a class that is used to load item actions from the configuration file.
Here is a complete example:
This page illustrates how data can be stored and shared between inventories.
The InventoryStorage
interface enables to store additional data related to an inventory. CraftVentory provides two types of storage for these additional data : local storage and shared storage.
Local storage is a type of storage specific to a single inventory. That means that all stored data is accessible only for the inventory that holds them.
This type of storage is accessible through the CraftVentory#getLocalStorage()
method.
Shared storage is a type of storage shared between all the inventories in the player's inventory history. That means that stored data is accessible to all the inventories in the history.
This type of storage is accessible through the InventoryViewManager#getSharedStorage()
method.
When the player's inventory history is reset, the shared storage is reset too.
This page describes how to create and register hooks.
A hook is a set of instructions to be executed when an inventory event is performed for a specific inventory.
Hooks are represented by the generic Hook functional interface. The following code illustrates how to create a hook.
Hooks can be applied on the following events:
FastInventoryBeforeOpenEvent
: Called before opening an inventory.
FastInventoryAfterOpenEvent
: Called after an inventory has been opened.
FastInventoryClickEvent
: Called when a player clicks on an item.
FastInventoryCloseEvent
: Called when an inventory is closed.
Hooks are registered specifically for an inventory in its inventory descriptor by redefining the addHooks(HookManager manager)
method.
The HookManager
interface provides the addHook(hookId, eventClass, hook)
method to register a new hook.
In CraftVentory, we do not rely on Bukkit events. Indeed, we prefer using hooks which are registered and executed specifically for an inventory instead of events which are executed for all inventories.
This page describes how to open an inventory to a player.
An InventoryViewer
is an encapsulation of a player and its associated inventories, which are handled in the an InventoryViewManager
. This class provides methods for opening / closing inventories and to navigate through the player's inventory history.
An InventoryViewer
is created automatically for each player joining the server and then stored in an InventoryService
. The viewer associated to a player can be retrieved as in the following code:
An InventoryProvider is responsible for providing an inventory. All the providers are stored in an InventoryService and can be retrieved as in the following code:
The following code shows how you can open an inventory to a player.
The open()
and close()
methods available in the CraftVentory
interface should never be used to open or close an inventory. You must use the methods from the InventoryViewManager
interface instead.
This page describes how to configure item actions.
In CraftVentory, an item action is a behavior executed when a player clicks on an item. An item may have as many actions as you want.
Item actions are fully configurable and each action has its own set of customizable properties. In the inventory configuration, each action is declared in a subsection of the actions
section of an item.
In the list below, you can find all the actions provided by default by the library.
Close the opened inventory.
Send a message to the inventory viewer.
Broadcast a message in the chat.
Make the inventory viewer execute a list of commands.
When performed, this action makes the server execute a command.
Play a sound to the inventory viewer.
Trigger an update of the content of an opened inventory.
Trigger an update of the paginations in an opened inventory.
Open a new inventory.
Open the root inventory in the viewer's history.
Open a previously opened inventory in the history which is before the current one in the history.
Open a previously opened inventory in the history which is after the current one in the history.
CraftVentory enables to configure the type of click a user must do to execute an action by adding the click-types
property.
Example:
This page describes how to register paginations in an inventory provider.
Pagination is the fact of splitting a list of values into pages. This is very useful when you want to display a large list of values in a container with a restricted size.
In CraftVentory, you can paginate a list of Java objects and convert each item to an InventoryItem
to be displayed in an inventory.
To learn how to configure a pagination in an inventory configuration file, please refer to the Paginations configuration section.
Like inventories, paginations are configurable. As a pagination relies on a list of Java object, there must be a link between its configuration and this list.
Let's say we want to paginate the following list of integers:
To paginate this list, you must create a pagination provider to let the library understand what data is paginated. This can be done by redefining the addProviders(ProviderManager manager)
method from the InventoryDescriptor
interface:
The PaginationProvider
class takes the following paramaters in its constructors:
paginationId
: The id of the pagination. It must be the same as the one defined in the configuration file of the inventory.
dataType
: The data type that will be paginated. In the example, it is an Integer
but it must be any Java class type.
supplier
: A function that retrieves and returns the list of values to paginate. Here, we simply use the variable values defined above but we can imagine that the list comes from another class.
An inventory can contain several paginations. Each pagination must have its own pagination provider.
Configuration :
This page describes the enhancement concept to customize inventories programmatically.
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:
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.
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:
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.
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:
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:
This page describes how to develop custom item actions.
CraftVentory comes with a set of predefined item actions. However, to meet your needs, you may want to create new item actions. This page explains step by step how this can be achieved.
The first step consists in creating the action itself. This is done by creating a new class that implements the interface ClickAction
. In the following example, we are implementing an action which executes a command when a player clicks on an item.
When implementing the ClickAction
interface, two methods must be defined:
execute(FastInventoryClickEvent event)
: Method executed when a player clicks on an item which has our action.
getName()
: Returns the name of the action. This name must identify uniquely the action and it is encouraged to use a globally visible constant to store it.
Like inventories and items, actions can be configured in configuration files. Thus, it is necessary to tell the library how to load each action.
This is done by creating a class that implements the ClickActionLoader<T>
interface. Here, we only want to load our action from YAML files so the generic parameter will be a ConfigurationSection
.
When implementing the ClickActionLoader<T>
interface, two methods must be defined:
load(ConfigurationSection section)
: Method executed to load the action. When an error occurs, this method should throw an InventoryConfigException
.
getName()
: Returns the name of the action. This name must be the same as the one defined in the class that implements the ClickAction
interface.
The last step consists in registering our action loader in a factory to tell the library to use it. If you are using the default ClickActionLoaderFactory
provided by the library, this can be done with the following code:
The page describes the context concept implemented in the library.
In the library, a Context
is a class that store values identified by keys which may differ depending on where the context is created and used. It is used in value providers and placeholders.
By default, several values may be added by the library and present in a context. The CommonContextKeyEnum
is an enumeration that defines the keys of these common values.
These keys are described in the following table:
Some keys are not available depending on where the context is declared and used. You must always check that the key is present before retrieving its associated value.
This page describes how to support text internationalization (i18n).
The I18n
interface was developed as a bridge between CraftVentory and any internationalization library. It consists in a single method whose goal is to retrieve a text using a key and the player who requests it.
To support text internationalization, the first step is to create a class that implements this interface and that does the mapping with an internationalization library. Then, the created class can be used in inventory descriptors by redefining the getI18n()
method and everything should be automatically translated.
CraftVentory is not an internationalization library. It only provides a bridge to be mapped with one.
VIEWER
Player
Everywhere
The player who is viewing the inventory
INVENTORY
FastInventory
Everywhere
The inventory viewed
SLOT
Integer
Item
The slot of the item
PAGINATION_ID
String
Pagination
The id of the pagination
PAGINATION_ITEM
Depends on what is paginated
Pagination item
The current pagination item