Class DeclarativeGui
- All Implemented Interfaces:
org.bukkit.inventory.InventoryHolder
- Direct Known Subclasses:
StatefulDeclarativeGui
It extends obliviate-invs' Gui, layering declarative UI capability on top. A subclass
only needs to implement build(BuildContext), returning a Widget tree.
Usage example:
public class ShopPage extends DeclarativeGui {
private final List<ItemStack> items;
public ShopPage(Player player, List<ItemStack> items) {
super(player, "shop", "Shop", 6);
this.items = items;
}
@Override
public Widget build(BuildContext context) {
return Column.builder()
.children(
// Title row
Center.builder()
.child(TextDisplay.title("Item Shop"))
.build(),
// Item grid
GridView.builder()
.items(items)
.itemBuilder(item -> ItemButton.builder()
.item(item)
.onClick(() -> buyItem(item))
.build())
.build(),
// Pagination controls
Row.builder()
.children(
PrevPageButton.builder().build(),
PageIndicator.builder().build(),
NextPageButton.builder().build()
)
.build()
)
.build();
}
private void buyItem(ItemStack item) {
// Purchase logic
player.sendMessage("Bought " + item.getType());
}
}
- Since:
- 6.2.0
- Version:
- 1.0.0
- Author:
- UltiTools Team
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final @NotNull Stringprivate booleanprivate final GuiRendererFields inherited from class mc.obliviate.inventory.Gui
player -
Constructor Summary
ConstructorsConstructorDescriptionDeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull String title, int rows) Creates a DeclarativeGui.DeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull String title, @NotNull org.bukkit.event.inventory.InventoryType inventoryType) Creates a DeclarativeGui.DeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull net.kyori.adventure.text.Component title, int rows) Creates a DeclarativeGui with a Component title.DeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull net.kyori.adventure.text.Component title, @NotNull org.bukkit.event.inventory.InventoryType inventoryType) Creates a DeclarativeGui with a Component title. -
Method Summary
Modifier and TypeMethodDescriptionabstract @NotNull Widgetbuild(@NotNull BuildContext context) Builds the Widget tree.@NotNull org.bukkit.entity.PlayerGets the player.protected @NotNull GuiRendererGets the renderer.booleanChecks whether this has been initialized.protected voidMarks that a rebuild is needed.final booleanonClick(@NotNull org.bukkit.event.inventory.InventoryClickEvent event) This is the framework's own bounds-check site (D-09 item 4), not a post-filter hook.final voidonClose(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event) protected booleanonGuiClick(@NotNull org.bukkit.event.inventory.InventoryClickEvent event) Hook method called on a GUI click.protected voidonGuiClose(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event) Hook method called when the GUI closes.protected voidonGuiOpen(@NotNull org.bukkit.event.inventory.InventoryOpenEvent event) Hook method called when the GUI opens.final voidonOpen(@NotNull org.bukkit.event.inventory.InventoryOpenEvent event) protected voidSets state and triggers a rebuild.Methods inherited from class mc.obliviate.inventory.Gui
addItem, addItem, addItem, addItem, addItem, addItem, addItem, addItem, createInventory, createInventory, fillColumn, fillGui, fillGui, fillGui, fillGui, fillRow, getId, getInventory, getItems, getLastSlot, getPlugin, getSize, getTaskList, getTitle, isClosed, onDrag, open, runTaskLater, sendSizeUpdate, sendTitleUpdate, setClosed, setInventory, setSize, setTitle, stopAllTasks, stopTask, updateTask
-
Field Details
-
id
-
renderer
-
initialized
private boolean initialized
-
-
Constructor Details
-
DeclarativeGui
public DeclarativeGui(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String id, @NotNull @NotNull String title, int rows) Creates a DeclarativeGui.- Parameters:
player- the playerid- the GUI IDtitle- the titlerows- the row count
-
DeclarativeGui
public DeclarativeGui(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String id, @NotNull @NotNull String title, @NotNull @NotNull org.bukkit.event.inventory.InventoryType inventoryType) Creates a DeclarativeGui.- Parameters:
player- the playerid- the GUI IDtitle- the titleinventoryType- the inventory type
-
DeclarativeGui
public DeclarativeGui(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String id, @NotNull @NotNull net.kyori.adventure.text.Component title, int rows) Creates a DeclarativeGui with a Component title.- Parameters:
player- the playerid- the GUI IDtitle- the title (Component)rows- the row count
-
DeclarativeGui
public DeclarativeGui(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String id, @NotNull @NotNull net.kyori.adventure.text.Component title, @NotNull @NotNull org.bukkit.event.inventory.InventoryType inventoryType) Creates a DeclarativeGui with a Component title.- Parameters:
player- the playerid- the GUI IDtitle- the title (Component)inventoryType- the inventory type
-
-
Method Details
-
build
Builds the Widget tree.Subclasses must implement this method, returning the Widget tree describing the UI. This method is called again every time the state changes.
- Parameters:
context- the build context, carrying the player, GUI configuration, and similar info- Returns:
- the Widget tree
-
onOpen
public final void onOpen(@NotNull @NotNull org.bukkit.event.inventory.InventoryOpenEvent event) - Overrides:
onOpenin classmc.obliviate.inventory.Gui
-
onClose
public final void onClose(@NotNull @NotNull org.bukkit.event.inventory.InventoryCloseEvent event) - Overrides:
onClosein classmc.obliviate.inventory.Gui
-
onClick
public final boolean onClick(@NotNull @NotNull org.bukkit.event.inventory.InventoryClickEvent event) This is the framework's own bounds-check site (D-09 item 4), not a post-filter hook. A subclass overriding this method needs to know that obliviate-invs'InvListenercalls this method unconditionally, BEFORE it applies any bounds check of its own — so every click reaches here, including a click in the player's own inventory. The actual check happens one call down, inGuiRenderer.handleClick(org.bukkit.event.inventory.InventoryClickEvent): it rejects (returns without dispatching) any click whose raw slot falls outside the GUI's own inventory, so a click on the player's own inventory can never reach a GUI handler here.- Overrides:
onClickin classmc.obliviate.inventory.Gui- Parameters:
event- the click event- Returns:
- see
onGuiClick(InventoryClickEvent)
-
markNeedsBuild
protected void markNeedsBuild()Marks that a rebuild is needed.Typically called in a subclass to trigger a UI update when data changes.
-
setState
Sets state and triggers a rebuild.This is the Flutter-style approach to state management. Modify state inside the callback, and the framework automatically triggers a rebuild.
Usage example:
setState(() -> { counter++; // modify state selectedItem = item; });- Parameters:
action- the state-modifying operation
-
onGuiOpen
protected void onGuiOpen(@NotNull @NotNull org.bukkit.event.inventory.InventoryOpenEvent event) Hook method called when the GUI opens.Subclasses may override this to perform extra initialization work.
- Parameters:
event- the open event
-
onGuiClose
protected void onGuiClose(@NotNull @NotNull org.bukkit.event.inventory.InventoryCloseEvent event) Hook method called when the GUI closes.Subclasses may override this to perform cleanup work.
- Parameters:
event- the close event
-
onGuiClick
protected boolean onGuiClick(@NotNull @NotNull org.bukkit.event.inventory.InventoryClickEvent event) Hook method called on a GUI click.Note: the click event is handled by the declarative framework first, before this method is called.
The return value reads backwards, so read it carefully: returning
falsekeeps the event cancelled and the player cannot take the clicked item; returningtruelets the click through and the item can be taken. The semantics come from obliviate-invs, whoseInvListenercallssetCancelled(false)whenGui.onClickreturns true andsetCancelled(true)when it returns false. The default offalsematches the library base class and is the safe side.- Parameters:
event- the click event- Returns:
falsekeeps the event cancelled (default, item cannot be taken);truelets it through
-
getRenderer
Gets the renderer.- Returns:
- the GuiRenderer
-
isInitialized
public boolean isInitialized()Checks whether this has been initialized.- Returns:
- true if initialized
-
getPlayer
@NotNull public @NotNull org.bukkit.entity.Player getPlayer()Gets the player.- Returns:
- the player
-