Skip to content

Commit bba1052

Browse files
Add second stonecutter window example
1 parent ab30d2f commit bba1052

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
992 KB
Binary file not shown.

docs/invui2/window.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,114 @@ The smithing window can be created using `#!kotlin SmithingWindow.builder()`. It
479479

480480
### Stonecutter Window
481481

482-
The stonecutter window can be created using `#!kotlin StonecutterWindow.builder()`. It consists of a `2x1` upper gui, a `4xN` buttons gui, where `N` can be any number, and a `9x4` lower gui. You can use the scroll bar to scroll through the buttons. A button can be selected and deselected by both the server and the player. You can register a selection handler via `addSelectedSlotChangeHandler`.
482+
The stonecutter window can be created using `#!kotlin StonecutterWindow.builder()`. It consists of a `2x1` upper gui, a `4xN` buttons gui, where `N` can be any number, and a `9x4` lower gui. You can use the scroll bar to scroll through the buttons. A button can be selected and deselected by both the server and the player. You can register a selection handler via `addSelectedSlotChangeHandler`. Clicking a button also fires the click handlers of the corresponding item.
483483

484484
![](assets/img/window/stonecutter.png){width=500}
485485

486+
??? example "Example: Using the buttons as tabs"
487+
488+
=== "Kotlin"
489+
```kotlin
490+
val woolTypes = Tag.ITEMS_WOOL.values.sortedBy { it.ordinal }
491+
492+
val lowerGui = TabGui.builder()
493+
.setStructure(
494+
"x x x x x x x x x",
495+
"x x x x x x x x x",
496+
"x x x x x x x x x",
497+
"x x x x x x x x x"
498+
)
499+
.addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
500+
.setTabs(woolTypes.map { Gui.of(9, 4, Item.simple(ItemStack.of(it))) })
501+
.build()
502+
503+
val buttonsGui = Gui.empty(4, Math.ceilDiv(woolTypes.size, 4))
504+
for (i in woolTypes.indices) {
505+
buttonsGui[i] = Item.builder()
506+
.setItemProvider(ItemStack.of(woolTypes[i]))
507+
.addClickHandler { lowerGui.tab = i }
508+
.build()
509+
}
510+
511+
StonecutterWindow.builder()
512+
.setButtonsGui(buttonsGui)
513+
.setLowerGui(lowerGui)
514+
.setSelectedSlot(0) // (1)!
515+
.open(player)
516+
```
517+
518+
1. By default, no button is selected initially. However, since the default tab is `0`, having button `0` selected initially makes sense in this case.
519+
520+
=== "Kotlin (DSL)"
521+
!!! warning "Experimental, see [Declarative Menus](declarative-menus.md)"
522+
523+
```kotlin
524+
stonecutterWindow(player) {
525+
val woolTypes = Tag.ITEMS_WOOL.values.sortedBy { it.ordinal }
526+
lowerGui by tabGui(
527+
"x x x x x x x x x",
528+
"x x x x x x x x x",
529+
"x x x x x x x x x",
530+
"x x x x x x x x x"
531+
) {
532+
'x' by Markers.CONTENT_LIST_SLOT_HORIZONTAL
533+
tabs by woolTypes.map { Gui.of(9, 4, Item.simple(ItemStack.of(it))) }
534+
tab by selectedSlot // (1)!
535+
}
536+
buttonsGui by Gui.empty(4, Math.ceilDiv(woolTypes.size, 4)).also { gui ->
537+
for (i in woolTypes.indices) {
538+
gui[i] = Item.simple(ItemStack.of(woolTypes[i]))
539+
}
540+
}
541+
}.open()
542+
```
543+
544+
1. This combines the tab- and selected slot properties such that selecting a button automatically switches the tab and switching the tab automatically selects the corresponding button.
545+
546+
=== "Java"
547+
```java
548+
var woolTypes = Tag.ITEMS_WOOL.getValues().stream()
549+
.sorted(Comparator.comparingInt(Enum::ordinal))
550+
.toList();
551+
552+
var lowerGui = TabGui.builder()
553+
.setStructure(
554+
"x x x x x x x x x",
555+
"x x x x x x x x x",
556+
"x x x x x x x x x",
557+
"x x x x x x x x x"
558+
)
559+
.addIngredient('x', Markers.CONTENT_LIST_SLOT_HORIZONTAL)
560+
.setTabs(
561+
woolTypes.stream()
562+
.map(it -> Gui.of(9, 4, Item.simple(ItemStack.of(it))))
563+
.toList()
564+
)
565+
.build();
566+
567+
var buttonsGui = Gui.empty(4, Math.ceilDiv(woolTypes.size(), 4));
568+
for (var i = 0; i < woolTypes.size(); i++) {
569+
var finalI = i;
570+
buttonsGui.setItem(
571+
i,
572+
Item.builder()
573+
.setItemProvider(ItemStack.of(woolTypes.get(i)))
574+
.addClickHandler(click -> lowerGui.setTab(finalI))
575+
.build()
576+
);
577+
}
578+
579+
StonecutterWindow.builder()
580+
.setButtonsGui(buttonsGui)
581+
.setLowerGui(lowerGui)
582+
.setSelectedSlot(0) // (1)!
583+
.open(player);
584+
```
585+
586+
1. By default, no button is selected initially. However, since the default tab is `0`, having button `0` selected initially makes sense in this case.
587+
588+
![](assets/img/window/stonecutter_buttons_as_tabs.avif){width=500}
589+
486590
??? example "Example: Using the buttons as an inventory"
487591

488592
The following example embeds a `VirtualInventory` into the buttons gui. Note that updating the buttons resets the scroll bar to the top, so this is not a practical use case.

0 commit comments

Comments
 (0)