-
Notifications
You must be signed in to change notification settings - Fork 22
Layouts
Layouts are used to organize the items in a view in an orderly or specific way by "drawing" the spaces where the items will be placed on.
Define in your view configuration the layout that will be used on your view. There are a few points you need to know first:
- Layout is a
String
array and the size of this array must be the same number of rows as the inventory. - Each
String
in the layout must have the same length as the inventory columns count. - If you specify a layout but doesn't specify a fixed container size, the size of the created container will be the size of the layout.
@Override
public void onInit(ViewConfigBuilder config) {
config.layout(your layout here);
}
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))
);
}