Layouts - DevNatan/inventory-framework GitHub Wiki
Layouts is a InventoryFramework feature that let you organize items in a view by “drawing” where each item should go.
Other Inventory Framework features integrate with layouts out of the box. For instance, when using Pagination, the paginated items automatically adjust to the layout without any additional configuration.
Configuration
In your view configuration, you can define which layout will be applied to the view. Before doing so, keep in mind the following:
- A layout is represented as a
String[], where each string corresponds to a row. - The number of rows in the array must match the number of rows in the inventory.
- Each string in the array must have the same length as the number of inventory columns.
- If you define a layout but don’t specify a fixed inventory size, the inventory will automatically use the dimensions of the layout.
@Override
public void onInit(ViewConfigBuilder config) {
config.layout(yourLayoutHere);
}
Assigning Layout Items
Each slot in your layout corresponds to a character.
You can map items to those characters using layoutSlot.
In the example below, both “A” and “#” represent items that will be defined later in the onFirstRender trigger.
@Override
public void onInit(@NotNull ViewConfigBuilder config) {
config.layout("### A ###");
}
@Override
public void onFirstRender(@NotNull RenderContext render) {
render.layoutSlot('A', new ItemStack(Material.DIAMOND));
render.layoutSlot('#')
.withItem(new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
}
Extending layout slot behavior
You may have noticed that even if there are multiple “#” symbols in the layout, a single layoutSlot('#', ...) call is enough to fill all of them. Each matching character automatically gets assigned the same item or behavior.
If you want to change which item is displayed based on the slot index, you can extend the layoutSlot call by providing a lambda that receives the slot’s index and item builder..
@Override
public void onInit(@NotNull ViewConfigBuilder config) {
config.layout("### A ###");
}
@Override
public void onFirstRender(@NotNull RenderContext render) {
render.layoutSlot('A', new ItemStack(Material.DIAMOND));
render.layoutSlot('#', (index, item) -> item.withItem(
new ItemStack(Material.BLACK_STAINED_GLASS_PANE, index + 1))
);
}
Example Usage
Example: In the example below, we use layout to create a black glass pane pattern around the inventory.
@Override
public void onInit(@NotNull ViewConfigBuilder config) {
config.layout(
"#########",
"# #",
"# #",
"# #",
"# #",
"#########"
);
}
@Override
public void onFirstRender(@NotNull RenderContext render) {
render.layoutSlot('#', new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
}
Code Examples
After configuring the layout, it is necessary to define the item that will be rendered for each character, use the layoutSlot function of the rendering context for that.
In the example below an item is rendered for character 'A' of the layout.
@Override
public void onInit(ViewConfigBuilder config) {
config.layout(
" ",
" AAAAAAA ",
" AAAAAAA ",
" AAAAAAA ",
" AAAAAAA ",
" "
);
}
@Override
public void onFirstRender(RenderContext render) {
render.layoutSlot('A', new ItemStack(Material.GOLD_INGOT));
}
Multiple characters can be used in the same layout. In the example below the characters "A" and "B" are rendered for different items.
@Override
public void onInit(ViewConfigBuilder config) {
config.layout(
" ",
" AAAAAAA ",
" A BBB A ",
" A BBB A ",
" AAAAAAA ",
" ");
}
@Override
public void onFirstRender(RenderContext render) {
render.layoutSlot('A', new ItemStack(Material.DIAMOND));
render.layoutSlot('B', new ItemStack(Material.GOLD_INGOT));
}
There is a function that exposes the current iteration index for each character rendered in the layout, you can use that for whatever you want to do with those indexes.
In the example below we render an item in the letter F whose value is the current iteration index.
@Override
public void onInit(ViewConfigBuilder config) {
config.layout(
" ",
" FFFFF ",
" FFFFF ",
" FFFFF ",
" FFFFF ",
" "
);
}
@Override
public void onFirstRender(RenderContext render) {
render.layoutSlot('F', (index, builder) ->
builder.withItem(new ItemStack(Material.DIAMOND, index))
);
}