Class DeclarativeGui
- All Implemented Interfaces:
org.bukkit.inventory.InventoryHolder
- Direct Known Subclasses:
StatefulDeclarativeGui
它继承自 obliviate-invs 的 Gui,提供声明式 UI 的能力。
子类只需要实现 build(BuildContext) 方法,返回 Widget 树即可。
使用示例:
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(
// 标题行
Center.builder()
.child(TextDisplay.title("Item Shop"))
.build(),
// 物品网格
GridView.builder()
.items(items)
.itemBuilder(item -> ItemButton.builder()
.item(item)
.onClick(() -> buyItem(item))
.build())
.build(),
// 分页控制
Row.builder()
.children(
PrevPageButton.builder().build(),
PageIndicator.builder().build(),
NextPageButton.builder().build()
)
.build()
)
.build();
}
private void buyItem(ItemStack item) {
// 购买逻辑
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) 创建 DeclarativeGui。DeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull String title, @NotNull org.bukkit.event.inventory.InventoryType inventoryType) 创建 DeclarativeGui。DeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull net.kyori.adventure.text.Component title, int rows) 创建 DeclarativeGui,使用 Component 标题。DeclarativeGui(@NotNull org.bukkit.entity.Player player, @NotNull String id, @NotNull net.kyori.adventure.text.Component title, @NotNull org.bukkit.event.inventory.InventoryType inventoryType) 创建 DeclarativeGui,使用 Component 标题。 -
Method Summary
Modifier and TypeMethodDescriptionabstract @NotNull Widgetbuild(@NotNull BuildContext context) 构建 Widget 树。@NotNull org.bukkit.entity.Player获取玩家。protected @NotNull GuiRenderer获取渲染器。boolean检查是否已初始化。protected void标记需要重建。final booleanonClick(@NotNull org.bukkit.event.inventory.InventoryClickEvent event) final voidonClose(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event) protected booleanonGuiClick(@NotNull org.bukkit.event.inventory.InventoryClickEvent event) GUI 点击时的钩子方法。protected voidonGuiClose(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event) GUI 关闭时的钩子方法。protected voidonGuiOpen(@NotNull org.bukkit.event.inventory.InventoryOpenEvent event) GUI 打开时的钩子方法。final voidonOpen(@NotNull org.bukkit.event.inventory.InventoryOpenEvent event) protected void设置状态并触发重建。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) 创建 DeclarativeGui。- Parameters:
player- 玩家id- GUI IDtitle- 标题rows- 行数
-
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) 创建 DeclarativeGui。- Parameters:
player- 玩家id- GUI IDtitle- 标题inventoryType- 背包类型
-
DeclarativeGui
public DeclarativeGui(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull String id, @NotNull @NotNull net.kyori.adventure.text.Component title, int rows) 创建 DeclarativeGui,使用 Component 标题。- Parameters:
player- 玩家id- GUI IDtitle- 标题(Component)rows- 行数
-
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) 创建 DeclarativeGui,使用 Component 标题。- Parameters:
player- 玩家id- GUI IDtitle- 标题(Component)inventoryType- 背包类型
-
-
Method Details
-
build
构建 Widget 树。子类必须实现此方法,返回描述 UI 的 Widget 树。 每次状态变化时,这个方法会被重新调用。
- Parameters:
context- 构建上下文,包含玩家、GUI 配置等信息- Returns:
- Widget 树
-
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) - Overrides:
onClickin classmc.obliviate.inventory.Gui
-
markNeedsBuild
protected void markNeedsBuild()标记需要重建。通常在子类中,当数据变化时调用此方法触发 UI 更新。
-
setState
设置状态并触发重建。这是 Flutter 风格的状态管理方式。在回调中修改状态, 框架会自动触发重建。
使用示例:
setState(() -> { counter++; // 修改状态 selectedItem = item; });- Parameters:
action- 状态修改操作
-
onGuiOpen
protected void onGuiOpen(@NotNull @NotNull org.bukkit.event.inventory.InventoryOpenEvent event) GUI 打开时的钩子方法。子类可以重写此方法执行额外的初始化工作。
- Parameters:
event- 打开事件
-
onGuiClose
protected void onGuiClose(@NotNull @NotNull org.bukkit.event.inventory.InventoryCloseEvent event) GUI 关闭时的钩子方法。子类可以重写此方法执行清理工作。
- Parameters:
event- 关闭事件
-
onGuiClick
protected boolean onGuiClick(@NotNull @NotNull org.bukkit.event.inventory.InventoryClickEvent event) GUI 点击时的钩子方法。注意:点击事件首先由声明式框架处理,然后才调用此方法。
返回值语义与直觉相反,务必看清:返回
false表示保持事件被取消, 玩家拿不走格子里的物品;返回true表示放行,玩家可以取走物品。 这一层语义来自 obliviate-invs —— 其InvListener在Gui.onClick返回 true 时调用setCancelled(false),返回 false 时调用setCancelled(true)。默认值false与库基类Gui.onClick的默认值保持一致,也是安全的一侧。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- 点击事件- Returns:
false保持事件取消(默认,物品拿不走);true放行
-
getRenderer
获取渲染器。- Returns:
- GuiRenderer
-
isInitialized
public boolean isInitialized()检查是否已初始化。- Returns:
- 如果已初始化返回 true
-
getPlayer
@NotNull public @NotNull org.bukkit.entity.Player getPlayer()获取玩家。- Returns:
- 玩家
-