java.lang.Object
mc.obliviate.inventory.Gui
com.ultikits.ultitools.abstracts.gui.declarative.engine.DeclarativeGui
All Implemented Interfaces:
org.bukkit.inventory.InventoryHolder
Direct Known Subclasses:
StatefulDeclarativeGui

public abstract class DeclarativeGui extends mc.obliviate.inventory.Gui
DeclarativeGui 是声明式 GUI 框架的基类。

它继承自 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

    Fields
    Modifier and Type
    Field
    Description
    protected final @NotNull String
     
    private boolean
     
    private final GuiRenderer
     

    Fields inherited from class mc.obliviate.inventory.Gui

    player
  • Constructor Summary

    Constructors
    Constructor
    Description
    DeclarativeGui(@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 Type
    Method
    Description
    abstract @NotNull Widget
    build(@NotNull BuildContext context)
    构建 Widget 树。
    @NotNull org.bukkit.entity.Player
    获取玩家。
    protected @NotNull GuiRenderer
    获取渲染器。
    boolean
    检查是否已初始化。
    protected void
    标记需要重建。
    final boolean
    onClick(@NotNull org.bukkit.event.inventory.InventoryClickEvent event)
     
    final void
    onClose(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event)
     
    protected boolean
    onGuiClick(@NotNull org.bukkit.event.inventory.InventoryClickEvent event)
    GUI 点击时的钩子方法。
    protected void
    onGuiClose(@NotNull org.bukkit.event.inventory.InventoryCloseEvent event)
    GUI 关闭时的钩子方法。
    protected void
    onGuiOpen(@NotNull org.bukkit.event.inventory.InventoryOpenEvent event)
    GUI 打开时的钩子方法。
    final void
    onOpen(@NotNull org.bukkit.event.inventory.InventoryOpenEvent event)
     
    protected void
    setState(@NotNull Runnable action)
    设置状态并触发重建。

    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • id

      @NotNull protected final @NotNull String id
    • renderer

      private final GuiRenderer 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 ID
      title - 标题
      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 ID
      title - 标题
      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 ID
      title - 标题(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 ID
      title - 标题(Component)
      inventoryType - 背包类型
  • Method Details

    • build

      @NotNull public abstract @NotNull Widget build(@NotNull @NotNull BuildContext context)
      构建 Widget 树。

      子类必须实现此方法,返回描述 UI 的 Widget 树。 每次状态变化时,这个方法会被重新调用。

      Parameters:
      context - 构建上下文,包含玩家、GUI 配置等信息
      Returns:
      Widget 树
    • onOpen

      public final void onOpen(@NotNull @NotNull org.bukkit.event.inventory.InventoryOpenEvent event)
      Overrides:
      onOpen in class mc.obliviate.inventory.Gui
    • onClose

      public final void onClose(@NotNull @NotNull org.bukkit.event.inventory.InventoryCloseEvent event)
      Overrides:
      onClose in class mc.obliviate.inventory.Gui
    • onClick

      public final boolean onClick(@NotNull @NotNull org.bukkit.event.inventory.InventoryClickEvent event)
      Overrides:
      onClick in class mc.obliviate.inventory.Gui
    • markNeedsBuild

      protected void markNeedsBuild()
      标记需要重建。

      通常在子类中,当数据变化时调用此方法触发 UI 更新。

    • setState

      protected void setState(@NotNull @NotNull Runnable action)
      设置状态并触发重建。

      这是 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 —— 其 InvListenerGui.onClick 返回 true 时调用 setCancelled(false),返回 false 时调用 setCancelled(true)。默认值 false 与库基类 Gui.onClick 的默认值保持一致,也是安全的一侧。

      The return value reads backwards, so read it carefully: returning false keeps the event cancelled and the player cannot take the clicked item; returning true lets the click through and the item can be taken. The semantics come from obliviate-invs, whose InvListener calls setCancelled(false) when Gui.onClick returns true and setCancelled(true) when it returns false. The default of false matches the library base class and is the safe side.

      Parameters:
      event - 点击事件
      Returns:
      false 保持事件取消(默认,物品拿不走);true 放行
    • getRenderer

      @NotNull protected @NotNull GuiRenderer getRenderer()
      获取渲染器。
      Returns:
      GuiRenderer
    • isInitialized

      public boolean isInitialized()
      检查是否已初始化。
      Returns:
      如果已初始化返回 true
    • getPlayer

      @NotNull public @NotNull org.bukkit.entity.Player getPlayer()
      获取玩家。
      Returns:
      玩家